fix(webui): prevent unwanted redirects to login on reset-password page (#487)

* fix(webui): allow reset-password route with token when stale auth exists

* fix(webui): prevent 401 redirect to login on reset-password page
This commit is contained in:
Yiling-J
2026-10-21 12:12:25 +08:00
committed by GitHub
parent e05a23bcc2
commit 19eed521ae
4 changed files with 52 additions and 2 deletions
+2
View File
@@ -11,7 +11,9 @@ const customFetch = async (input: RequestInfo | URL, init?: RequestInit) => {
if (
!window.location.pathname.includes('/login') &&
!window.location.pathname.includes('/signup') &&
!window.location.pathname.includes('/reset-password') &&
!window.location.pathname.startsWith('/share/') &&
!url.includes('/api/auth') &&
!url.includes('/api/authn')
) {
useAuthStore.getState().clearAuth()
+1 -1
View File
@@ -71,7 +71,7 @@ function RootComponent() {
if (!res.ok) throw new Error('Failed to fetch me')
return await res.json()
},
enabled: !!storedTeamId,
enabled: !!storedTeamId && !!user,
})
if (!showSidebar) {
@@ -0,0 +1,39 @@
import { describe, it, expect, beforeEach } from 'vitest'
import { useAuthStore } from '@/ui/stores/auth'
import { Route } from './reset-password'
describe('Reset Password Route beforeLoad guard', () => {
beforeEach(() => {
useAuthStore.getState().clearAuth()
})
it('redirects to / when user is authenticated in store and no reset token is present', () => {
useAuthStore.getState().setUser({ id: 'user-1', email: 'test@example.com' })
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Route.options.beforeLoad?.({ search: { token: '' } } as any)
}).toThrow()
})
it('does NOT redirect and clears stale auth when a reset token is present in search params', () => {
useAuthStore.getState().setUser({ id: 'user-1', email: 'test@example.com' })
// When the user has stale auth in localStorage but provides a token,
// they should NOT be redirected to / (which would 401 and redirect to login)
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Route.options.beforeLoad?.({ search: { token: 'valid-token-123' } } as any)
}).not.toThrow()
// And the stale auth store should be cleared
expect(useAuthStore.getState().user).toBeNull()
})
it('does not redirect when user is not logged in and no token is present', () => {
expect(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Route.options.beforeLoad?.({ search: { token: '' } } as any)
}).not.toThrow()
})
})
+10 -1
View File
@@ -8,7 +8,16 @@ const searchSchema = z.object({
export const Route = createFileRoute('/reset-password')({
validateSearch: (search) => searchSchema.parse(search),
beforeLoad: () => {
beforeLoad: ({ search }) => {
// If a reset token is present in the URL, prioritize password recovery
// and clear any stale local auth state so the user can cleanly reset and log in
if (search.token) {
if (useAuthStore.getState().user) {
useAuthStore.getState().clearAuth()
}
return
}
if (useAuthStore.getState().user) {
throw redirect({
to: '/',