Implement session management (Task 5)
- Configure JWT session strategy with 30-day expiration - Implement session validation middleware for route protection - Create logout functionality with session termination - Handle session expiration automatically via JWT - Add session helper functions (getCurrentSession, isAuthenticated) - Protect routes: /, /list/*, /profile/*, /invitations/* - Redirect unauthenticated users to login with callback URL - Requirements: 2.4, 2.5, 3.1, 3.2, 3.4
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
import { prisma } from './db'
|
||||
import bcrypt from 'bcryptjs'
|
||||
import { z } from 'zod'
|
||||
import { signIn } from '@/auth'
|
||||
import { signIn, signOut } from '@/auth'
|
||||
|
||||
// Validation schemas
|
||||
const registerSchema = z.object({
|
||||
@@ -132,3 +132,44 @@ export async function loginUser(
|
||||
return { success: false, error: 'Invalid email or password' }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout user and terminate session
|
||||
* Requirement: 2.5 - Logout terminates session and clears authentication tokens
|
||||
*/
|
||||
export async function logoutUser(): Promise<void> {
|
||||
try {
|
||||
// Sign out using NextAuth - this will:
|
||||
// 1. Clear the session token
|
||||
// 2. Remove authentication cookies
|
||||
// 3. Terminate the session
|
||||
await signOut({ redirect: false })
|
||||
} catch (error) {
|
||||
console.error('Logout error:', error)
|
||||
throw new Error('Logout failed')
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current session
|
||||
* Requirements: 3.2 - Session validation and persistence
|
||||
*/
|
||||
export async function getCurrentSession() {
|
||||
try {
|
||||
const { auth } = await import('@/auth')
|
||||
const session = await auth()
|
||||
return session
|
||||
} catch (error) {
|
||||
console.error('Session retrieval error:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate if user is authenticated
|
||||
* Requirements: 2.4, 3.3 - Session expiration and validation
|
||||
*/
|
||||
export async function isAuthenticated(): Promise<boolean> {
|
||||
const session = await getCurrentSession()
|
||||
return !!session?.user
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user