-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
33 lines (31 loc) · 884 Bytes
/
auth.ts
File metadata and controls
33 lines (31 loc) · 884 Bytes
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
import NextAuth from "next-auth";
import { authConfig } from "./auth.config";
import { prisma } from "@/lib/prisma";
export const { auth, handlers, signIn, signOut } = NextAuth({
...authConfig,
callbacks: {
...(authConfig.callbacks ?? {}),
async jwt({ token, user }) {
const email = user?.email ?? token.email;
if (!email) return token;
if (user || !token.role) {
const dbUser = await prisma.user.upsert({
where: { email },
update: {
name: user?.name ?? undefined,
image: user?.image ?? undefined,
},
create: {
email,
name: user?.name ?? null,
image: user?.image ?? null,
},
select: { role: true },
});
token.role = dbUser.role;
}
return token;
},
},
secret: process.env.AUTH_SECRET,
});