-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
71 lines (46 loc) · 1.97 KB
/
middleware.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// export const config = {
// /*
// * Match all request paths except for the ones starting with:
// * - api (API routes)
// * - _next/static (static files)
// * - _next/image (image optimization files)
// * - favicon.ico, sitemap.xml, robots.txt (metadata files)
// */
// matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
// }
import { auth } from "@/auth";
import { NextRequest, NextResponse } from 'next/server';
//all routes are protected by default
export function middleware(req: NextRequest) {
const cookies = req.cookies
console.log('middleware is running');
const { nextUrl } = req
let isLoggedIn
if (process.env.NODE_ENV === 'development') {
isLoggedIn = !!cookies.get('authjs.session-token')
} else if (process.env.NODE_ENV === 'production') {
isLoggedIn = !!cookies.get('__Secure-authjs.session-token')
}
// Public routes that don't require authentication
const publicRoutes = ['/auth/error', '/auth/verify', '/', '/configure/*'];
const authRoutes = ['/auth/signin']
// Protected routes that require authentication
// const protectedRoutes = [];
// Redirect to dashboard if authenticated user tries to access auth routes public routes
if (isLoggedIn && authRoutes.some(route => nextUrl.pathname.startsWith(route))) {
return NextResponse.redirect(new URL('/', req.url));
}
// Allow access to public routes even without a token
if (!isLoggedIn && publicRoutes.some(route => nextUrl.pathname.startsWith(route))) {
console.log('public');
return NextResponse.next();
}
// Redirect to signin if unauthenticated user tries to access protected routes
// if (!isLoggedIn && protectedRoutes.some(route => nextUrl.pathname.startsWith(route))) {
// return NextResponse.redirect(new URL('/auth/signin', req.url));
// }
return NextResponse.next();
}
export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
}