-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
29 lines (21 loc) · 816 Bytes
/
middleware.js
File metadata and controls
29 lines (21 loc) · 816 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 { NextResponse } from 'next/server';
import { jwtVerify } from 'jose';
// 🧩 What is jose?
// jose is a modern JavaScript library for working with JWTs (JSON Web Tokens), JWKs (JSON Web Keys), and cryptographic operations — it’s essentially a replacement for older libraries like jsonwebtoken.
export async function middleware(req) {
const token = req.cookies.get('token')?.value;
if (!token) {
return NextResponse.redirect(new URL('/login', req.url));
}
try {
const secret = new TextEncoder().encode(process.env.JWT_SECRET);
await jwtVerify(token, secret);
return NextResponse.next();
} catch (err) {
console.error('JWT verification failed:', err);
return NextResponse.redirect(new URL('/login', req.url));
}
}
export const config = {
matcher: ['/'],
};