-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathmiddleware.ts
More file actions
36 lines (27 loc) · 1015 Bytes
/
middleware.ts
File metadata and controls
36 lines (27 loc) · 1015 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
31
32
33
34
35
36
import { auth } from "@/lib/auth";
import { headers } from "next/headers";
import { NextResponse, type NextRequest } from "next/server";
import { API_AUTH_PREFIX, DEFAULT_LOGIN_REDIRECT } from "./routes";
export async function middleware(request: NextRequest) {
const session = await auth.api.getSession({
headers: await headers(),
});
const pathname = request.nextUrl.pathname;
const isApiAuth = pathname.startsWith(API_AUTH_PREFIX);
if (isApiAuth) {
return NextResponse.next();
}
if (!session) {
return NextResponse.redirect(new URL(DEFAULT_LOGIN_REDIRECT, request.url));
}
if (session) {
// Redirect logged-in users from /dashboard or /settings (root) to /settings/themes
if (pathname === "/dashboard" || pathname === "/settings") {
return NextResponse.redirect(new URL("/settings/themes", request.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/editor/theme/:themeId", "/dashboard", "/settings/:path*", "/success"],
};