-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.tsx
More file actions
29 lines (23 loc) · 905 Bytes
/
middleware.tsx
File metadata and controls
29 lines (23 loc) · 905 Bytes
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
import { NextRequest, NextResponse } from "next/server";
const protectedRoutes = ["/dashbaoard"];
const publicRoutes = ["/sign-in", "/signup"];
import { getToken } from "next-auth/jwt";
export default async function middleware(req: NextRequest) {
const path = req.nextUrl.pathname;
const isProtectedRoute = protectedRoutes.includes(path);
const isPublicRoute = publicRoutes.includes(path);
// const token = req.cookies.get("next-auth.session-token")?.value;
const token = await getToken({
req,
secret: process.env.NEXTAUTH_SECRET ?? "",
});
// console.log("token.accessToken: ", token?.id);
// console.log("token ", token);
if (isProtectedRoute && !token?.id) {
return NextResponse.redirect(new URL("/sign-in", req.nextUrl));
}
if (isPublicRoute && token?.id) {
return NextResponse.redirect(new URL("/", req.nextUrl));
}
return NextResponse.next();
}