mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
feat: add login in frontend
* extract icon library from main.js * extract router from main.js * failure with error in passport * add example to swagger
This commit is contained in:
1 parent
b55b7aa1ec
commit
49417c405a
11 files changed
+206
-61
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import GamePage from './views/GamePage.vue'
|
||||
import AuthPage from './views/AuthPage.vue'
|
||||
import { isAuthenticated } from './utils/auth'
|
||||
|
||||
const routes = [
|
||||
{
|
||||
path: '/',
|
||||
name: 'auth',
|
||||
component: AuthPage,
|
||||
meta: { requiresGuest: true },
|
||||
},
|
||||
{
|
||||
path: '/game',
|
||||
name: 'game',
|
||||
component: GamePage,
|
||||
meta: { requiresAuth: true },
|
||||
},
|
||||
]
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(),
|
||||
routes,
|
||||
})
|
||||
|
||||
router.beforeEach((to, from, next) => {
|
||||
console.log(to, from)
|
||||
const isAuthed = isAuthenticated.value
|
||||
// Check if the route requires authentication
|
||||
if (to.meta.requiresAuth && !isAuthed) {
|
||||
// Check if the user isn't authenticated
|
||||
// Redirect to the login page
|
||||
next({
|
||||
name: 'auth',
|
||||
query: { redirect: to.fullPath }, // Optionally pass a redirect parameter
|
||||
})
|
||||
} else if (to.meta.requiresGuest && isAuthed) {
|
||||
next({
|
||||
name: 'game', // Or any default authenticated user route
|
||||
})
|
||||
} else {
|
||||
next() // Always call next() at the end
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
Reference in new issue
Block a user