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