-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.config.ts
More file actions
120 lines (112 loc) · 4.63 KB
/
auth.config.ts
File metadata and controls
120 lines (112 loc) · 4.63 KB
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
import { type NextAuthConfig } from "next-auth"
import Github from "next-auth/providers/github"
import Google from "next-auth/providers/google"
import { signInSchema } from "./lib/formSchema"
import { compare } from "bcryptjs"
import Credentials from "next-auth/providers/credentials"
import { db } from "./lib/prisma"
import { PrismaAdapter } from "@auth/prisma-adapter"
export const authConfig = {
secret: process.env.AUTH_SECRET,
adapter: PrismaAdapter(db),
providers: [
Github({
clientId: process.env.AUTH_GITHUB_ID,
clientSecret: process.env.AUTH_GITHUB_SECRET,
}),
Google({
clientId: process.env.AUTH_GOOGLE_ID,
clientSecret: process.env.AUTH_GOOGLE_SECRET,
}),
Credentials({
async authorize(credentials) {
// console.log("Authorize function started...");
const validatedFields = signInSchema.safeParse(credentials);
if (validatedFields.success) {
const { email, password } = validatedFields.data;
// console.log(`Attempting to authorize user: ${email}`);
const user = await db.user.findUnique({ where: { email } });
if (!user || !user.password) {
// console.log("Authorization failed: User not found or no password set.");
return null;
}
const passwordsMatch = await compare(password, user.password);
if (passwordsMatch) {
// console.log(`Authorization successful for user: ${email}`);
return user;
}
}
// console.log("Authorization failed: Invalid credentials or validation error.");
return null;
}
}),
],
events: {
async linkAccount({ user }) {
await db.user.update({
where: { id: user.id },
data: { emailVerified: new Date() },
});
},
},
callbacks: {
async signIn({ user, account, profile }) {
// console.log(`Sign-in callback triggered for user: ${user.email}, provider: ${account?.provider}`);
if (account?.provider === "google") {
if (profile?.email_verified) {
// Update image if changed
if (profile?.picture) {
if (user.email) {
const dbUser = await db.user.findUnique({ where: { email: user.email } });
if (dbUser && dbUser.image !== profile.picture) {
await db.user.update({
where: { email: user.email },
data: { image: profile.picture },
});
}
}
}
return true;
}
// console.log("Google sign-in blocked: email not verified by Google.");
return false; // Block sign-in
}
// Allow other OAuth providers like GitHub without this specific check
if (account?.provider !== "credentials") {
return true;
}
// For "credentials" provider, check our database by email instead of id
if (user.email) {
const existingUser = await db.user.findUnique({ where: { email: user.email } });
if (!existingUser?.emailVerified) {
// console.log(`Sign-in blocked for ${user.email}: Email not verified in DB.`);
return false;
}
}
return true;
},
async jwt({ token, user }) {
if (user) { // This block is only triggered on sign-in
const dbUser = await db.user.findUnique({ where: { id: user.id } });
if (dbUser) {
token.id = dbUser.id;
token.name = `${dbUser.firstName} ${dbUser.lastName}`;
token.picture = dbUser.image;
}
}
return token;
},
async session({ session, token }) {
if (session.user) {
session.user.id = token.id as string;
session.user.name = token.name as string;
session.user.image = token.picture as string | null;
}
return session;
},
},
pages: {
signIn: "/login",
error: "/error",
},
} satisfies NextAuthConfig