mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-19 14:16:29 +00:00
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:
@@ -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()
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
})
|
||||
@@ -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: '/',
|
||||
|
||||
Reference in New Issue
Block a user