mirror of
https://github.com/wasp-lang/wasp.git
synced 2024-11-26 10:54:12 +03:00
Fix session handling across multiple pages
This commit is contained in:
parent
246ae9c782
commit
667dee66dc
@ -15,9 +15,11 @@ let waspAppAuthSessionId = storage.get(WASP_APP_AUTH_SESSION_ID_NAME) as string
|
|||||||
|
|
||||||
// PRIVATE API (sdk)
|
// PRIVATE API (sdk)
|
||||||
export function setSessionId(sessionId: string): void {
|
export function setSessionId(sessionId: string): void {
|
||||||
waspAppAuthSessionId = sessionId
|
if (waspAppAuthSessionId !== sessionId) {
|
||||||
storage.set(WASP_APP_AUTH_SESSION_ID_NAME, sessionId)
|
waspAppAuthSessionId = sessionId
|
||||||
apiEventsEmitter.emit('sessionId.set')
|
storage.set(WASP_APP_AUTH_SESSION_ID_NAME, sessionId)
|
||||||
|
apiEventsEmitter.emit('sessionId.set')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PRIVATE API (sdk)
|
// PRIVATE API (sdk)
|
||||||
@ -27,9 +29,12 @@ export function getSessionId(): string | undefined {
|
|||||||
|
|
||||||
// PRIVATE API (sdk)
|
// PRIVATE API (sdk)
|
||||||
export function clearSessionId(): void {
|
export function clearSessionId(): void {
|
||||||
waspAppAuthSessionId = undefined
|
const sessionIdInStorage = storage.get(WASP_APP_AUTH_SESSION_ID_NAME) as string | undefined
|
||||||
storage.remove(WASP_APP_AUTH_SESSION_ID_NAME)
|
if (!sessionIdInStorage || sessionIdInStorage === waspAppAuthSessionId) {
|
||||||
apiEventsEmitter.emit('sessionId.clear')
|
waspAppAuthSessionId = undefined
|
||||||
|
storage.remove(WASP_APP_AUTH_SESSION_ID_NAME)
|
||||||
|
apiEventsEmitter.emit('sessionId.clear')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// PRIVATE API (sdk)
|
// PRIVATE API (sdk)
|
||||||
@ -49,7 +54,10 @@ api.interceptors.request.use((request) => {
|
|||||||
|
|
||||||
api.interceptors.response.use(undefined, (error) => {
|
api.interceptors.response.use(undefined, (error) => {
|
||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
clearSessionId()
|
const sessionIdInStorage = storage.get(WASP_APP_AUTH_SESSION_ID_NAME) as string | undefined
|
||||||
|
if (!sessionIdInStorage || sessionIdInStorage === waspAppAuthSessionId) {
|
||||||
|
clearSessionId()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user