-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
23 lines (19 loc) · 802 Bytes
/
middleware.ts
File metadata and controls
23 lines (19 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { getToken } from "next-auth/jwt";
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
const authenticatedRoutes = [ "/", "/game", "/hall-of-wins" ]
export async function middleware(request: NextRequest) {
const token = await getToken({
req: request,
secret: process.env.NEXTAUTH_SECRET
})
const pathName = request.nextUrl.pathname;
if (!token) {
if (authenticatedRoutes.some(route => pathName == route)) {
const callbackUrl = encodeURIComponent(`${request.nextUrl.origin}${pathName}`);
const absoluteRedirectUrl = `${request.nextUrl.origin}/login?callbackUrl=${callbackUrl}`;
return NextResponse.redirect(absoluteRedirectUrl);
}
}
return NextResponse.next();
}