mirror of
https://github.com/kevin-DL/wails-vue-template.git
synced 2026-01-19 04:45:23 +00:00
Added basic auth and route protection
This commit is contained in:
@@ -1,22 +1,62 @@
|
||||
import { createApp } from 'vue'
|
||||
import { createWebHashHistory, createRouter } from 'vue-router'
|
||||
import { IsLoggedIn } from '../wailsjs/go/main/Auth'
|
||||
import App from './App.vue'
|
||||
import './style.css';
|
||||
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
name: 'home',
|
||||
component: () => import('./pages/Index.vue')
|
||||
}
|
||||
{
|
||||
path: '/',
|
||||
name: 'home',
|
||||
component: () => import('./pages/Index.vue'),
|
||||
meta: {
|
||||
requiresAuth: false,
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/protected',
|
||||
name: 'protected',
|
||||
component: () => import("./pages/Protected.vue"),
|
||||
meta: {
|
||||
requiresAuth: true,
|
||||
}
|
||||
},
|
||||
{
|
||||
path: '/login',
|
||||
name: 'login',
|
||||
component: () => import("./pages/Login.vue"),
|
||||
meta: {
|
||||
requiresAuth: "guest",
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHashHistory(),
|
||||
routes
|
||||
history: createWebHashHistory(),
|
||||
routes
|
||||
})
|
||||
|
||||
|
||||
router.beforeEach(async (to, from) => {
|
||||
// instead of having to check every route record with
|
||||
// to.matched.some(record => record.meta.requiresAuth)
|
||||
const loggedIn = await IsLoggedIn()
|
||||
if (to.meta.requiresAuth === true && !loggedIn) {
|
||||
return {
|
||||
path: '/login',
|
||||
query: { redirect: to.fullPath },
|
||||
}
|
||||
} else {
|
||||
if (to.meta.requiresAuth === 'guest' && loggedIn) {
|
||||
return {
|
||||
path: '/protected',
|
||||
query: { redirect: to.fullPath },
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
createApp(App)
|
||||
.use(router)
|
||||
.mount('#app')
|
||||
.use(router)
|
||||
.mount('#app')
|
||||
|
||||
Reference in New Issue
Block a user