-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.config.ts
54 lines (43 loc) · 1.33 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
import { NextAuthConfig } from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { signInFormSchema } from "./validators";
import bcrypt from "bcryptjs";
import { getUserWithEmail } from "./lib/auth/user";
import Google from "next-auth/providers/google";
export default {
providers: [
Google,
Credentials({
credentials: {
email: {},
password: {},
},
authorize: async (credentials) => {
const validatedFields = signInFormSchema.safeParse(credentials);
if (validatedFields.success) {
const { email, password } = validatedFields.data;
// check email exist before sign in with data
const user = await getUserWithEmail(email);
if (!user || !user.password) return null;
const isPasswordMatch = bcrypt.compareSync(password, user.password);
if (!isPasswordMatch) {
return null;
}
return user;
}
return null;
},
}),
],
callbacks: {
async signIn({ user, account, profile, email, credentials }) {
try {
// Your sign-in logic here
return true; // Allow sign-in
} catch (error) {
console.error("Error in signIn callback:", error);
return false; // Deny sign-in
}
},
},
} satisfies NextAuthConfig;