Fix session handling across multiple pages

This commit is contained in:
Mihovil Ilakovac 2024-06-03 20:38:08 +02:00
parent 246ae9c782
commit 667dee66dc

View File

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