-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
49 lines (42 loc) · 1.05 KB
/
auth.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
import NextAuth from "next-auth"
import Google from "next-auth/providers/google"
import { db } from "@/utils/db"
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Google({
authorization: {
params: {
prompt: "consent",
access_type: "offline",
response_type: "code",
},
},
}),
],
callbacks: {
async signIn({ user }) {
const email = user.email ?? "";
const name = user.name ?? "";
const picture = user.image ?? "";
if(email === "" || name === "" || picture === "") {
console.log("Error: email, name, or picture is empty");
return false
}
const username = email.split("@")[0];
const currentUser = await db.user.findUnique({
where: { email },
});
if (!currentUser) {
await db.user.create({
data: {
email,
username: username,
nickname: name,
picture,
},
});
}
return true
},
},
})