-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.ts
37 lines (35 loc) · 1.11 KB
/
middleware.ts
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
import { cookies } from "next/headers";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const privatePaths = [
"/my-fitness-journey",
"/settings/preferences",
"/email-and-password",
"/subscription",
];
const authPaths = ["/login", "/signup"];
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const sessionToken = cookies().get("sessionToken")?.value;
if (
privatePaths.some((path) => pathname.startsWith(path)) &&
!sessionToken
) {
return NextResponse.redirect(new URL("/login", request.url));
}
if (authPaths.some((path) => pathname.startsWith(path)) && sessionToken)
return NextResponse.redirect(new URL("/program", request.url));
return NextResponse.next();
}
// See "Matching Paths" below to learn more
export const config = {
matcher: [
"/my-fitness-journey",
"/settings/preferences",
"/email-and-password",
"/subscription",
"/login",
"/signup",
],
};