Repository navigation
Expand file tree
/
Copy pathproxy.ts
More file actions
69 lines (58 loc) · 1.99 KB
/
Copy pathproxy.ts
File metadata and controls
69 lines (58 loc) · 1.99 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { clerkMiddleware, createRouteMatcher } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const isBypassRoute = createRouteMatcher([
"/studio(.*)",
"/onboarding",
"/api/set-onboarded",
"/api/track-session",
"/api/user",
"/api/end-session(.*)",
"/sign-in(.*)",
"/sign-up(.*)",
"/sign-out",
]);
const isWebhookRoute = createRouteMatcher([
"/api/stripe/webhook",
"/api/end-session/webhook",
]);
function getOnboardingCookieValue(req: NextRequest): string | undefined {
return req.cookies.get("onboarding_complete")?.value;
}
function getVerifiedSessionCookie(req: NextRequest): string | undefined {
return req.cookies.get("ob_verified")?.value;
}
export default clerkMiddleware(async (auth, req: NextRequest) => {
try {
const { userId, sessionId } = await auth();
if (!userId) return NextResponse.next();
if (isWebhookRoute(req)) return NextResponse.next();
if (isBypassRoute(req)) return NextResponse.next();
const onboardingCookie = getOnboardingCookieValue(req);
const verifiedSession = getVerifiedSessionCookie(req);
// Fast path: cookie present AND we have DB-verified this session.
if (onboardingCookie === "1" && verifiedSession === sessionId) {
return NextResponse.next();
}
// Slow path: route through /api/set-onboarded for DB verification.
const verifyUrl = req.nextUrl.clone();
verifyUrl.pathname = "/api/set-onboarded";
verifyUrl.searchParams.set(
"next",
req.nextUrl.pathname + req.nextUrl.search
);
return NextResponse.redirect(verifyUrl);
} catch (error) {
console.error("[middleware] auth error:", error);
return NextResponse.json(
{ error: "Authentication error. Please try again." },
{ status: 500 }
);
}
});
export const config = {
matcher: [
"/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)",
"/(api|trpc)(.*)",
],
};