mirror of
https://github.com/iflytek/skillhub.git
synced 2026-10-09 03:17:52 +00:00
Sync header auth state after login
This commit is contained in:
parent
c2b75bf961
commit
de5956476b
2 changed files with 17 additions and 4 deletions
|
|
@ -1,15 +1,25 @@
|
|||
import { useMutation } from '@tanstack/react-query'
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { authApi } from '@/api/client'
|
||||
import type { LocalLoginRequest, LocalRegisterRequest } from '@/api/types'
|
||||
import type { LocalLoginRequest, LocalRegisterRequest, User } from '@/api/types'
|
||||
|
||||
export function useLocalLogin() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (request: LocalLoginRequest) => authApi.localLogin(request),
|
||||
onSuccess: (user) => {
|
||||
queryClient.setQueryData<User | null>(['auth', 'me'], user)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useLocalRegister() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (request: LocalRegisterRequest) => authApi.localRegister(request),
|
||||
onSuccess: (user) => {
|
||||
queryClient.setQueryData<User | null>(['auth', 'me'], user)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import { useTranslation } from 'react-i18next'
|
||||
import { Link, useNavigate } from '@tanstack/react-router'
|
||||
import { useQueryClient } from '@tanstack/react-query'
|
||||
import { authApi } from '@/api/client'
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
|
|
@ -21,6 +23,7 @@ interface UserMenuProps {
|
|||
export function UserMenu({ user }: UserMenuProps) {
|
||||
const { t } = useTranslation()
|
||||
const navigate = useNavigate()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
const hasRole = (role: string) => user.platformRoles?.includes(role) ?? false
|
||||
const isReviewer = hasRole('SKILL_ADMIN') || hasRole('NAMESPACE_ADMIN') || hasRole('SUPER_ADMIN')
|
||||
|
|
@ -30,9 +33,9 @@ export function UserMenu({ user }: UserMenuProps) {
|
|||
|
||||
const handleLogout = async () => {
|
||||
try {
|
||||
await fetch('/api/auth/logout', { method: 'POST' })
|
||||
await authApi.logout()
|
||||
queryClient.setQueryData(['auth', 'me'], null)
|
||||
navigate({ to: '/' })
|
||||
window.location.reload()
|
||||
} catch (error) {
|
||||
console.error('Logout failed:', error)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue