-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.tsx
37 lines (31 loc) · 908 Bytes
/
middleware.tsx
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
import { NextRequest, NextResponse } from "next/server";
import { jwtVerify } from "jose";
export default async function Middleware(req: NextRequest) {
const jwtCookie = req.cookies.get("token");
const jwt = jwtCookie && jwtCookie.value;
if (!jwt) {
return NextResponse.redirect(new URL("/login", req.url));
}
if (req.url.includes("login")) {
if (jwt) {
return NextResponse.redirect(new URL("/", req.url))
}
return NextResponse.next()
}
try {
await jwtVerify(jwt, new TextEncoder().encode("s3Cr3t"));
return NextResponse.next();
} catch (error) {
return NextResponse.redirect(new URL("/login", req.url));
}
}
export const config = {
matcher: [
"/",
'/products',
'/onboarding/:path*',
'/profile/:path*',
'/cart/:path*',
'/receipt/:path*',
],
}