|
1 | | -import { AuthError } from '../src/lib/errors' |
| 1 | +import { AuthError, AuthPKCECodeVerifierMissingError } from '../src/lib/errors' |
2 | 2 | import { STORAGE_KEY } from '../src/lib/constants' |
3 | 3 | import { memoryLocalStorageAdapter } from '../src/lib/local-storage' |
4 | 4 | import GoTrueClient from '../src/GoTrueClient' |
@@ -455,11 +455,59 @@ describe('GoTrueClient', () => { |
455 | 455 | }) |
456 | 456 |
|
457 | 457 | test('exchangeCodeForSession() should fail with invalid authCode', async () => { |
458 | | - const { error } = await pkceClient.exchangeCodeForSession('mock_code') |
| 458 | + // Mock fetch to return a 400 error for invalid auth code |
| 459 | + const mockFetch = jest.fn().mockResolvedValue({ |
| 460 | + ok: false, |
| 461 | + status: 400, |
| 462 | + headers: new Headers(), |
| 463 | + json: () => |
| 464 | + Promise.resolve({ |
| 465 | + error: 'invalid_grant', |
| 466 | + error_description: 'Invalid auth code', |
| 467 | + }), |
| 468 | + }) |
| 469 | + |
| 470 | + const storage = memoryLocalStorageAdapter() |
| 471 | + const client = new GoTrueClient({ |
| 472 | + url: GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON, |
| 473 | + autoRefreshToken: false, |
| 474 | + persistSession: true, |
| 475 | + storage, |
| 476 | + flowType: 'pkce', |
| 477 | + fetch: mockFetch, |
| 478 | + }) |
| 479 | + |
| 480 | + // Set up a code verifier so we can test the invalid auth code error |
| 481 | + // @ts-expect-error 'Allow access to protected storageKey' |
| 482 | + const storageKey = client.storageKey |
| 483 | + await storage.setItem(`${storageKey}-code-verifier`, 'mock-verifier') |
| 484 | + |
| 485 | + const { error } = await client.exchangeCodeForSession('mock_code') |
459 | 486 |
|
460 | 487 | expect(error).not.toBeNull() |
461 | 488 | expect(error?.status).toEqual(400) |
462 | 489 | }) |
| 490 | + |
| 491 | + test('exchangeCodeForSession() should throw helpful error when code verifier is missing', async () => { |
| 492 | + const storage = memoryLocalStorageAdapter() |
| 493 | + // Don't set a code verifier - this simulates the common issue where |
| 494 | + // the auth flow was initiated in a different browser/device |
| 495 | + |
| 496 | + const client = new GoTrueClient({ |
| 497 | + url: GOTRUE_URL_SIGNUP_ENABLED_AUTO_CONFIRM_ON, |
| 498 | + autoRefreshToken: false, |
| 499 | + persistSession: true, |
| 500 | + storage, |
| 501 | + flowType: 'pkce', |
| 502 | + }) |
| 503 | + |
| 504 | + const { error } = await client.exchangeCodeForSession('some-auth-code') |
| 505 | + |
| 506 | + expect(error).toBeInstanceOf(AuthPKCECodeVerifierMissingError) |
| 507 | + expect(error?.message).toContain('PKCE code verifier not found in storage') |
| 508 | + expect(error?.message).toContain('@supabase/ssr') |
| 509 | + expect(error?.code).toEqual('pkce_code_verifier_not_found') |
| 510 | + }) |
463 | 511 | }) |
464 | 512 |
|
465 | 513 | describe('Email Auth', () => { |
|
0 commit comments