-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
27 lines (20 loc) · 755 Bytes
/
middleware.ts
File metadata and controls
27 lines (20 loc) · 755 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
import { NextResponse } from 'next/server';
import { jwtVerify } from 'jose';
import { cookies } from 'next/headers';
const secret = new TextEncoder().encode(process.env.ACCESS_TOKEN);
export async function middleware(request: Request) {
const cookieStore = await cookies();
const accessToken = cookieStore.get('accessToken')?.value;
if (!accessToken) {
return NextResponse.redirect(new URL('/login', request.url));
}
try {
await jwtVerify(accessToken, secret);
return NextResponse.next();
} catch (err: unknown) {
return NextResponse.redirect(new URL('/login', request.url));
}
}
export const config = {
matcher: ['/create-post/:path*','/dashboard/:path*','/subscribers/:path*'],
};