-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.config.ts
73 lines (62 loc) · 2.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
import type { NextAuthConfig } from "next-auth";
import { compare } from "bcryptjs";
import Credentials from "next-auth/providers/credentials";
import Github from "next-auth/providers/github";
import Google from "next-auth/providers/google";
import { LoginSchema } from "@/src/schemas";
import { getUserByEmail } from "@/src/data-queries/user_data";
export default {
providers: [
Github({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
profile(profile) {
return {
id: profile.id.toString(),
name: profile.name,
email: profile.email,
};
},
}),
Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
profile(profile) {
return {
id: profile.id,
name: profile.name,
email: profile.email, // Access email from Google profile
image: profile.picture,
};
},
}),
Credentials({
// Start Custom Credentials Providers setup
async authorize(credentials) {
const validatedLoginData = LoginSchema.safeParse(credentials);
if (validatedLoginData.success) {
const { email, password } = validatedLoginData.data;
/**
* After we have validated the credentials we received from the custom user, we now have to check two things
* if the user exists, through the email.
*
* Check whether the user signed up by other providers.
* In this case, if the user signed up, and they don't have a password, they will use the provider.
*
* if none of this is true, then it returns a null value, prompting the user to signup
*/
const user = await getUserByEmail(email);
if (!user || !user.password) {
return null;
}
const matchedPassword = await compare(password, user.password);
if (matchedPassword) {
return user;
}
}
return null;
},
// End
}),
],
} satisfies NextAuthConfig;