Appearance
Authentication System ​
File Information ​
- Path:
app/stores/auth.ts,app/middleware/auth.global.ts,app/plugins/01.auth-init.ts - Purpose: Secure authentication system with HTTP-only cookie support
Overview ​
The Authentication System provides production-ready authentication with industry-best security practices. It implements HTTP-only cookie storage for JWT tokens, preventing XSS token theft, and includes automatic session management, token-based login, and SSR compatibility.
Architecture ​
System Components ​
- Auth Store: Centralized authentication state management (Pinia)
- Auth Middleware: Global route protection and authentication validation
- Auth Plugin: Early initialization and cookie hydration
- Server API Routes: Secure token management via HTTP-only cookies
- Redirect Middleware: Prevents authenticated users from accessing auth pages
Key Features ​
1. HTTP-Only Cookie Storage ​
- JWT tokens stored in HTTP-only cookies
- Tokens cannot be accessed via
document.cookie - XSS protection - tokens completely inaccessible to JavaScript
- Secure flag enabled in production (HTTPS only)
- SameSite=lax for CSRF protection
2. Server-Side Token Management ​
- All token operations handled by server API routes
- Token never exposed to client JavaScript
- Automatic token injection in API requests
- Token validation on each protected route access
3. Multiple Login Methods ​
- Email/Password Login: Standard authentication with workspace selection
- Token-Based Login: One-time token access for password-less login
- Password Recovery: Forgot password and reset password flows
- Password Generation: Generate password for new accounts
4. Automatic Session Management ​
- Auto token refresh on page refresh
- User data fetching when needed
- Session persistence across page navigations
- Automatic cleanup on logout
5. Protected Routes ​
- Automatic redirection for unauthenticated users
- Public routes support (shared assets, login pages)
- Token-based route access
- Workspace validation
File Structure ​
Core Files ​
Stores ​
app/stores/auth.ts- Authentication store- User state management
- Login/logout actions
- Token-based login
- Session management
- User data fetching
Middleware ​
app/middleware/auth.global.ts- Global authentication middleware- Runs on ALL pages
- Server-side cookie hydration
- Client-side authentication validation
- Protected route enforcement
- Automatic redirects
app/middleware/auth.ts- Per-route authentication middleware- Optional per-route authentication check
- Custom authentication logic
app/middleware/redirect-if-logged-in.ts- Redirect middleware- Prevents authenticated users from accessing auth pages
- Automatic redirect to dashboard
app/middleware/check-url.ts- URL validation middleware- Validates brand name in route
- Ensures brand exists in user's accessible workspaces
Plugins ​
app/plugins/01.auth-init.ts- Auth initialization plugin- Runs early in Nuxt lifecycle (prefix
01) - Initial auth state hydration from cookies
- Automatic user data fetching if token exists
- Cookie-store synchronization
- Runs early in Nuxt lifecycle (prefix
Server API Routes ​
server/api/auth/set-session.post.ts- Set HTTP-only auth cookieserver/api/auth/clear-session.post.ts- Clear HTTP-only auth cookie (logout)server/api/auth/session.get.ts- Get session status (no token exposed)server/api/auth/refresh-user.post.ts- Fetch user data using HTTP-only token
Composables ​
app/composables/api/useAuthApi.ts- Authentication API operations- Login/logout API calls
- Token-based login
- Password recovery
- User data fetching
app/composables/auth/useAuth.ts- Authentication utilities- Login/logout functionality
- Secure token storage
app/composables/auth/useCookieHelper.ts- Cookie helper utilities
Authentication Flow ​
Login Flow ​
- User submits credentials (email, password, workspaceId)
- Client calls
authStore.login(email, password, workspaceId) - Backend API validates credentials and returns JWT token
- Client calls
/api/auth/set-sessionwith token - Server stores token in HTTP-only cookie (not accessible to JS)
- User data stored in separate non-httpOnly cookie for UI
- Full user object stored in localStorage for complete data
- User data fetched via
authStore.getUser() - Redirect to dashboard
Token-Based Login Flow ​
- User navigates to
/{brand_name}/{token}route - Page component calls
authStore.loginWithToken(token, url) - Backend API validates token and returns JWT
- Token stored in HTTP-only cookie
- User data fetched and stored
- Redirect to dashboard
Logout Flow ​
- User clicks logout
- Client calls
authStore.logout() - Server API route
/api/auth/clear-sessioncalled - Server removes HTTP-only cookie
- Client clears localStorage
- Client state cleared
- Redirect to login page
Protected Route Flow ​
- User navigates to protected route
- Global auth middleware (
auth.global.ts) runs - Checks if route is public → Allow access
- Checks if user is authenticated → Allow access
- If not authenticated → Redirect to login
- If authenticated but no user data → Fetch user data
Cookie Configuration ​
typescript
// HTTP-Only Auth Token Cookie (set by server only)
{
httpOnly: true, // Cannot be accessed by JavaScript
secure: true (in production), // HTTPS only in production
sameSite: 'lax', // CSRF protection
path: '/', // Available site-wide
maxAge: 60 * 60 * 24 * 7 // 7 days
}
// User Data Cookie (accessible by client for UI)
{
httpOnly: false, // Client needs access for UI
secure: true (in production),
sameSite: 'lax',
path: '/',
maxAge: 60 * 60 * 24 * 7
}Hybrid Storage for Large Data ​
Cookies have a 4096 character limit. To preserve ALL user fields:
- Cookie: Stores minimal user data (essential auth info + current workspace instance only) for SSR
- localStorage: Stores the FULL user object (key:
auth_full_user) with ALL fields fromgetUser() - localStorage: Also stores
accessibleInstancesandsettingsfor backwards compatibility
Security Features ​
- HTTP-Only Cookies: Auth tokens stored securely
- XSS Protection: Tokens completely inaccessible to JavaScript
- Server-Side Token Management: All token operations on server
- HTTPS Only: Secure flag enabled in production
- CSRF Protection: SameSite=lax cookie attribute
- Auto Logout: 401 responses trigger automatic logout
- Token Validation: Tokens validated on each protected route access
- Secure WebSocket Auth: Pusher authentication proxied through server
Usage Examples ​
Login ​
typescript
const authStore = useAuthStore()
const result = await authStore.login(email, password, workspaceId)
if (result.success) {
await authStore.getUser()
await navigateTo(`/${brandName}`)
}Token-Based Login ​
typescript
const authStore = useAuthStore()
const result = await authStore.loginWithToken(token, hostname)
if (result.success && authStore.user) {
await navigateTo(`/${brandName}`)
}Check Authentication ​
typescript
const authStore = useAuthStore()
if (authStore.isAuthenticated) {
const user = authStore.user
// User is logged in
}Logout ​
typescript
const authStore = useAuthStore()
await authStore.logout() // Automatically redirects to loginRelated Documentation ​
- Auth Store - Authentication state management
- Global Auth Middleware - Global route protection
- Per-Route Auth Middleware - Per-route authentication
- Auth API - Authentication endpoints
- Login Page - Login page implementation