Refactor login and auth
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user