-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.config.ts
192 lines (190 loc) · 6.11 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import NextAuth, { NextAuthConfig } from "next-auth";
import Resend from "next-auth/providers/resend";
import SignupSA from "./app/actions/signup/SignUpWithCrendentails";
import getUserByEmail, { userType } from "./app/actions/signup/getUserByEmail";
import GitHub from "next-auth/providers/github";
import Google from "next-auth/providers/google";
import Credentials from "next-auth/providers/credentials";
import bcrypt from "bcryptjs";
import { JWT } from "next-auth/jwt";
import { Session } from "next-auth";
import { User } from "next-auth";
import { Account } from "next-auth";
import { Profile } from "next-auth";
import { Rolestype } from "./app/types/next-auth";
export default {
providers: [
Credentials({
credentials: {
name: {},
email: {},
password: {},
role: {},
},
// @ts-ignore
async authorize(
credentials: Partial<
Record<"name" | "email" | "password" | "role", unknown>
>,
req: Request
): Promise<
| {
name: string;
email: string;
role: string;
}
| null
| undefined
> {
let user = null;
// verify the login schema
// const { email, password } = await LoginSchema.parseAsync(credentials);
const { email, password, name, role } = credentials as {
email: string;
password: string;
name: string;
role: string;
};
console.log(
"credentails received from the sigin are ",
email,
password,
name,
role
);
// name is not checked as the it is not necssary for the signin
if (email && password) {
let userFromDB: userType | false = await getUserByEmail(email);
console.log("user from the db", userFromDB);
// signin
if (
userFromDB &&
userFromDB.name &&
userFromDB.email &&
userFromDB.role
) {
// check the password
let isPasswordMatch = await bcrypt.compare(
password,
userFromDB.password
);
if (isPasswordMatch) {
user = {
name: userFromDB.name,
email: userFromDB.email,
role: userFromDB.role,
};
return user;
} else {
// how to throw the error as the invald password asnd show this is error in the frontend as well
return null;
}
} else {
// as record doesn't exist signing up
const response: {
msg: string;
user: {
name: string;
email: string;
} | null;
} = await SignupSA(role, name, email, password);
// check if the user is created
if (response.user && response.user.name && response.user.email) {
user = {
name: response.user.name,
email: response.user.email,
role: role,
};
return user;
}
}
} else {
console.log("user", user);
return null;
}
},
}),
Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
GitHub,
],
// TODO once the gym is created then add the gym details to the sesstion
callbacks: {
async signIn({ user, account }) {
if (account && account.provider === "google") {
console.log("user in the google signin", user);
// google signin and signup
if (user && user.email) {
// check if the user is already in the database
const userFromDb: userType | false = await getUserByEmail(user.email);
if (
userFromDb &&
userFromDb.name &&
userFromDb.email &&
userFromDb.role
) {
// login
user.role = userFromDb.role as Rolestype;
user.name = userFromDb.name;
user.email = userFromDb.email;
}
// signup
// as without role we dont know what to type of user ,
// here allow user to signup once the user selecs the role we do backend call to create the user in the database[
return true;
}
console.log("false is the user is not in the database");
return false;
}
// if the user records not in the datbase then th token formed with the without role
return true;
},
async jwt({
token,
user,
trigger,
session,
}: {
token: JWT;
user?: User;
account?: Account | null;
profile?: Profile;
trigger?: "signIn" | "signUp" | "update";
session?: Session;
}) {
if (trigger === "update") {
console.log("session updates is triggered", session);
console.log("token is updating...", token);
console.log("updated token is ", token);
token.gym = session?.gym;
if (!token.role) {
token.role = session?.user?.role || session?.role;
}
return token;
}
if (user && user.email && user.name) {
token.role = user.role;
console.log("token when credentails are correct", token);
return token;
}
console.log(" token before passing to sesion callback", token);
// as we can't update the token directly so we are updating the session then using the session callback we are updating the token
// console.log("token is this ", token);
return token;
},
async session({ token, session }) {
console.log("token from the session callback ", token);
if (token && token.email && token.name && token.role ) {
session.user.name = token.name;
session.user.email = token.email;
session.gym = token.gym as gym;
session.role = token.role as Rolestype;
console.log("updated sesion from the session callback ", session);
return session;
}
return session;
},
},
} satisfies NextAuthConfig;