18 lines
519 B
TypeScript
18 lines
519 B
TypeScript
import { useEffect } from 'react'
|
|
import { useAuthStore } from '@/stores/authStore'
|
|
import { authApi } from '@/lib/api'
|
|
|
|
export function useAuth() {
|
|
const { token, user, isAuthenticated, login, logout, setUser } = useAuthStore()
|
|
|
|
useEffect(() => {
|
|
if (token && !user) {
|
|
authApi.me()
|
|
.then((data) => setUser(data.user as unknown as Parameters<typeof setUser>[0]))
|
|
.catch(() => logout())
|
|
}
|
|
}, [token, user, setUser, logout])
|
|
|
|
return { token, user, isAuthenticated, login, logout }
|
|
}
|