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) => {
|
||||
const pinia = getActivePinia();
|
||||
const pageStore = usePageStore(pinia);
|
||||
const userStore = useUserStore(pinia);
|
||||
const pageStore = usePageStore();
|
||||
const userStore = useUserStore();
|
||||
|
||||
if (to.path === '/login') next();
|
||||
|
||||
const api = useApiStore(pinia);
|
||||
const api = useApiStore();
|
||||
try {
|
||||
const res = await api.fetchData(to.path);
|
||||
|
||||
if (!res.user) next('/login');
|
||||
|
||||
if (!res.user && to.path === '/login' && from.path === '/login') {
|
||||
next();
|
||||
} else if (res.user && to.path === '/login') {
|
||||
next('/');
|
||||
} else {
|
||||
pageStore.page = res.page;
|
||||
userStore.user = res.user;
|
||||
next();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
next(false);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue