Refactor login and auth

This commit is contained in:
2023-11-13 21:37:53 -05:00
parent ab8e50991e
commit 5ec697b94a
5 changed files with 35 additions and 41 deletions

View File

@@ -7,7 +7,7 @@ import {
} from 'vue-router';
import routes from './routes';
import { account } from 'boot/appwrite';
import { useAuthStore } from 'src/stores/auth';
/*
* If not building with SSR mode, you can
@@ -35,24 +35,14 @@ export default route(function (/* { store, ssrContext } */) {
history: createHistory(process.env.VUE_ROUTER_BASE),
});
const accountRoutes = ['login', 'register'];
Router.beforeEach(async (to, from, next) => {
const name = to.name as string;
try {
const session = await account.getSession('current');
to.meta.session = session;
if (accountRoutes.includes(name)) {
return next({ name: 'index' });
}
} catch {
if (!accountRoutes.includes(name)) {
return next({ name: 'login' });
}
}
next();
const auth = useAuthStore();
console.log([to.meta.accountRoute, auth.currentUser]);
return !to.meta.accountRoute && !auth.currentUser
? next({ name: 'login' })
: to.meta.accountRoute && auth.currentUser
? next({ name: 'index' })
: next();
});
return Router;