-
Notifications
You must be signed in to change notification settings - Fork 59
/
auth.ts
39 lines (36 loc) · 965 Bytes
/
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
import { PrismaAdapter } from "@auth/prisma-adapter";
import NextAuth from "next-auth";
import authConfig from "./auth.config";
import prisma from "./lib/prisma";
export const { handlers, auth } = NextAuth({
adapter: PrismaAdapter(prisma),
session: { strategy: "jwt" },
callbacks: {
jwt: async ({ token, user, trigger }) => {
if (user) {
token.user = user;
}
// refresh the user's data if they update their name / email
if (trigger === "update") {
const refreshedUser = await prisma.user.findUnique({
where: { id: token.sub },
});
if (refreshedUser) {
token.user = refreshedUser;
} else {
return {};
}
}
return token;
},
session: async ({ session, token }) => {
session.user = {
id: token.sub,
// @ts-ignore
...(token || session).user,
};
return session;
},
},
...authConfig,
});