front > login : redirect to home page if user is already logged in
This commit is contained in:
parent
4113c69ccf
commit
144196793f
1 changed files with 12 additions and 11 deletions
|
|
@ -11,21 +11,22 @@ const router = createRouter({
|
||||||
});
|
});
|
||||||
|
|
||||||
router.beforeEach(async (to, from, next) => {
|
router.beforeEach(async (to, from, next) => {
|
||||||
const pinia = getActivePinia();
|
const pageStore = usePageStore();
|
||||||
const pageStore = usePageStore(pinia);
|
const userStore = useUserStore();
|
||||||
const userStore = useUserStore(pinia);
|
|
||||||
|
|
||||||
if (to.path === '/login') next();
|
const api = useApiStore();
|
||||||
|
|
||||||
const api = useApiStore(pinia);
|
|
||||||
try {
|
try {
|
||||||
const res = await api.fetchData(to.path);
|
const res = await api.fetchData(to.path);
|
||||||
|
|
||||||
if (!res.user) next('/login');
|
if (!res.user && to.path === '/login' && from.path === '/login') {
|
||||||
|
next();
|
||||||
pageStore.page = res.page;
|
} else if (res.user && to.path === '/login') {
|
||||||
userStore.user = res.user;
|
next('/');
|
||||||
next();
|
} else {
|
||||||
|
pageStore.page = res.page;
|
||||||
|
userStore.user = res.user;
|
||||||
|
next();
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
next(false);
|
next(false);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue