"git@web.lueluesay.top:chenxi/sub2api.git" did not exist on "c048ca80a458772a2ffca28313b8a9e01ca7e019"
Commit a70f7aca authored by IanShaw027's avatar IanShaw027
Browse files

fix pending auth session restore

parent ea27ac6f
...@@ -261,6 +261,35 @@ describe('useAuthStore', () => { ...@@ -261,6 +261,35 @@ describe('useAuthStore', () => {
expect(localStorage.getItem('pending_auth_session')).toBeNull() expect(localStorage.getItem('pending_auth_session')).toBeNull()
}) })
it('restores a persisted pending oauth session without requiring a token value', () => {
const firstStore = useAuthStore()
firstStore.setPendingAuthSession({
token: '',
token_field: 'pending_oauth_token',
provider: 'oidc',
redirect: '/welcome',
adoption_required: true,
suggested_display_name: 'OIDC Nick'
})
setActivePinia(createPinia())
const restoredStore = useAuthStore()
restoredStore.checkAuth()
expect(restoredStore.isAuthenticated).toBe(false)
expect(restoredStore.hasPendingAuthSession).toBe(true)
expect(restoredStore.pendingAuthSession).toEqual({
token: '',
token_field: 'pending_oauth_token',
provider: 'oidc',
redirect: '/welcome',
adoption_required: true,
suggested_display_name: 'OIDC Nick',
suggested_avatar_url: undefined
})
})
it('preserves pending auth session when registration fails', async () => { it('preserves pending auth session when registration fails', async () => {
const store = useAuthStore() const store = useAuthStore()
store.setPendingAuthSession({ store.setPendingAuthSession({
......
...@@ -28,6 +28,10 @@ interface PendingAuthSessionSummary { ...@@ -28,6 +28,10 @@ interface PendingAuthSessionSummary {
suggested_avatar_url?: string suggested_avatar_url?: string
} }
function normalizePendingAuthTokenField(value: unknown): PendingAuthTokenField {
return value === 'pending_oauth_token' ? 'pending_oauth_token' : 'pending_auth_token'
}
function getPersistedPendingAuthSession(): PendingAuthSessionSummary | null { function getPersistedPendingAuthSession(): PendingAuthSessionSummary | null {
const raw = localStorage.getItem(PENDING_AUTH_SESSION_KEY) const raw = localStorage.getItem(PENDING_AUTH_SESSION_KEY)
if (!raw) { if (!raw) {
...@@ -35,18 +39,20 @@ function getPersistedPendingAuthSession(): PendingAuthSessionSummary | null { ...@@ -35,18 +39,20 @@ function getPersistedPendingAuthSession(): PendingAuthSessionSummary | null {
} }
try { try {
const parsed = JSON.parse(raw) as PendingAuthSessionSummary const parsed = JSON.parse(raw) as Partial<PendingAuthSessionSummary> | null
if (!parsed?.token || !parsed?.provider) { const provider = typeof parsed?.provider === 'string' ? parsed.provider.trim() : ''
if (!provider) {
localStorage.removeItem(PENDING_AUTH_SESSION_KEY)
return null return null
} }
return { return {
token: parsed.token, token: typeof parsed?.token === 'string' ? parsed.token : '',
token_field: parsed.token_field || 'pending_auth_token', token_field: normalizePendingAuthTokenField(parsed?.token_field),
provider: parsed.provider, provider,
redirect: parsed.redirect, redirect: typeof parsed?.redirect === 'string' ? parsed.redirect : undefined,
adoption_required: parsed.adoption_required, adoption_required: typeof parsed?.adoption_required === 'boolean' ? parsed.adoption_required : undefined,
suggested_display_name: parsed.suggested_display_name, suggested_display_name: typeof parsed?.suggested_display_name === 'string' ? parsed.suggested_display_name : undefined,
suggested_avatar_url: parsed.suggested_avatar_url suggested_avatar_url: typeof parsed?.suggested_avatar_url === 'string' ? parsed.suggested_avatar_url : undefined
} }
} catch { } catch {
localStorage.removeItem(PENDING_AUTH_SESSION_KEY) localStorage.removeItem(PENDING_AUTH_SESSION_KEY)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment