-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmiddleware.ts
More file actions
38 lines (32 loc) · 1 KB
/
middleware.ts
File metadata and controls
38 lines (32 loc) · 1 KB
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
import { NextRequest, NextResponse } from "next/server";
import { AUTH_COOKIE_NAME } from "./src/lib/auth";
function isAuthenticated(request: NextRequest): boolean {
return request.cookies.has(AUTH_COOKIE_NAME);
}
export function middleware(request: NextRequest) {
const { pathname, search } = request.nextUrl;
const authenticated = isAuthenticated(request);
if (pathname === "/login" && authenticated) {
const problemsUrl = new URL("/problems", request.url);
return NextResponse.redirect(problemsUrl);
}
if (!authenticated && pathname !== "/login") {
const loginUrl = new URL("/login", request.url);
const redirectPath = `${pathname}${search}`;
loginUrl.searchParams.set("redirect", redirectPath);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
matcher: [
"/",
"/login",
"/problems/:path*",
"/competitions/:path*",
"/learn/:path*",
"/tracks/:path*",
"/progress/:path*",
"/profile/:path*",
],
};