mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 15:17:23 +01:00
Updated login check.
This commit is contained in:
parent
e074394b13
commit
c24a0c87dc
6 changed files with 36 additions and 33 deletions
|
|
@ -13,7 +13,7 @@ export * from './useMessages';
|
|||
export * from './useNavigation';
|
||||
export * from './useReport';
|
||||
export * from './useReports';
|
||||
export * from './useRequireLogin';
|
||||
export * from './useLogin';
|
||||
export * from './useShareToken';
|
||||
export * from './useSticky';
|
||||
export * from './useTheme';
|
||||
|
|
|
|||
22
src/components/hooks/useLogin.ts
Normal file
22
src/components/hooks/useLogin.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import useApi from 'components/hooks/useApi';
|
||||
import useUser from 'components/hooks/useUser';
|
||||
|
||||
export function useLogin() {
|
||||
const { get, useQuery } = useApi();
|
||||
const { user, setUser } = useUser();
|
||||
|
||||
const query = useQuery({
|
||||
queryKey: ['login'],
|
||||
queryFn: async () => {
|
||||
const data = await get('/auth/verify');
|
||||
|
||||
setUser(data);
|
||||
|
||||
return data;
|
||||
},
|
||||
});
|
||||
|
||||
return { user, ...query };
|
||||
}
|
||||
|
||||
export default useLogin;
|
||||
|
|
@ -1,28 +0,0 @@
|
|||
import { useEffect } from 'react';
|
||||
import useApi from 'components/hooks/useApi';
|
||||
import useUser from 'components/hooks/useUser';
|
||||
|
||||
export function useRequireLogin() {
|
||||
const { get } = useApi();
|
||||
const { user, setUser } = useUser();
|
||||
|
||||
useEffect(() => {
|
||||
async function loadUser() {
|
||||
try {
|
||||
const data = await get('/auth/verify');
|
||||
|
||||
setUser(data);
|
||||
} catch {
|
||||
window.location.href = `${process.env.basePath || ''}/login`;
|
||||
}
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
loadUser();
|
||||
}
|
||||
}, [user]);
|
||||
|
||||
return { user };
|
||||
}
|
||||
|
||||
export default useRequireLogin;
|
||||
Loading…
Add table
Add a link
Reference in a new issue