mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
feat: add loggedIn checks for frontend
This commit is contained in:
1 parent
34c277c886
commit
d939b7eb4d
5 files changed
+37
-21
No files matched your search
@@ -31,7 +31,7 @@ const handleLogin = async () => {
|
|||||||
async function check() {
|
async function check() {
|
||||||
const user = username.value.trim()
|
const user = username.value.trim()
|
||||||
try {
|
try {
|
||||||
const res = await api.get('/auth/check', {
|
const res = await api.get('/auth/check-username', {
|
||||||
params: { username: user },
|
params: { username: user },
|
||||||
})
|
})
|
||||||
// user not exists, try register
|
// user not exists, try register
|
||||||
|
|||||||
+20
-19
@@ -1,6 +1,7 @@
|
|||||||
import { createRouter, createWebHistory } from 'vue-router'
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
import GamePage from './views/GamePage.vue'
|
import GamePage from './views/GamePage.vue'
|
||||||
import AuthPage from './views/AuthPage.vue'
|
import AuthPage from './views/AuthPage.vue'
|
||||||
|
import { isLoggedIn } from './utils/auth'
|
||||||
|
|
||||||
const routes = [
|
const routes = [
|
||||||
{
|
{
|
||||||
@@ -26,24 +27,24 @@ const router = createRouter({
|
|||||||
routes,
|
routes,
|
||||||
})
|
})
|
||||||
|
|
||||||
// router.beforeEach((to, from, next) => {
|
router.beforeEach(async (to, from, next) => {
|
||||||
// console.log(to, from)
|
console.log(to, from)
|
||||||
// const isAuthed = isAuthenticated.value
|
const loggedIn = await isLoggedIn()
|
||||||
// // Check if the route requires authentication
|
// Check if the route requires authentication
|
||||||
// if (to.meta.requiresAuth && !isAuthed) {
|
if (to.meta.requiresAuth && !loggedIn) {
|
||||||
// // Check if the user isn't authenticated
|
// Check if the user isn't authenticated
|
||||||
// // Redirect to the login page
|
// Redirect to the login page
|
||||||
// next({
|
next({
|
||||||
// name: 'auth',
|
name: 'auth',
|
||||||
// query: { redirect: to.fullPath }, // Optionally pass a redirect parameter
|
query: { redirect: to.fullPath }, // Optionally pass a redirect parameter
|
||||||
// })
|
})
|
||||||
// } else if (to.meta.requiresGuest && isAuthed) {
|
} else if (to.meta.requiresGuest && loggedIn) {
|
||||||
// next({
|
next({
|
||||||
// name: 'game', // Or any default authenticated user route
|
name: 'game', // Or any default authenticated user route
|
||||||
// })
|
})
|
||||||
// } else {
|
} else {
|
||||||
// next() // Always call next() at the end
|
next() // Always call next() at the end
|
||||||
// }
|
}
|
||||||
// })
|
})
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
@@ -1,5 +1,11 @@
|
|||||||
import api from './api'
|
import api from './api'
|
||||||
|
|
||||||
|
export async function isLoggedIn() {
|
||||||
|
const res = await api.get('/auth/check-session')
|
||||||
|
const { loggedIn } = res.data
|
||||||
|
return loggedIn
|
||||||
|
}
|
||||||
|
|
||||||
export const login = async (username, password) => {
|
export const login = async (username, password) => {
|
||||||
try {
|
try {
|
||||||
const res = await api.post('/auth/login', { username, password })
|
const res = await api.post('/auth/login', { username, password })
|
||||||
|
|||||||
@@ -121,3 +121,9 @@ export function logout(req, res) {
|
|||||||
res.redirect('/login')
|
res.redirect('/login')
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function checkSession(req, res) {
|
||||||
|
res.json({
|
||||||
|
loggedIn: req.isAuthenticated(),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import passport from 'passport'
|
|||||||
import {
|
import {
|
||||||
register,
|
register,
|
||||||
checkUsername,
|
checkUsername,
|
||||||
|
checkSession,
|
||||||
logout,
|
logout,
|
||||||
} from '../controllers/authController.js'
|
} from '../controllers/authController.js'
|
||||||
|
|
||||||
@@ -21,6 +22,8 @@ router.post('/register', register)
|
|||||||
|
|
||||||
router.post('/logout', logout)
|
router.post('/logout', logout)
|
||||||
|
|
||||||
router.get('/check', checkUsername)
|
router.get('/check-username', checkUsername)
|
||||||
|
|
||||||
|
router.get('/check-session', checkSession)
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
Reference in new issue
Block a user