mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-04 00:56:43 +09:00
## Summary - `fetchUser()` was clearing the access token on ANY error (network failures, timeouts, aborted requests during page redirects) - This caused users to get logged out when returning from external flows like Stripe Connect onboarding — the redirect chain triggers transient fetch errors that wiped the token - Now only clears the token when the server explicitly returns 401 (session expired/invalid) This is the root cause of the "logged out after Stripe Connect" issue reported alongside #702. ## Test plan - [ ] Manual: complete Stripe Connect flow, verify user stays logged in on return - [x] Existing auth tests still pass (no behavior change for actual 401s) @coderabbitai ignore 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
/**
|
|
* Tests for auth store's fetchUser() behavior, specifically:
|
|
* - Abort errors (from page navigation) should NOT clear auth
|
|
* - 401 errors (expired session) SHOULD clear auth
|
|
* - Network errors (fetch failures) should NOT clear auth
|
|
*/
|
|
|
|
// Mock api module before importing auth
|
|
const mockGetCurrentUser = jest.fn()
|
|
jest.mock('@/lib/api', () => ({
|
|
api: { getCurrentUser: (...args: unknown[]) => mockGetCurrentUser(...args) },
|
|
adminApi: {},
|
|
setAccessToken: jest.fn(),
|
|
clearAccessToken: jest.fn(),
|
|
getAccessToken: jest.fn(() => 'test-token'),
|
|
}))
|
|
|
|
import { useAuth } from '../auth'
|
|
import { clearAccessToken } from '../api'
|
|
|
|
describe('fetchUser', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks()
|
|
// Reset store to a known state with a user
|
|
useAuth.setState({
|
|
user: {
|
|
token: 'test-token',
|
|
email: 'user@test.com',
|
|
role: 'user',
|
|
credits: 5,
|
|
display_name: 'Test User',
|
|
total_jobs_created: 1,
|
|
total_jobs_completed: 0,
|
|
feedback_eligible: false,
|
|
},
|
|
isLoading: false,
|
|
hasHydrated: true,
|
|
})
|
|
})
|
|
|
|
it('preserves auth state when fetch is aborted (page navigation)', async () => {
|
|
// Simulate an AbortError (thrown when browser navigates away during fetch)
|
|
mockGetCurrentUser.mockRejectedValueOnce(new DOMException('The operation was aborted.', 'AbortError'))
|
|
|
|
const result = await useAuth.getState().fetchUser()
|
|
|
|
expect(result).toBe(false)
|
|
// Auth should NOT be cleared
|
|
expect(clearAccessToken).not.toHaveBeenCalled()
|
|
expect(useAuth.getState().user).not.toBeNull()
|
|
expect(useAuth.getState().isLoading).toBe(false)
|
|
})
|
|
|
|
it('clears auth state when API returns 401 (expired session)', async () => {
|
|
const err = new Error('Unauthorized') as Error & { status: number }
|
|
err.status = 401
|
|
mockGetCurrentUser.mockRejectedValueOnce(err)
|
|
|
|
const result = await useAuth.getState().fetchUser()
|
|
|
|
expect(result).toBe(false)
|
|
// Auth SHOULD be cleared on explicit 401
|
|
expect(clearAccessToken).toHaveBeenCalled()
|
|
expect(useAuth.getState().user).toBeNull()
|
|
expect(useAuth.getState().isLoading).toBe(false)
|
|
})
|
|
|
|
it('preserves auth state on network errors (non-401)', async () => {
|
|
mockGetCurrentUser.mockRejectedValueOnce(new TypeError('Failed to fetch'))
|
|
|
|
const result = await useAuth.getState().fetchUser()
|
|
|
|
expect(result).toBe(false)
|
|
// Auth should NOT be cleared on network errors
|
|
expect(clearAccessToken).not.toHaveBeenCalled()
|
|
expect(useAuth.getState().isLoading).toBe(false)
|
|
})
|
|
|
|
it('sets user on successful fetch', async () => {
|
|
mockGetCurrentUser.mockResolvedValueOnce({
|
|
user: {
|
|
email: 'fresh@test.com',
|
|
role: 'admin',
|
|
credits: 10,
|
|
display_name: 'Fresh User',
|
|
total_jobs_created: 5,
|
|
total_jobs_completed: 3,
|
|
feedback_eligible: true,
|
|
},
|
|
})
|
|
|
|
const result = await useAuth.getState().fetchUser()
|
|
|
|
expect(result).toBe(true)
|
|
expect(useAuth.getState().user?.email).toBe('fresh@test.com')
|
|
expect(useAuth.getState().user?.role).toBe('admin')
|
|
expect(useAuth.getState().isLoading).toBe(false)
|
|
})
|
|
})
|