strat-gameplay-webapp/frontend-sba/middleware/auth.ts
Cal Corum 891fb03c52 CLAUDE: Frontend enhancements for auth and game display
Auth Improvements:
- auth.ts: Enhanced authentication store with better error handling
- auth.ts middleware: Improved redirect logic
- login.vue: Better login flow and error display

Game Display:
- PlayByPlay.vue: Enhanced play-by-play feed with player name display
- game.ts types: Additional type definitions for runner advancement

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-28 12:09:39 -06:00

45 lines
1.2 KiB
TypeScript

/**
* Auth Middleware
*
* Protects routes that require authentication.
* Redirects to login if user is not authenticated.
*
* Works on both server and client:
* - Server: Forwards cookies from incoming request to /api/auth/me
* - Client: Browser sends cookies automatically
*/
import { useAuthStore } from '~/store/auth'
export default defineNuxtRouteMiddleware(async (to, from) => {
const authStore = useAuthStore()
// If already authenticated (from previous navigation), allow access
if (authStore.isAuthenticated) {
console.log('[Auth Middleware] Already authenticated, allowing access to:', to.path)
return
}
// Check auth status by calling backend
console.log('[Auth Middleware] Checking auth for:', to.path)
const isAuthed = await authStore.checkAuth()
console.log('[Auth Middleware]', {
path: to.path,
isAuthenticated: isAuthed,
hasUser: !!authStore.currentUser,
})
// Allow access if authenticated
if (isAuthed) {
return
}
// Redirect to login with return URL
console.log('[Auth Middleware] Not authenticated, redirecting to login')
return navigateTo({
path: '/auth/login',
query: { redirect: to.fullPath },
})
})