-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
52 lines (45 loc) · 1.51 KB
/
middleware.ts
File metadata and controls
52 lines (45 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { auth } from '@/lib/auth'
/**
* Auth.js v5 Middleware
*
* Handles:
* - Protected route authentication (/dashboard/*)
* - Redirects authenticated users away from auth pages
* - Session refresh on each request
*/
export default auth(req => {
const { pathname } = req.nextUrl
const isAuthenticated = !!req.auth
// Protected routes - redirect to sign-in if not authenticated
const protectedRoutes = ['/dashboard']
const isProtectedRoute = protectedRoutes.some(route =>
pathname.startsWith(route)
)
if (isProtectedRoute && !isAuthenticated) {
const signInUrl = new URL('/sign-in', req.url)
signInUrl.searchParams.set('callbackUrl', pathname)
return Response.redirect(signInUrl)
}
// Auth pages - redirect to dashboard if already authenticated
const authPages = ['/sign-in', '/sign-up', '/verify-email']
const isAuthPage = authPages.some(page => pathname === page)
if (isAuthPage && isAuthenticated) {
return Response.redirect(new URL('/dashboard', req.url))
}
// Continue with request
return
})
export const config = {
// Match all routes except static files and API routes that don't need auth
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder files (images, etc.)
* - API routes for auth (handled by Auth.js)
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
}