-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.config.ts
71 lines (68 loc) · 1.88 KB
/
auth.config.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
64
65
66
67
68
69
70
71
import { NextAuthOptions } from "next-auth";
import GithubProvider from "next-auth/providers/github";
import GoogleProvider from "next-auth/providers/google";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
export const authOptions: NextAuthOptions = {
callbacks: {
async jwt({ token, user }) {
if (user) {
const dbUser = await prisma.user.findUnique({
where: {
email: user.email as string | undefined,
},
});
await prisma.$disconnect();
if (!dbUser) {
throw new Error("No user found");
}
token.id = dbUser.id;
}
return token;
},
async session({ session, token }) {
if (session?.user) {
session.user.id = token.id;
}
return session;
},
async signIn({ user }) {
let prismaUser = await prisma.user.findUnique({
where: { email: user.email as string | undefined },
});
try {
if (!prismaUser) {
const dbUser = {
name: user.name as string,
email: user.email as string,
avatarUrl: user.image as string,
};
prismaUser = await prisma.user.create({ data: dbUser });
}
user.id = prismaUser.id;
return true;
} catch (error) {
console.error("Error signing in:", error);
return false;
} finally {
await prisma.$disconnect();
}
},
},
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID as string,
clientSecret: process.env.GITHUB_SECRET as string,
}),
GoogleProvider({
clientId: process.env.GOOGLE_ID as string,
clientSecret: process.env.GOOGLE_SECRET as string,
}),
],
pages: {
verifyRequest: "/auth/verify-request",
signIn: "/auth/signin",
signOut: "/auth/signout",
error: "/auth/error",
},
};