diff --git a/packages/webui/api/client.ts b/packages/webui/api/client.ts index 9e25ed0a..32c9bf5e 100644 --- a/packages/webui/api/client.ts +++ b/packages/webui/api/client.ts @@ -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() diff --git a/packages/webui/routes/__root.tsx b/packages/webui/routes/__root.tsx index a0216092..0088fd88 100644 --- a/packages/webui/routes/__root.tsx +++ b/packages/webui/routes/__root.tsx @@ -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) { diff --git a/packages/webui/routes/reset-password.test.ts b/packages/webui/routes/reset-password.test.ts new file mode 100644 index 00000000..885e07da --- /dev/null +++ b/packages/webui/routes/reset-password.test.ts @@ -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() + }) +}) diff --git a/packages/webui/routes/reset-password.tsx b/packages/webui/routes/reset-password.tsx index 4a5b23e3..4bd7a6c6 100644 --- a/packages/webui/routes/reset-password.tsx +++ b/packages/webui/routes/reset-password.tsx @@ -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: '/',