-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.config.ts
93 lines (82 loc) · 2.93 KB
/
auth.config.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
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
import type { NextAuthConfig } from "next-auth";
export const authConfig = {
trustHost: true,
// Remove the trustHostedDomain property
// trustHostedDomain: true,
pages: {
signIn: "/login",
},
providers: [
// added later in auth.ts since it requires bcrypt which is only compatible with Node.js
// while this file is also used in non-Node.js environments
],
callbacks: {
async authorized({ auth, request: { nextUrl } }) {
// TODO: this logic conflicts with nextAuth callbackURL, it would be nice to fix it
const isLoggedIn = !!auth?.user;
if (!isLoggedIn) {
if (
nextUrl.pathname.startsWith("/login") ||
nextUrl.pathname.startsWith("/register")
) {
return true;
}
return false; // Redirect unauthenticated users to login page
}
if (nextUrl.pathname.startsWith("/admin")) {
// TODO: fix fetch node-gyp error from middleware
const res = await fetch(
new URL(`/api/get-role?userId=${auth.user?.id}`, nextUrl),
);
const { role } = await res.json();
if (role === "ADMIN") return true;
return Response.redirect(new URL("/forbbiden", nextUrl));
}
if (nextUrl.pathname === "/dashboard") {
return Response.redirect(
new URL(`/dashboard/${auth.user?.id}`, nextUrl),
);
}
if (nextUrl.pathname.startsWith("/dashboard")) {
const segments = nextUrl.pathname.split("/");
const userId = segments[2]; // '2' because 'segments' is ['', 'dashboard', '<user_id>']
const managerId = auth.user?.id;
const res = await fetch(
new URL(
`/api/is-managed-by?userId=${userId}&managerId=${managerId}`,
nextUrl,
),
);
const { isManagedBy } = await res.json();
if (
userId === managerId || // if i'm trying to see my own dashboard
isManagedBy // or it's a dashboard from someone I manage
) {
return true;
} else {
return Response.redirect(new URL("/forbbiden", nextUrl));
}
}
if (
nextUrl.pathname.startsWith("/login") ||
nextUrl.pathname.startsWith("/register")
)
return Response.redirect(new URL(`/profile/${auth.user?.id}`, nextUrl));
if (nextUrl.pathname === "/profile" || nextUrl.pathname == "/")
return Response.redirect(new URL(`/profile/${auth.user?.id}`, nextUrl));
return true;
},
async jwt({ token, user: jwtUser, trigger }) {
// Persist the OAuth access_token and or the user id to the token right after signin
if (trigger === "signIn") {
token.id = jwtUser.id;
}
return token;
},
async session({ session, token }) {
// Send properties to the client, like an access_token from a provider.
session.user.id = token.id as string;
return session;
},
},
} satisfies NextAuthConfig;