mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-19 14:16:29 +00:00
38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import { hc } from 'hono/client'
|
|
import type { AppType } from '@shumai/api'
|
|
import { useAuthStore } from '@/ui/stores/auth'
|
|
import { toast } from 'sonner'
|
|
|
|
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.startsWith('/share/') &&
|
|
!url.includes('/api/authn')
|
|
) {
|
|
useAuthStore.getState().clearAuth()
|
|
window.location.href = '/login'
|
|
}
|
|
}
|
|
|
|
if (response.status === 403) {
|
|
toast.error('You do not have permission. Please contact the team admin.', {
|
|
id: 'forbidden-error',
|
|
})
|
|
}
|
|
|
|
return response
|
|
}
|
|
|
|
export const client = hc<AppType>('/', {
|
|
fetch: customFetch,
|
|
init: {
|
|
credentials: 'include',
|
|
},
|
|
})
|