-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.ts
60 lines (58 loc) · 1.78 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
import NextAuth from "next-auth";
import authConfig from "@/auth.config";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { db } from "./lib/db";
import { getProfileAction } from "./actions/get-profile-action";
import { createUserProfileAction } from "./actions/create-user-profile-action";
import { getUserByEmailAction } from "./actions/get-user-by-email-action";
import { getAdminDetailsAction } from "./actions/get-admin-details";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
callbacks: {
async jwt({ token, account }) {
if (account) {
token.id = account.providerAccountId;
token.accessToken = account.access_token;
}
return token;
},
async signIn({ account, profile }) {
if (account?.provider !== "credentials") return true;
return true;
},
async session({ token, user, session }) {
if (token.sub && token.email) {
const adminDetails = await getAdminDetailsAction(token.email);
if (!adminDetails) {
const userProfile = await getProfileAction(token.sub);
if (!userProfile) {
await createUserProfileAction(token.sub);
}
}
}
if (session.user && token.sub && token.email) {
const getUserByEmail = await getUserByEmailAction(token.email);
if (token.provider !== "credentials") {
return {
...session,
user: {
...session.user,
id: token.sub,
emailVerified: token.emailVerified,
provider: token.provider,
role: getUserByEmail?.role,
},
};
}
}
return session;
},
},
adapter: PrismaAdapter(db),
session: { strategy: "jwt" },
...authConfig,
});