fix(frontend): restore pending auth session flow

This commit is contained in:
IanShaw027
2026-04-21 00:05:44 +08:00
parent 4f6966d7b3
commit 85fc54b205
6 changed files with 556 additions and 24 deletions

View File

@@ -211,6 +211,78 @@ describe('useAuthStore', () => {
expect(store.isAuthenticated).toBe(true)
})
it('恢复持久化 pending auth session', () => {
localStorage.setItem(
'pending_auth_session',
JSON.stringify({
token: 'pending-token',
token_field: 'pending_auth_token',
provider: 'wechat',
redirect: '/profile',
})
)
const store = useAuthStore()
store.checkAuth()
expect(store.hasPendingAuthSession).toBe(true)
expect(store.pendingAuthSession).toEqual({
token: 'pending-token',
token_field: 'pending_auth_token',
provider: 'wechat',
redirect: '/profile',
})
})
})
describe('pending auth session', () => {
it('persists and clears pending auth session state', () => {
const store = useAuthStore()
store.setPendingAuthSession({
token: 'pending-token',
token_field: 'pending_auth_token',
provider: 'wechat',
redirect: '/profile',
})
expect(store.hasPendingAuthSession).toBe(true)
expect(JSON.parse(localStorage.getItem('pending_auth_session') || 'null')).toEqual({
token: 'pending-token',
token_field: 'pending_auth_token',
provider: 'wechat',
redirect: '/profile',
})
store.clearPendingAuthSession()
expect(store.hasPendingAuthSession).toBe(false)
expect(localStorage.getItem('pending_auth_session')).toBeNull()
})
it('preserves pending auth session when registration fails', async () => {
const store = useAuthStore()
store.setPendingAuthSession({
token: 'pending-token',
token_field: 'pending_auth_token',
provider: 'oidc',
redirect: '/register',
})
mockRegister.mockRejectedValue(new Error('Register failed'))
await expect(
store.register({ email: 'user@example.com', password: 'secret-123' })
).rejects.toThrow('Register failed')
expect(store.hasPendingAuthSession).toBe(true)
expect(store.pendingAuthSession).toEqual({
token: 'pending-token',
token_field: 'pending_auth_token',
provider: 'oidc',
redirect: '/register',
})
})
})
// --- isAdmin ---