Files
Yiling-J 19eed521ae 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
2026-10-21 12:12:25 +08:00

33 lines
961 B
TypeScript

import { hc } from 'hono/client'
import type { AppType } from '@shumai/api'
import { useAuthStore } from '@/ui/stores/auth'
const customFetch = async (input: RequestInfo | URL, init?: RequestInit) => {
const response = await fetch(input, init)
if (response.status === 401) {
const url = input.toString()
// Don't redirect if we are already on login page, it's an auth request, or we are on a public share page
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()
window.location.href = '/login'
}
}
return response
}
export const client = hc<AppType>('/', {
fetch: customFetch,
init: {
credentials: 'include',
},
})