-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.ts
37 lines (31 loc) · 1.02 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
import { auth } from "@/auth";
import { NextResponse } from "next/server";
export default auth((req) => {
const { role, onboardingStatus } = req.auth?.user || {};
const { pathname } = req.nextUrl;
if (pathname === "/" && !role) {
return NextResponse.redirect(`${req.nextUrl.origin}/login`);
}
if (pathname === "/profile" && !role) {
return NextResponse.redirect(`${req.nextUrl.origin}/login`);
}
if (pathname === "/profile/edit" && !req.auth?.user) {
return NextResponse.redirect(`${req.nextUrl.origin}/login`);
}
if (pathname === "/admin" && role !== "ADMIN") {
return NextResponse.redirect(`${req.nextUrl.origin}/`);
}
if (!onboardingStatus) {
return NextResponse.redirect(`${req.nextUrl.origin}/sign-up/onboarding`);
}
if (onboardingStatus < 4) {
return NextResponse.redirect(
`${req.nextUrl.origin}/sign-up/onboarding?step=${onboardingStatus}`
);
}
});
export const config = {
matcher: [
"/((?!api|_next/static|_next/image|login|sign-up|sign-up/onboarding).*)",
],
};