Skip to content

Commit 314cca0

Browse files
authored
fix(auth): clear local session on signout failures (#2504)
1 parent 559dfb0 commit 314cca0

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

‎packages/core/auth-js/src/GoTrueClient.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,6 +4000,10 @@ export default class GoTrueClient {
40004000
{ scope }: SignOut = { scope: 'global' }
40014001
): Promise<{ error: AuthError | null }> {
40024002
return await this._useSession(async (result) => {
4003+
const removeCurrentSession = async () => {
4004+
await this._removeSession()
4005+
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
4006+
}
40034007
const { data, error: sessionError } = result
40044008
if (sessionError && !isAuthSessionMissingError(sessionError)) {
40054009
return this._returnResult({ error: sessionError })
@@ -4017,13 +4021,15 @@ export default class GoTrueClient {
40174021
isAuthSessionMissingError(error)
40184022
)
40194023
) {
4024+
if (scope !== 'others') {
4025+
await removeCurrentSession()
4026+
}
40204027
return this._returnResult({ error })
40214028
}
40224029
}
40234030
}
40244031
if (scope !== 'others') {
4025-
await this._removeSession()
4026-
await removeItemAsync(this.storage, `${this.storageKey}-code-verifier`)
4032+
await removeCurrentSession()
40274033
}
40284034
return this._returnResult({ error: null })
40294035
})
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import GoTrueClient from '../src/GoTrueClient'
2+
import { memoryLocalStorageAdapter } from '../src/lib/local-storage'
3+
4+
const createStoredSession = () => ({
5+
access_token: 'test-access-token',
6+
refresh_token: 'test-refresh-token',
7+
token_type: 'bearer',
8+
expires_in: 3600,
9+
expires_at: Math.floor(Date.now() / 1000) + 3600,
10+
user: {
11+
id: 'test-user-id',
12+
aud: 'authenticated',
13+
role: 'authenticated',
14+
email: 'user@example.com',
15+
app_metadata: {},
16+
user_metadata: {},
17+
created_at: new Date(0).toISOString(),
18+
},
19+
})
20+
21+
describe('GoTrueClient signOut offline cleanup', () => {
22+
let consoleErrorSpy: jest.SpyInstance
23+
24+
beforeEach(() => {
25+
consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {})
26+
})
27+
28+
afterEach(() => {
29+
consoleErrorSpy.mockRestore()
30+
})
31+
32+
test.each(['global', 'local'] as const)(
33+
'removes the local session when %s signOut cannot reach the auth server',
34+
async (scope) => {
35+
const storageKey = `test-signout-offline-${scope}`
36+
const storage = memoryLocalStorageAdapter({
37+
[storageKey]: JSON.stringify(createStoredSession()),
38+
[`${storageKey}-code-verifier`]: 'test-code-verifier',
39+
})
40+
const mockFetch = jest.fn().mockRejectedValue(new Error('offline'))
41+
const client = new GoTrueClient({
42+
url: 'http://localhost:9999',
43+
autoRefreshToken: false,
44+
persistSession: true,
45+
storage,
46+
storageKey,
47+
fetch: mockFetch,
48+
})
49+
50+
const { error } = await client.signOut({ scope })
51+
52+
expect(error?.name).toBe('AuthRetryableFetchError')
53+
expect(await storage.getItem(storageKey)).toBeNull()
54+
expect(await storage.getItem(`${storageKey}-code-verifier`)).toBeNull()
55+
}
56+
)
57+
58+
test('keeps the current session for others scope when remote signOut fails', async () => {
59+
const storageKey = 'test-signout-offline-others'
60+
const storage = memoryLocalStorageAdapter({
61+
[storageKey]: JSON.stringify(createStoredSession()),
62+
})
63+
const mockFetch = jest.fn().mockRejectedValue(new Error('offline'))
64+
const client = new GoTrueClient({
65+
url: 'http://localhost:9999',
66+
autoRefreshToken: false,
67+
persistSession: true,
68+
storage,
69+
storageKey,
70+
fetch: mockFetch,
71+
})
72+
73+
const { error } = await client.signOut({ scope: 'others' })
74+
75+
expect(error?.name).toBe('AuthRetryableFetchError')
76+
expect(await storage.getItem(storageKey)).not.toBeNull()
77+
})
78+
})

0 commit comments

Comments
 (0)