-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
133 lines (117 loc) · 3.48 KB
/
Copy pathproxy.ts
File metadata and controls
133 lines (117 loc) · 3.48 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { NextResponse } from "next/server";
import { auth } from "./auth";
import { getHirexaVerificationGateForUser } from "@/app/lib/auth/hirexaVerification";
import { getOnboardingStatusForUser } from "@/app/lib/onboarding/status";
import { VERIFY_ACCOUNT_ROUTE } from "@/app/lib/onboarding-flow";
const AUTH_REQUIRED_PREFIXES = [
"/dashboard",
"/plans",
"/hirepilot",
"/settings",
"/profile",
"/applications",
"/agency",
"/recruiter",
];
const ONBOARDING_REDIRECT_PREFIXES = [
"/resume",
"/questions",
"/benefits",
"/questions/step2Resume",
"/onboarding/job-interest",
"/onboarding/time-saved",
"/onboarding/min-salary",
"/onboarding/skills",
"/onboarding/job-alerts",
"/onboarding/choose-workplace",
"/onboarding/account",
"/onboarding/profile",
];
const INCOMPLETE_ONBOARDING_BLOCKED_PREFIXES = [
"/dashboard",
"/job-tools/generate",
"/hirepilot",
"/job-tools/ai-assistant",
"/settings",
"/profile",
"/applications",
];
export default auth(async (req) => {
const { pathname, search, origin } = req.nextUrl;
const isAuthenticated = !!req.auth;
const userId = (req.auth?.user as { id?: string } | undefined)?.id ?? null;
if (
isAuthenticated &&
userId
) {
const verification = await getHirexaVerificationGateForUser(userId);
if (verification.requiresVerification) {
const verifyUrl = new URL(VERIFY_ACCOUNT_ROUTE, origin);
verifyUrl.searchParams.set("callbackUrl", `${pathname}${search}`);
return NextResponse.redirect(verifyUrl);
}
const onboarding = await getOnboardingStatusForUser(userId);
if (
onboarding.completed &&
ONBOARDING_REDIRECT_PREFIXES.some((prefix) => pathname.startsWith(prefix))
) {
return NextResponse.redirect(new URL("/dashboard", origin));
}
if (!onboarding.completed) {
const nextPath = onboarding.nextPath ?? "/resume";
if (pathname.startsWith("/onboarding/account")) {
return NextResponse.redirect(new URL(nextPath, origin));
}
if (pathname === "/questions" && nextPath !== "/questions") {
return NextResponse.redirect(new URL(nextPath, origin));
}
if (
INCOMPLETE_ONBOARDING_BLOCKED_PREFIXES.some((prefix) =>
pathname.startsWith(prefix)
)
) {
return NextResponse.redirect(new URL(nextPath, origin));
}
}
}
if (
!isAuthenticated &&
AUTH_REQUIRED_PREFIXES.some((prefix) => pathname.startsWith(prefix))
) {
const loginUrl = new URL("/login", origin);
if (pathname.startsWith("/recruiter") || pathname.startsWith("/agency")) {
loginUrl.searchParams.set("mode", "recruiter");
}
loginUrl.searchParams.set("callbackUrl", `${pathname}${search}`);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
});
export const config = {
matcher: [
"/resume",
"/questions",
"/dashboard/:path*",
"/hirepilot",
"/job-tools/:path*",
"/job-tools/generate",
"/job-tools/ai-assistant/:path*",
"/benefits/:path*",
"/plans/:path*",
"/settings/:path*",
"/profile",
"/applications/:path*",
"/agency/:path*",
"/recruiter/:path*",
"/saved-jobs",
"/onboarding/profile/:path*",
"/questions/step2Resume/:path*",
"/onboarding/job-interest/:path*",
"/onboarding/time-saved/:path*",
"/onboarding/min-salary/:path*",
"/onboarding/skills/:path*",
"/onboarding/job-alerts/:path*",
"/onboarding/choose-workplace/:path*",
"/onboarding/account/:path*",
],
};