SyntaxSquad/DADProject#14 added protection to logout inside a game and going to another page

This commit is contained in:
2025-12-29 11:15:33 +00:00
committed by FernandoJVideira
parent e68f1357cc
commit 3ec6f8971c
6 changed files with 226 additions and 32 deletions
+7 -7
View File
@@ -15,10 +15,12 @@ const router = createRouter({
routes: [
{
path: '/',
name: 'home',
component: HomePage,
},
{
path: '/login',
name: 'login',
component: LoginPage,
},
{
@@ -68,15 +70,13 @@ const router = createRouter({
})
router.beforeEach((to, from, next) => {
if (to.meta.requiresAuth) {
const token = localStorage.getItem('token') // Ou useAuthStore().token
const requiresAuth = to.matched.some((record) => record.meta.requiresAuth)
if (requiresAuth) {
const token = localStorage.getItem('token')
if (!token) {
toast.error('Acesso Negado', {
description: 'Precisas de fazer login para aceder ao jogo.',
duration: 4000,
})
return next({ name: 'home' })
return next({ name: 'login' })
}
}
next()