diff --git a/src/client/index.tsx b/src/client/index.tsx index f0b9947..e7a05af 100755 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -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( diff --git a/src/server/gsheets/accessors.ts b/src/server/gsheets/accessors.ts index 2a03f4b..db502e7 100644 --- a/src/server/gsheets/accessors.ts +++ b/src/server/gsheets/accessors.ts @@ -219,7 +219,7 @@ export class Sheet< async dbFirstLoad(): Promise { if (!(await hasGSheetsAccess())) { await this.loadLocalDb() - } else if (this.toRunAfterLoad && __DEV__) { + } else if (this.toRunAfterLoad) { // Save once this.toRunAfterLoad.push(() => this.saveLocalDb()) } diff --git a/src/server/secure.ts b/src/server/secure.ts index 2fe2024..4a5ddf5 100644 --- a/src/server/secure.ts +++ b/src/server/secure.ts @@ -65,14 +65,6 @@ async function getSecret() { } export async function getJwt(id: number, roles: string[]): Promise { - const jwt = sign( - { id, roles }, - await getSecret() - // __TEST__ - // ? undefined - // : { - // expiresIn: "365d", - // } - ) + const jwt = sign({ id, roles }, await getSecret()) return jwt } diff --git a/src/store/auth.ts b/src/store/auth.ts index 0d1dfb0..235aa1a 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -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) => { + 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 = "" }, }, }) diff --git a/src/store/index.ts b/src/store/index.ts index 132480b..ad2e0bf 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -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 }) diff --git a/src/store/volunteerDayWishesSet.ts b/src/store/volunteerDayWishesSet.ts index f5420cc..ca8991e 100644 --- a/src/store/volunteerDayWishesSet.ts +++ b/src/store/volunteerDayWishesSet.ts @@ -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)) diff --git a/src/store/volunteerNotifsSet.ts b/src/store/volunteerNotifsSet.ts index a0f24ef..a34db8c 100644 --- a/src/store/volunteerNotifsSet.ts +++ b/src/store/volunteerNotifsSet.ts @@ -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)) diff --git a/src/store/volunteerParticipationDetailsSet.ts b/src/store/volunteerParticipationDetailsSet.ts index 521d9f8..50f36d3 100644 --- a/src/store/volunteerParticipationDetailsSet.ts +++ b/src/store/volunteerParticipationDetailsSet.ts @@ -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)) diff --git a/src/store/volunteerTeamWishesSet.ts b/src/store/volunteerTeamWishesSet.ts index 179af1d..21d17e3 100644 --- a/src/store/volunteerTeamWishesSet.ts +++ b/src/store/volunteerTeamWishesSet.ts @@ -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))