-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
39 lines (32 loc) · 1.35 KB
/
Copy pathmiddleware.ts
File metadata and controls
39 lines (32 loc) · 1.35 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
import { NextRequest, NextResponse } from "next/server";
import { decrypt } from "@/lib/auth";
// 1. Specify protected and public routes
const protectedRoutes = ["/dashboard"];
const publicRoutes = ["/login", "/signup", "/", "/contact", "/pricing"];
export default async function middleware(req: NextRequest) {
// 2. Check if the current route is protected or public
const path = req.nextUrl.pathname;
const isProtectedRoute = protectedRoutes.some((route) => path.startsWith(route));
const isPublicRoute = publicRoutes.includes(path);
// 3. Decrypt the session from the cookie
const cookie = req.cookies.get("session")?.value;
const session = cookie ? await decrypt(cookie).catch(() => null) : null;
// 4. Redirect to /login if the user is not authenticated on a protected route
if (isProtectedRoute && !session) {
return NextResponse.redirect(new URL("/login", req.nextUrl));
}
// 5. Redirect to /dashboard if the user is authenticated on a public auth route
if (
isPublicRoute &&
session &&
(path.startsWith("/login") || path.startsWith("/signup")) &&
!req.nextUrl.searchParams.has("logout")
) {
return NextResponse.redirect(new URL("/dashboard", req.nextUrl));
}
return NextResponse.next();
}
// Routes Middleware should not run on
export const config = {
matcher: ["/((?!api|_next/static|_next/image|.*\\.png$).*)"],
};