-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathauth.ts
71 lines (71 loc) · 1.93 KB
/
auth.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
import { PrismaAdapter } from "@auth/prisma-adapter";
import { UserRole } from "@prisma/client";
import NextAuth from "next-auth";
import authConfig from "./auth.config";
import { prisma } from "./lib/db";
import { findUserbyEmail } from "./services";
import { isTwoFactorAutenticationEnabled } from "./services/auth";
import { findOrgByOwnerId } from "./services/onboarding/org";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
unstable_update: update,
} = NextAuth({
adapter: PrismaAdapter(prisma),
session: {
strategy: "jwt",
},
pages: {
signIn: "/auth/login",
},
callbacks: {
async signIn({ user, email, account, profile }) {
if (account && (account.provider === "google" || account.provider === "github")) {
return true;
}
if (user.email) {
const registeredUser = await findUserbyEmail(user?.email);
if (!registeredUser?.emailVerified) return false;
}
return true;
},
async jwt({ token, user, trigger, session }) {
if (trigger && trigger === "update" && session) {
token.orgId = session.user.orgId;
return token;
}
if (user) {
// User is available during sign-in
if (user.id) {
const isTwoFactorEnabled = await isTwoFactorAutenticationEnabled(user?.id || "");
token.isTwoFactorEnabled = isTwoFactorEnabled;
const org = await findOrgByOwnerId(user.id);
token.orgId = org?.id || "";
if (org?.id) {
token.role = UserRole.ADMIN;
}
}
}
return token;
},
async session({ session, token }) {
// `session.user.role` is now a valid property, and will be type-checked
// in places like `useSession().data.user` or `auth().user`
if (session.user && token.sub) {
session.user.id = token.sub;
session.user.isTwoFactorEnabled = token.isTwoFactorEnabled as boolean;
session.user.orgId = token.orgId;
}
return {
...session,
user: {
...session.user,
role: token.role,
},
};
},
},
...authConfig,
});