mirror of
https://github.com/umami-software/umami.git
synced 2026-02-09 07:07:17 +01:00
Support basePath in all queries.
This commit is contained in:
parent
2508198a51
commit
5068ab12a9
11 changed files with 57 additions and 29 deletions
|
|
@ -1,6 +1,8 @@
|
|||
import { useState, useEffect } from 'react';
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { updateUser } from 'redux/actions/user';
|
||||
import { useRouter } from 'next/router';
|
||||
import { get } from '../lib/web';
|
||||
|
||||
export async function fetchUser() {
|
||||
const res = await fetch('/api/auth/verify');
|
||||
|
|
@ -13,29 +15,33 @@ export async function fetchUser() {
|
|||
}
|
||||
|
||||
export default function useRequireLogin() {
|
||||
const router = useRouter();
|
||||
const dispatch = useDispatch();
|
||||
const storeUser = useSelector(state => state.user);
|
||||
const [loading, setLoading] = useState(!storeUser);
|
||||
const [user, setUser] = useState(storeUser);
|
||||
|
||||
async function loadUser() {
|
||||
setLoading(true);
|
||||
|
||||
const { ok, data } = await get(`${router.basePath}/api/auth/verify`);
|
||||
|
||||
if (!ok) {
|
||||
return router.push('/login');
|
||||
}
|
||||
|
||||
await dispatch(updateUser(data));
|
||||
|
||||
setUser(user);
|
||||
setLoading(false);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!loading && user) {
|
||||
return;
|
||||
}
|
||||
|
||||
setLoading(true);
|
||||
|
||||
fetchUser().then(async user => {
|
||||
if (!user) {
|
||||
window.location.href = '/login';
|
||||
return;
|
||||
}
|
||||
|
||||
await dispatch(updateUser(user));
|
||||
|
||||
setUser(user);
|
||||
setLoading(false);
|
||||
});
|
||||
loadUser();
|
||||
}, []);
|
||||
|
||||
return { user, loading };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue