-
Notifications
You must be signed in to change notification settings - Fork 2
/
middleware.ts
58 lines (55 loc) · 1.49 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { NextRequest, NextResponse } from "next/server";
import { auth } from "./auth";
interface AuthRequest extends NextRequest {
auth?: {
user: {
id?: string;
email: string;
role: "youtuber" | "editor" | undefined;
name: string;
image: string;
};
expires: number;
};
}
export async function middleware(req: AuthRequest) {
const session = await auth();
console.log(session);
if (session) {
if (!session.user.role && req.nextUrl.pathname !== "/role") {
return NextResponse.redirect(new URL("/role", req.nextUrl));
} else if (session.user.role) {
const userRole = session.user.role;
if (userRole === "youtuber" && req.nextUrl.pathname === "/dashboard") {
return NextResponse.redirect(
new URL("/youtube/dashboard", req.nextUrl)
);
}
if (
userRole === "editor" &&
req.nextUrl.pathname === "/youtube/dashboard"
) {
return NextResponse.redirect(new URL("/dashboard", req.nextUrl));
}
if (req.nextUrl.pathname === "/") {
return NextResponse.redirect(
new URL(
userRole === "editor" ? "/dashboard" : "/youtube/dashboard",
req.nextUrl
)
);
}
}
}
return NextResponse.next();
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
"/",
"/sign-in",
"/api/:path*",
"/youtube/dashboard/:path",
"/dahsboard/:path",
],
};