-
Notifications
You must be signed in to change notification settings - Fork 6
/
middleware.ts
31 lines (26 loc) · 1.04 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
import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
export async function middleware(req: NextRequest) {
const { pathname } = new URL(req.url, process.env.NEXTAUTH_URL);
// Check if the user is logged in
const session = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
if (pathname === "/admin") {
// Redirect user to /admin/dashboard if logged in
if (session) {
return NextResponse.redirect(
process.env.NEXTAUTH_URL + "/admin/dashboard"
);
}
} else if (pathname === "/admin/dashboard") {
// Check if the user is logged in, otherwise redirect to /admin
if (!session) {
return NextResponse.redirect(process.env.NEXTAUTH_URL + "/admin");
}
}
// If the user is logged in and accessing /admin/dashboard, continue to the requested page
if (session && pathname.startsWith("/admin/dashboard")) {
return NextResponse.next();
}
// If none of the conditions above match, continue to the requested page
return NextResponse.next();
}