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
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(

View File

@ -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())
}

View File

@ -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
}

View File

@ -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 = ""
},
},
})

View File

@ -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 })

View File

@ -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))

View File

@ -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))

View File

@ -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))

View File

@ -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))