-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathauth.config.ts
74 lines (58 loc) · 2.19 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
import bcrypt from "bcryptjs";
import { getuserByEmail } from "@/data/user";
import GitHub from "next-auth/providers/github"
import Google from "next-auth/providers/google"
import type { NextAuthConfig } from "next-auth"
import Credentials from "next-auth/providers/credentials"
import { LoginSchema } from "@/schemas";
export default { providers: [
Google({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET
}),
GitHub({
clientId: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
}),
Credentials({
async authorize(credentials) {
console.log("Authorize called with credentials:", credentials);
try {
// Ensure credentials is not undefined
if (!credentials) {
console.error("No credentials provided");
return null;
}
// Validate credentials structure
const validatedFields = LoginSchema.safeParse({
email: credentials.email,
password: credentials.password
});
console.log("Validation result:", validatedFields);
if (!validatedFields.success) {
console.error("Validation failed:", validatedFields.error);
return null;
}
const { email, password } = validatedFields.data;
// Log email for debugging
console.log("Attempting to find user with email:", email);
const user = await getuserByEmail(email);
console.log("User found:", !!user);
if (!user || !user.password) {
console.error("User not found or no password");
return null;
}
const passwordMatch = await bcrypt.compare(password, user.password);
console.log("Password match:", passwordMatch);
if (passwordMatch) {
return user;
}
return null;
} catch (error) {
console.error("Authorization error:", error);
return null;
}
}
})
]
} satisfies NextAuthConfig