-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
38 lines (29 loc) · 1.14 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
38
import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
export const config = {
matcher: ["/((?!api/|_next/|og|_static/|_vercel|[\\w-]+\\.\\w+).*)"],
};
export default async function middleware(req: NextRequest) {
const url = req.nextUrl;
if (req.nextUrl.pathname.endsWith(".png")) {
return NextResponse.next();
}
const dashboardRegex = /^\/dashboard\/[a-z]{7}$/;
// Redirect /dashboard/:id to /dashboard/:id/content immediately before the login check
if (dashboardRegex.test(url.pathname)) {
return NextResponse.redirect(new URL(`${url.pathname}/content`, req.url));
}
if (
url.pathname.startsWith("/dashboard") ||
url.pathname.startsWith("/login")
) {
const session = await getToken({ req });
if (!session && !url.pathname.startsWith("/login")) {
return NextResponse.redirect(new URL(`/login`, req.url));
} else if (session && url.pathname.startsWith("/login")) {
return NextResponse.redirect(new URL(`/dashboard`, req.url));
}
return NextResponse.next();
}
return NextResponse.rewrite(new URL(`/home${req.nextUrl.pathname}`, req.url));
}