Basic auth working
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
} from 'vue-router';
|
||||
|
||||
import routes from './routes';
|
||||
import { account } from 'boot/appwrite';
|
||||
|
||||
/*
|
||||
* If not building with SSR mode, you can
|
||||
@@ -20,7 +21,9 @@ import routes from './routes';
|
||||
export default route(function (/* { store, ssrContext } */) {
|
||||
const createHistory = process.env.SERVER
|
||||
? createMemoryHistory
|
||||
: (process.env.VUE_ROUTER_MODE === 'history' ? createWebHistory : createWebHashHistory);
|
||||
: process.env.VUE_ROUTER_MODE === 'history'
|
||||
? createWebHistory
|
||||
: createWebHashHistory;
|
||||
|
||||
const Router = createRouter({
|
||||
scrollBehavior: () => ({ left: 0, top: 0 }),
|
||||
@@ -32,5 +35,25 @@ 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();
|
||||
});
|
||||
|
||||
return Router;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user