-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
46 lines (41 loc) · 1.06 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
import NextAuth from "next-auth"
import Google from "next-auth/providers/google"
import { nanoid } from "nanoid"
import { prisma } from "./lib/prisma"
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [Google({
clientId: process.env.AUTH_GOOGLE_ID,
clientSecret: process.env.AUTH_GOOGLE_SECRET,
})],
callbacks: {
async signIn({ user, account, profile }) {
if (!user.email) return false
let dbUser = await prisma.user.findUnique({
where: { email: user.email },
})
if (!dbUser) {
dbUser = await prisma.user.create({
data: {
id: nanoid(),
name: user.name || `Anonymous${nanoid(6)}`,
email: user.email,
},
})
}
user.id = dbUser.id
return true
},
async session({ session, token }) {
if (session.user && token.sub) {
session.user.id = token.sub;
}
return session;
},
async jwt({ token, user }) {
if (user) {
token.id = user.id
}
return token
}
},
})