diff --git a/code/websites/pokedex.online/src/router/guards.js b/code/websites/pokedex.online/src/router/guards.js new file mode 100644 index 0000000..88435d1 --- /dev/null +++ b/code/websites/pokedex.online/src/router/guards.js @@ -0,0 +1,63 @@ +/** + * Route Guards + * + * Navigation guards for protecting admin routes + */ + +import { useAuth } from '../composables/useAuth.js'; + +/** + * Create router guards with auth check + * @param {Router} router - Vue Router instance + */ +export function setupAuthGuards(router) { + router.beforeEach(async (to, from, next) => { + const { isAuthenticated, initializeAuth } = useAuth(); + + // Initialize auth from stored token + if (!isAuthenticated.value) { + await initializeAuth(); + } + + // Check if route requires admin access + if (to.meta.requiresAdmin) { + if (!isAuthenticated.value) { + // Redirect to login + next({ + name: 'admin-login', + query: { redirect: to.fullPath } + }); + } else { + next(); + } + } else { + next(); + } + }); + + router.afterEach((to, from) => { + // Optional: Log navigation for debugging + if (to.meta.requiresAdmin) { + console.log(`[Auth] Navigated to protected route: ${to.path}`); + } + }); +} + +/** + * Check if a route requires authentication + * @param {RouteLocationNormalized} route - Route object + * @returns {boolean} True if route requires authentication + */ +export function requiresAuthentication(route) { + return route.meta.requiresAdmin === true; +} + +/** + * Get redirect path after login + * @param {Router} router - Vue Router instance + * @returns {string} Path to redirect to + */ +export function getPostLoginRedirect(router) { + const redirect = router.currentRoute.value.query.redirect; + return redirect || '/'; +} diff --git a/code/websites/pokedex.online/src/views/AdminLogin.vue b/code/websites/pokedex.online/src/views/AdminLogin.vue index a0e7918..62358de 100644 --- a/code/websites/pokedex.online/src/views/AdminLogin.vue +++ b/code/websites/pokedex.online/src/views/AdminLogin.vue @@ -59,24 +59,24 @@

🔐 Protected Access

- Admin login provides access to protected features like the Gamemaster Manager - and other administration tools. + Admin login provides access to protected features like the + Gamemaster Manager and other administration tools.

🔒 Security

- Your session is protected with JWT authentication. Tokens expire after 7 days - of inactivity. + Your session is protected with JWT authentication. Tokens expire + after 7 days of inactivity.

📱 Device Specific

- Your login is stored securely in your browser's local storage. Each device - requires its own login. + Your login is stored securely in your browser's local storage. Each + device requires its own login.