-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
63 lines (60 loc) · 1.31 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import NextAuth from "next-auth"
import { PrismaAdapter } from "@auth/prisma-adapter"
import prisma from "@/lib/prisma"
import Google from "next-auth/providers/google"
import { Adapter } from "next-auth/adapters"
interface UserData {
name?: string | null
email?: string | null
image?: string | null
}
const customAdapter = {
...PrismaAdapter(prisma),
createUser: async (user: UserData) => {
if (!user.email) {
throw new Error("User email is required")
}
const existingUser = await prisma.user.findUnique({
where: { email: user.email },
})
if (existingUser) {
return existingUser
}
return prisma.user.create({ data: user })
},
} as Adapter
export const {
handlers,
auth,
signIn,
signOut
} = NextAuth({
adapter: customAdapter,
providers: [
Google({
clientId: process.env.AUTH_GOOGLE_ID,
clientSecret: process.env.AUTH_GOOGLE_SECRET,
}),
],
session: {
strategy: "jwt",
},
callbacks: {
async jwt({ token, user, account }) {
if (account) {
token.id = user.id;
}
return token;
},
async session({ session, token }) {
if (session.user) {
session.user.id = token.id as string;
}
return session;
},
},
pages: {
signIn: '/signin',
},
secret: process.env.AUTH_SECRET,
})