-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
55 lines (42 loc) · 1.38 KB
/
middleware.js
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
import { NextResponse } from "next/server";
async function verifyToken(token) {
const secret = await crypto.subtle.importKey(
"raw",
new TextEncoder().encode(process.env.JWT_SECRET),
{
name: "HMAC",
hash: { name: "SHA-256" },
},
false,
["verify"]
);
const verified = await crypto.subtle.verify(
"HMAC",
secret,
Buffer.from(token.split(".")[2], "base64url"),
new TextEncoder().encode(token.split(".").splice(0, 2).join("."))
);
return verified;
}
export async function middleware(req) {
const { pathname } = req.nextUrl;
// If the user is already logged in, redirect to /admin
if (pathname === '/login') {
const token = req.cookies.get('token')?.value;
if (!token) return NextResponse.next();
const verified = await verifyToken(token);
if (verified) {
return NextResponse.redirect(new URL('/admin', req.url));
}
}
// Allow access to /login and /admin with a valid token, other than /api/login
if (pathname.startsWith('/admin') || pathname.startsWith('/api') && !pathname.startsWith('/api/login')) {
const token = req.cookies.get('token')?.value;
if (!token) return NextResponse.redirect(new URL('/login', req.url));
const verified = await verifyToken(token);
if (!verified) {
return NextResponse.redirect(new URL('/login', req.url));
}
}
return NextResponse.next();
}