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;
|
||||
});
|
||||
|
||||
@@ -4,12 +4,24 @@ const routes: RouteRecordRaw[] = [
|
||||
{
|
||||
path: '/',
|
||||
component: () => import('layouts/MainLayout.vue'),
|
||||
children: [{ path: '', component: () => import('pages/IndexPage.vue') }],
|
||||
name: 'index',
|
||||
children: [
|
||||
{
|
||||
path: '',
|
||||
component: () => import('pages/IndexPage.vue'),
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
path: '/Login',
|
||||
path: '/login',
|
||||
component: () => import('pages/LoginPage.vue'),
|
||||
name: 'login',
|
||||
},
|
||||
// {
|
||||
// path: '/register',
|
||||
// component: () => import('pages/RegisterPage.vue'),
|
||||
// name: 'register'
|
||||
// },
|
||||
// Always leave this as last one,
|
||||
// but you can also remove it
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user