-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.ts
More file actions
54 lines (48 loc) · 1.27 KB
/
auth.ts
File metadata and controls
54 lines (48 loc) · 1.27 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
import NextAuth from "next-auth";
import authConfig from "@/auth.config";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { db } from "@/lib/db";
export const {
handlers: { GET, POST },
auth,
signIn,
signOut,
} = NextAuth({
pages: {
signIn: "/signIn",
error: "/error",
},
callbacks: {
async signIn({ user, account }) {
if (account?.provider !== "credentails") return true;
const existingUser = await db.user.findUnique({
where: { id: user.id },
});
if (!existingUser?.emailVerified) return false;
return true;
},
async session({ token, session }) {
if (token.sub && session.user) {
session.user.id = token.sub;
session.user.phoneNumber = token.phoneNumber;
session.user.image = token.image;
}
return session;
},
async jwt({ token }) {
if (!token.sub) return token;
const user = await db.user.findFirst({
where: { id: token.sub },
});
if (!user) return token;
token.email = user.email;
token.image = user.image as string;
token.name = user.name;
token.phoneNumber = user.phoneNumber as string;
return token;
},
},
adapter: PrismaAdapter(db),
session: { strategy: "jwt" },
...authConfig,
});