-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
30 lines (24 loc) · 786 Bytes
/
middleware.ts
File metadata and controls
30 lines (24 loc) · 786 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
30
import { NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
export async function middleware(req:any) {
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
const isAuth = !!token;
const isLoginPage = req.nextUrl.pathname === "/signin";
// Not authenticated → redirect to login
if (!isAuth && !isLoginPage) {
return NextResponse.redirect(new URL("/signin", req.url));
}
// Already authenticated → avoid login/register pages
if (isAuth && isLoginPage) {
return NextResponse.redirect(new URL("/home", req.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
"/home/:path*", // protect dashboard routes
"/problems/:path*",
"/contest/:path*",
"/settings"
],
};