Add files via upload
This commit is contained in:
commit
675493bafc
28 changed files with 2176 additions and 0 deletions
27
hooks/useAuth.ts
Normal file
27
hooks/useAuth.ts
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import { useEffect } from 'react';
|
||||
import { getAccessToken, getMe, clearTokens } from '../services/api';
|
||||
import { useAuth } from '../store';
|
||||
|
||||
export function useBootAuth() {
|
||||
const { setUser, setReady } = useAuth();
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
try {
|
||||
const token = await getAccessToken();
|
||||
if (!token) { setReady(true); return; }
|
||||
const me = await getMe();
|
||||
setUser(me);
|
||||
} catch {
|
||||
// ignore
|
||||
} finally {
|
||||
setReady(true);
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
await clearTokens();
|
||||
useAuth.getState().setUser(null);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue