diff --git a/app/src/components/AuthenticationPart.vue b/app/src/components/AuthenticationPart.vue index cae5a94..431a1b0 100644 --- a/app/src/components/AuthenticationPart.vue +++ b/app/src/components/AuthenticationPart.vue @@ -31,7 +31,7 @@ const handleLogin = async () => { async function check() { const user = username.value.trim() try { - const res = await api.get('/auth/check', { + const res = await api.get('/auth/check-username', { params: { username: user }, }) // user not exists, try register diff --git a/app/src/router.js b/app/src/router.js index e2d79e0..86a7894 100644 --- a/app/src/router.js +++ b/app/src/router.js @@ -1,6 +1,7 @@ import { createRouter, createWebHistory } from 'vue-router' import GamePage from './views/GamePage.vue' import AuthPage from './views/AuthPage.vue' +import { isLoggedIn } from './utils/auth' const routes = [ { @@ -26,24 +27,24 @@ const router = createRouter({ 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 -// } -// }) +router.beforeEach(async (to, from, next) => { + console.log(to, from) + const loggedIn = await isLoggedIn() + // Check if the route requires authentication + if (to.meta.requiresAuth && !loggedIn) { + // 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 && loggedIn) { + next({ + name: 'game', // Or any default authenticated user route + }) + } else { + next() // Always call next() at the end + } +}) export default router diff --git a/app/src/utils/auth.js b/app/src/utils/auth.js index bdff578..deb5e9d 100644 --- a/app/src/utils/auth.js +++ b/app/src/utils/auth.js @@ -1,5 +1,11 @@ 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) => { try { const res = await api.post('/auth/login', { username, password }) diff --git a/server/src/api/controllers/authController.js b/server/src/api/controllers/authController.js index 7f11812..7968354 100644 --- a/server/src/api/controllers/authController.js +++ b/server/src/api/controllers/authController.js @@ -121,3 +121,9 @@ export function logout(req, res) { res.redirect('/login') }) } + +export function checkSession(req, res) { + res.json({ + loggedIn: req.isAuthenticated(), + }) +} diff --git a/server/src/api/routes/auth.js b/server/src/api/routes/auth.js index 36879ee..fb52e09 100644 --- a/server/src/api/routes/auth.js +++ b/server/src/api/routes/auth.js @@ -4,6 +4,7 @@ import passport from 'passport' import { register, checkUsername, + checkSession, logout, } from '../controllers/authController.js' @@ -21,6 +22,8 @@ router.post('/register', register) router.post('/logout', logout) -router.get('/check', checkUsername) +router.get('/check-username', checkUsername) + +router.get('/check-session', checkSession) export default router