Fix auth with jwt and localDb only in dev

This commit is contained in:
pikiou 2022-03-11 14:00:54 +01:00
parent ed8f20072f
commit 79b0dd47bf
9 changed files with 19 additions and 25 deletions

View File

@ -11,13 +11,15 @@ import routes from "../routes"
// Get the initial state from server-side rendering // Get the initial state from server-side rendering
const initialState = window.__INITIAL_STATE__ const initialState = window.__INITIAL_STATE__
const id = +(Cookies.get("id") || 0)
const jwt = Cookies.get("jwt") const jwt = Cookies.get("jwt")
if (id && jwt) { const id = +(Cookies.get("id") || 0)
Cookies.set("id", `${id}`, { expires: 3650 }) const roles = Cookies.get("roles")?.split(",") || []
if (jwt && id && roles) {
Cookies.set("jwt", jwt, { expires: 3650 }) 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[]) => const render = (Routes: RouteConfig[]) =>
ReactDOM.hydrate( ReactDOM.hydrate(

View File

@ -219,7 +219,7 @@ export class Sheet<
async dbFirstLoad(): Promise<void> { async dbFirstLoad(): Promise<void> {
if (!(await hasGSheetsAccess())) { if (!(await hasGSheetsAccess())) {
await this.loadLocalDb() await this.loadLocalDb()
} else if (this.toRunAfterLoad && __DEV__) { } else if (this.toRunAfterLoad) {
// Save once // Save once
this.toRunAfterLoad.push(() => this.saveLocalDb()) this.toRunAfterLoad.push(() => this.saveLocalDb())
} }

View File

@ -65,14 +65,6 @@ async function getSecret() {
} }
export async function getJwt(id: number, roles: string[]): Promise<string> { export async function getJwt(id: number, roles: string[]): Promise<string> {
const jwt = sign( const jwt = sign({ id, roles }, await getSecret())
{ id, roles },
await getSecret()
// __TEST__
// ? undefined
// : {
// expiresIn: "365d",
// }
)
return jwt return jwt
} }

View File

@ -3,16 +3,16 @@ import { AppState } from "."
// Define a type for the slice state // Define a type for the slice state
interface AuthState { interface AuthState {
jwt: string
id: number id: number
roles: string[] roles: string[]
jwt: string
} }
// Define the initial state using that type // Define the initial state using that type
const initialState: AuthState = { const initialState: AuthState = {
jwt: "",
id: 0, id: 0,
roles: [], roles: [],
jwt: "",
} }
export const auth = createSlice({ export const auth = createSlice({
@ -20,15 +20,15 @@ export const auth = createSlice({
initialState, initialState,
reducers: { reducers: {
setCurrentUser: (state, action: PayloadAction<AuthState>) => { setCurrentUser: (state, action: PayloadAction<AuthState>) => {
state.jwt = action.payload.jwt
state.id = action.payload.id state.id = action.payload.id
state.roles = action.payload.roles state.roles = action.payload.roles
state.jwt = action.payload.jwt
}, },
logoutUser: (state) => { logoutUser: (state) => {
// Unused, just reload page :/ // Unused, just reload page :/
state.jwt = ""
state.id = 0 state.id = 0
state.roles = [] state.roles = []
state.jwt = ""
}, },
}, },
}) })

View File

@ -42,13 +42,13 @@ const createStore = ({ initialState, url, jwt, id, roles }: Arg = {}) => {
return { store, history } return { store, history }
} }
const jwt = Cookies.get("jwt")
const id = +(Cookies.get("id") || 0) const id = +(Cookies.get("id") || 0)
const roles = Cookies.get("roles")?.split(",") || [] const roles = Cookies.get("roles")?.split(",") || []
const jwt = Cookies.get("jwt") if (jwt && id && roles) {
if (id && jwt && roles) { Cookies.set("jwt", jwt, { expires: 3650 })
Cookies.set("id", `${id}`, { expires: 3650 }) Cookies.set("id", `${id}`, { expires: 3650 })
Cookies.set("roles", roles.join(","), { expires: 3650 }) Cookies.set("roles", roles.join(","), { expires: 3650 })
Cookies.set("jwt", jwt, { expires: 3650 })
} }
const { store } = createStore({ jwt, id, roles }) const { store } = createStore({ jwt, id, roles })

View File

@ -50,7 +50,7 @@ export const fetchVolunteerDayWishesSetIfNeed =
let jwt = "" let jwt = ""
if (!id) { if (!id) {
;({ id, jwt } = getState().auth) ;({ jwt, id } = getState().auth)
} }
if (shouldFetchVolunteerDayWishesSet(getState(), id)) if (shouldFetchVolunteerDayWishesSet(getState(), id))
return dispatch(fetchVolunteerDayWishesSet(jwt, id, wishes)) return dispatch(fetchVolunteerDayWishesSet(jwt, id, wishes))

View File

@ -50,7 +50,7 @@ export const fetchVolunteerNotifsSetIfNeed =
let jwt = "" let jwt = ""
if (!id) { if (!id) {
;({ id, jwt } = getState().auth) ;({ jwt, id } = getState().auth)
} }
if (shouldFetchVolunteerNotifsSet(getState(), id)) if (shouldFetchVolunteerNotifsSet(getState(), id))
return dispatch(fetchVolunteerNotifsSet(jwt, id, notif)) return dispatch(fetchVolunteerNotifsSet(jwt, id, notif))

View File

@ -54,7 +54,7 @@ export const fetchVolunteerParticipationDetailsSetIfNeed =
let jwt = "" let jwt = ""
if (!id) { if (!id) {
;({ id, jwt } = getState().auth) ;({ jwt, id } = getState().auth)
} }
if (shouldFetchVolunteerParticipationDetailsSet(getState(), id)) if (shouldFetchVolunteerParticipationDetailsSet(getState(), id))
return dispatch(fetchVolunteerParticipationDetailsSet(jwt, id, wishes)) return dispatch(fetchVolunteerParticipationDetailsSet(jwt, id, wishes))

View File

@ -50,7 +50,7 @@ export const fetchVolunteerTeamWishesSetIfNeed =
let jwt = "" let jwt = ""
if (!id) { if (!id) {
;({ id, jwt } = getState().auth) ;({ jwt, id } = getState().auth)
} }
if (shouldFetchVolunteerTeamWishesSet(getState(), id)) if (shouldFetchVolunteerTeamWishesSet(getState(), id))
return dispatch(fetchVolunteerTeamWishesSet(jwt, id, wishes)) return dispatch(fetchVolunteerTeamWishesSet(jwt, id, wishes))