mirror of
https://github.com/iflytek/skillhub.git
synced 2026-10-09 03:17:52 +00:00
fix: refresh auth roles promptly
This commit is contained in:
parent
dee07ad7a2
commit
cb9c2f489b
2 changed files with 27 additions and 5 deletions
15
web/src/features/auth/use-auth.test.ts
Normal file
15
web/src/features/auth/use-auth.test.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
import { describe, expect, it } from 'vitest'
|
||||
import { getAuthQueryOptions } from './use-auth'
|
||||
|
||||
describe('getAuthQueryOptions', () => {
|
||||
it('keeps auth state fresh so role changes are picked up promptly', () => {
|
||||
const options = getAuthQueryOptions(true)
|
||||
|
||||
expect(options.queryKey).toEqual(['auth', 'me'])
|
||||
expect(options.staleTime).toBe(0)
|
||||
expect(options.refetchOnWindowFocus).toBe(true)
|
||||
expect(options.refetchOnReconnect).toBe(true)
|
||||
expect(options.refetchInterval).toBe(60_000)
|
||||
expect(options.enabled).toBe(true)
|
||||
})
|
||||
})
|
||||
|
|
@ -2,14 +2,21 @@ import { useQuery } from '@tanstack/react-query'
|
|||
import { authApi } from '@/api/client'
|
||||
import type { User } from '@/api/types'
|
||||
|
||||
export function useAuth(enabled = true) {
|
||||
const { data: user, isLoading, error } = useQuery<User | null>({
|
||||
queryKey: ['auth', 'me'],
|
||||
export function getAuthQueryOptions(enabled = true) {
|
||||
return {
|
||||
queryKey: ['auth', 'me'] as const,
|
||||
queryFn: authApi.getMe,
|
||||
retry: false,
|
||||
enabled,
|
||||
staleTime: 5 * 60 * 1000, // 5 分钟
|
||||
})
|
||||
staleTime: 0,
|
||||
refetchOnWindowFocus: true,
|
||||
refetchOnReconnect: true,
|
||||
refetchInterval: 60_000,
|
||||
}
|
||||
}
|
||||
|
||||
export function useAuth(enabled = true) {
|
||||
const { data: user, isLoading, error } = useQuery<User | null>(getAuthQueryOptions(enabled))
|
||||
|
||||
return {
|
||||
user: user ?? null,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue