mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 07:07:17 +01:00
Updated login check method.
This commit is contained in:
parent
170b30b391
commit
796c65fa29
10 changed files with 45 additions and 40 deletions
28
hooks/useRequireLogin.js
Normal file
28
hooks/useRequireLogin.js
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import { useEffect } from 'react';
|
||||
import { useRouter } from 'next/router';
|
||||
import useApi from 'hooks/useApi';
|
||||
import useUser from 'hooks/useUser';
|
||||
|
||||
export default function useRequireLogin() {
|
||||
const router = useRouter();
|
||||
const { get } = useApi();
|
||||
const { user, setUser } = useUser();
|
||||
|
||||
useEffect(() => {
|
||||
async function loadUser() {
|
||||
try {
|
||||
const { user } = await get('/auth/verify');
|
||||
|
||||
setUser(user);
|
||||
} catch {
|
||||
await router.push('/login');
|
||||
}
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
loadUser();
|
||||
}
|
||||
}, [user]);
|
||||
|
||||
return { user };
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue