mirror of
https://github.com/Paris-est-Ludique/intranet.git
synced 2025-06-08 08:34:20 +02:00
Fix auth with jwt and localDb only in dev
This commit is contained in:
parent
ed8f20072f
commit
79b0dd47bf
@ -11,13 +11,15 @@ import routes from "../routes"
|
||||
// Get the initial state from server-side rendering
|
||||
const initialState = window.__INITIAL_STATE__
|
||||
|
||||
const id = +(Cookies.get("id") || 0)
|
||||
const jwt = Cookies.get("jwt")
|
||||
if (id && jwt) {
|
||||
Cookies.set("id", `${id}`, { expires: 3650 })
|
||||
const id = +(Cookies.get("id") || 0)
|
||||
const roles = Cookies.get("roles")?.split(",") || []
|
||||
if (jwt && id && roles) {
|
||||
Cookies.set("jwt", jwt, { expires: 3650 })
|
||||
Cookies.set("id", `${id}`, { expires: 3650 })
|
||||
Cookies.set("roles", roles.join(","), { expires: 3650 })
|
||||
}
|
||||
const { store, history } = createStore({ initialState, id, jwt })
|
||||
const { store, history } = createStore({ initialState, jwt, id, roles })
|
||||
|
||||
const render = (Routes: RouteConfig[]) =>
|
||||
ReactDOM.hydrate(
|
||||
|
@ -219,7 +219,7 @@ export class Sheet<
|
||||
async dbFirstLoad(): Promise<void> {
|
||||
if (!(await hasGSheetsAccess())) {
|
||||
await this.loadLocalDb()
|
||||
} else if (this.toRunAfterLoad && __DEV__) {
|
||||
} else if (this.toRunAfterLoad) {
|
||||
// Save once
|
||||
this.toRunAfterLoad.push(() => this.saveLocalDb())
|
||||
}
|
||||
|
@ -65,14 +65,6 @@ async function getSecret() {
|
||||
}
|
||||
|
||||
export async function getJwt(id: number, roles: string[]): Promise<string> {
|
||||
const jwt = sign(
|
||||
{ id, roles },
|
||||
await getSecret()
|
||||
// __TEST__
|
||||
// ? undefined
|
||||
// : {
|
||||
// expiresIn: "365d",
|
||||
// }
|
||||
)
|
||||
const jwt = sign({ id, roles }, await getSecret())
|
||||
return jwt
|
||||
}
|
||||
|
@ -3,16 +3,16 @@ import { AppState } from "."
|
||||
|
||||
// Define a type for the slice state
|
||||
interface AuthState {
|
||||
jwt: string
|
||||
id: number
|
||||
roles: string[]
|
||||
jwt: string
|
||||
}
|
||||
|
||||
// Define the initial state using that type
|
||||
const initialState: AuthState = {
|
||||
jwt: "",
|
||||
id: 0,
|
||||
roles: [],
|
||||
jwt: "",
|
||||
}
|
||||
|
||||
export const auth = createSlice({
|
||||
@ -20,15 +20,15 @@ export const auth = createSlice({
|
||||
initialState,
|
||||
reducers: {
|
||||
setCurrentUser: (state, action: PayloadAction<AuthState>) => {
|
||||
state.jwt = action.payload.jwt
|
||||
state.id = action.payload.id
|
||||
state.roles = action.payload.roles
|
||||
state.jwt = action.payload.jwt
|
||||
},
|
||||
logoutUser: (state) => {
|
||||
// Unused, just reload page :/
|
||||
state.jwt = ""
|
||||
state.id = 0
|
||||
state.roles = []
|
||||
state.jwt = ""
|
||||
},
|
||||
},
|
||||
})
|
||||
|
@ -42,13 +42,13 @@ const createStore = ({ initialState, url, jwt, id, roles }: Arg = {}) => {
|
||||
return { store, history }
|
||||
}
|
||||
|
||||
const jwt = Cookies.get("jwt")
|
||||
const id = +(Cookies.get("id") || 0)
|
||||
const roles = Cookies.get("roles")?.split(",") || []
|
||||
const jwt = Cookies.get("jwt")
|
||||
if (id && jwt && roles) {
|
||||
if (jwt && id && roles) {
|
||||
Cookies.set("jwt", jwt, { expires: 3650 })
|
||||
Cookies.set("id", `${id}`, { expires: 3650 })
|
||||
Cookies.set("roles", roles.join(","), { expires: 3650 })
|
||||
Cookies.set("jwt", jwt, { expires: 3650 })
|
||||
}
|
||||
const { store } = createStore({ jwt, id, roles })
|
||||
|
||||
|
@ -50,7 +50,7 @@ export const fetchVolunteerDayWishesSetIfNeed =
|
||||
let jwt = ""
|
||||
|
||||
if (!id) {
|
||||
;({ id, jwt } = getState().auth)
|
||||
;({ jwt, id } = getState().auth)
|
||||
}
|
||||
if (shouldFetchVolunteerDayWishesSet(getState(), id))
|
||||
return dispatch(fetchVolunteerDayWishesSet(jwt, id, wishes))
|
||||
|
@ -50,7 +50,7 @@ export const fetchVolunteerNotifsSetIfNeed =
|
||||
let jwt = ""
|
||||
|
||||
if (!id) {
|
||||
;({ id, jwt } = getState().auth)
|
||||
;({ jwt, id } = getState().auth)
|
||||
}
|
||||
if (shouldFetchVolunteerNotifsSet(getState(), id))
|
||||
return dispatch(fetchVolunteerNotifsSet(jwt, id, notif))
|
||||
|
@ -54,7 +54,7 @@ export const fetchVolunteerParticipationDetailsSetIfNeed =
|
||||
let jwt = ""
|
||||
|
||||
if (!id) {
|
||||
;({ id, jwt } = getState().auth)
|
||||
;({ jwt, id } = getState().auth)
|
||||
}
|
||||
if (shouldFetchVolunteerParticipationDetailsSet(getState(), id))
|
||||
return dispatch(fetchVolunteerParticipationDetailsSet(jwt, id, wishes))
|
||||
|
@ -50,7 +50,7 @@ export const fetchVolunteerTeamWishesSetIfNeed =
|
||||
let jwt = ""
|
||||
|
||||
if (!id) {
|
||||
;({ id, jwt } = getState().auth)
|
||||
;({ jwt, id } = getState().auth)
|
||||
}
|
||||
if (shouldFetchVolunteerTeamWishesSet(getState(), id))
|
||||
return dispatch(fetchVolunteerTeamWishesSet(jwt, id, wishes))
|
||||
|
Loading…
x
Reference in New Issue
Block a user