Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix session handling across multiple pages #2077

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions waspc/data/Generator/templates/sdk/wasp/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ let waspAppAuthSessionId = storage.get(WASP_APP_AUTH_SESSION_ID_NAME) as string

// PRIVATE API (sdk)
export function setSessionId(sessionId: string): void {
waspAppAuthSessionId = sessionId
storage.set(WASP_APP_AUTH_SESSION_ID_NAME, sessionId)
apiEventsEmitter.emit('sessionId.set')
if (waspAppAuthSessionId !== sessionId) {
waspAppAuthSessionId = sessionId
storage.set(WASP_APP_AUTH_SESSION_ID_NAME, sessionId)
apiEventsEmitter.emit('sessionId.set')
}
}

// PRIVATE API (sdk)
Expand All @@ -27,9 +29,12 @@ export function getSessionId(): string | undefined {

// PRIVATE API (sdk)
export function clearSessionId(): void {
waspAppAuthSessionId = undefined
storage.remove(WASP_APP_AUTH_SESSION_ID_NAME)
apiEventsEmitter.emit('sessionId.clear')
const sessionIdInStorage = storage.get(WASP_APP_AUTH_SESSION_ID_NAME) as string | undefined
if (!sessionIdInStorage || sessionIdInStorage === waspAppAuthSessionId) {
waspAppAuthSessionId = undefined
storage.remove(WASP_APP_AUTH_SESSION_ID_NAME)
apiEventsEmitter.emit('sessionId.clear')
}
}

// PRIVATE API (sdk)
Expand All @@ -49,7 +54,10 @@ api.interceptors.request.use((request) => {

api.interceptors.response.use(undefined, (error) => {
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)
})
Expand Down
Loading