-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
32 lines (32 loc) · 1002 Bytes
/
auth.js
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
import NextAuth from "next-auth";
import { getUserByEmail, createUser } from "@/src/queries";
import Google from "next-auth/providers/google";
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [Google],
callbacks: {
async signIn({ user }) {
try {
// console.log("USER", user);
const existingUser = await getUserByEmail(user.email);
// console.log("existingUser", existingUser);
if (
existingUser === undefined ||
existingUser === null ||
!existingUser ||
existingUser.length === 0
) {
// console.log("User does not exist, adding user");
await createUser({
name: user.name,
email: user.email,
image: user.image,
});
}
return true;
} catch (error) {
console.error("Error in signIn callback:", error);
throw new Error("Failed to execute database operations");
}
},
},
});