-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.ts
More file actions
86 lines (75 loc) Β· 2.19 KB
/
auth.ts
File metadata and controls
86 lines (75 loc) Β· 2.19 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// @ts-nocheck
import { NextAuthOptions } from "next-auth";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import GoogleProvider from "next-auth/providers/google";
import GithubProvider from "next-auth/providers/github";
// import { PrismaClient } from '@prisma/client'
import prisma from "./lib/database/dbConnect";
// const prisma = new PrismaClient()
export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma),
secret: process.env.NEXTAUTH_SECRET as string,
theme: {
logo: `https://avatars.githubusercontent.com/u/131470832?s=100&v=4`,
},
session: {
strategy: "jwt",
},
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_SECRET_ID as string,
}),
// GithubProvider({
// clientId: process.env.GITHUB_CLIENT_ID as string,
// clientSecret: process.env.GITHUB_SECRET as string,
// }),
],
callbacks: {
async signIn(user) {
try {
const existingUser = await prisma.user.findUnique({
where: {
email: user.user.email!,
},
});
if (!existingUser) {
const isAdmin =
user.user.email === "ns3887255@gmail.com" ? true : false;
const newUserProfile = await prisma.profile.create({
data: {
name: user.user.name,
email: user.user.email,
emailVerified: user.user.emailVerified,
image: user.user.image,
isAdmin: isAdmin,
isPro: isAdmin ? true : false,
},
});
}
return user;
} catch (error) {
console.error("Error creating profile:", error);
return user;
}
},
async session({ token, session }) {
if (token) {
const userProfile = await prisma.profile.findUnique({
where: { email: token.email },
});
session.user.profile = userProfile;
}
return session;
},
async jwt({ token }) {
const dbUser = await prisma.user.findUnique({
where: {
email: token.email!,
},
});
token.id = dbUser?.id;
return token;
},
},
};