-
-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathproxy.ts
More file actions
37 lines (29 loc) · 1.04 KB
/
proxy.ts
File metadata and controls
37 lines (29 loc) · 1.04 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
import { NextRequest, NextResponse } from "next/server";
import { headers } from "next/headers";
import { auth } from "@/lib/auth";
export async function proxy(request: NextRequest) {
const session = await auth.api.getSession({
headers: await headers(),
});
const { pathname } = request.nextUrl;
if (session?.user) {
if (pathname === "/login" || pathname === "/signup") {
const { db } = await import("@/lib/db");
const user = await db.user.findUnique({
where: { id: session.user.id },
select: { isOnboarded: true },
});
const redirectUrl = user?.isOnboarded ? "/dashboard" : "/onboarding/username";
return NextResponse.redirect(new URL(redirectUrl, request.url));
}
}
if (!session?.user) {
if (pathname.startsWith("/dashboard") || pathname.startsWith("/onboarding")) {
return NextResponse.redirect(new URL("/login", request.url));
}
}
return NextResponse.next();
}
export const config = {
matcher: ["/dashboard/:path*", "/onboarding/:path*", "/login", "/signup"],
};