-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
72 lines (67 loc) · 1.98 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
61
62
63
64
65
66
67
68
69
70
71
72
import axios from "axios";
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import prisma from "@/lib/db/db.config";
import bcrypt from "bcrypt";
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [
Credentials({
credentials: {
identifier: { label: "identifier" },
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
const { identifier, password } = credentials;
try {
const user = await prisma.user.findUnique({
where: {
email: identifier as string,
},
});
if (!user) throw new Error("User not found");
const isUserVerified = user.isVerified;
if (!isUserVerified) throw new Error("User not verified");
const isValidPassword = await bcrypt.compare(
password as string,
user.password as string
);
if (!isValidPassword) throw new Error("Invalid password");
return user as any;
} catch (error: any) {
throw new Error(error.message);
}
},
}),
],
callbacks: {
async session({ session, token }) {
if (token) {
session.user.id = token.userid as string;
session.user.isVerified = token.isVerified as boolean;
session.user.isAdmin = token.isAdmin as boolean;
session.user.projects = token.projects as string[];
session.user.name = token.name as string;
}
return session;
},
async jwt({ token, user }) {
if (user) {
token.userid = user.id;
token.isVerified = user.isVerified;
token.isAdmin = user.isAdmin;
token.projects = user.projects;
token.name = user.name;
}
return token;
},
},
pages: {
signIn: "/sign-in",
},
session: {
strategy: "jwt",
maxAge: 60 * 60 * 24,
},
secret: process.env.AUTH_SECRET,
trustHost: true,
});