-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
104 lines (98 loc) · 2.61 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
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
import NextAuth from "next-auth";
import bcryptjs from "bcryptjs";
import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
import CredentialsProvider from "next-auth/providers/credentials";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { prisma } from "@/db";
import { Role } from "@prisma/client";
declare module "next-auth" {
// eslint-disable-next-line no-unused-vars
interface User {
role: Role;
onboardingStatus: number | null;
}
}
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
// eslint-disable-next-line camelcase
unstable_update,
} = NextAuth({
session: {
strategy: "jwt",
},
adapter: PrismaAdapter(prisma),
pages: {
newUser: "/sign-up/onboarding",
},
providers: [
GithubProvider({
clientId: process.env.GITHUB_CLIENT_ID!,
clientSecret: process.env.GITHUB_CLIENT_SECRET!,
}),
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}),
CredentialsProvider({
name: "credentials",
credentials: {
email: {
label: "Email",
type: "email",
placeholder: "Provider your email",
},
password: {
label: "Password",
type: "password",
placeholder: "Provider your password",
},
},
async authorize(credentials, request) {
const { email, password: credentialsPassword } = credentials;
const user = await prisma.user.findUnique({
where: {
email: email as string,
},
});
if (user) {
const passwordCheck = await bcryptjs.compare(
credentialsPassword as string,
user.password as string
);
if (passwordCheck) {
return user;
} else {
throw new Error("Invalid password");
}
} else {
return null;
}
},
}),
],
callbacks: {
async jwt({ token, user, trigger }) {
// everytime read or write to the token
if (user) {
token.role = user.role;
token.onboardingStatus = user.onboardingStatus;
}
if (trigger === "update") {
token.onboardingStatus = 5;
}
return token; // this token will get passed to session
},
async session({ session, token }) {
// will run this also in jwt
if (token) {
session.user.role = token.role as Role;
session.user.onboardingStatus = token.onboardingStatus as number;
}
return session;
},
},
});