-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.ts
More file actions
50 lines (44 loc) · 1.41 KB
/
auth.ts
File metadata and controls
50 lines (44 loc) · 1.41 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
import NextAuth from "next-auth";
import { PrismaAdapter } from "@auth/prisma-adapter";
import { getPrismaClient } from "@/lib/database";
import credentials from "next-auth/providers/credentials";
import bcrypt from "bcryptjs";
// Initialize standard MySQL connection via Prisma
const prisma = getPrismaClient();
export const { handlers, auth, signIn, signOut } = NextAuth({
adapter: PrismaAdapter(prisma),
providers: [
credentials({
credentials: {
email: { label: "Email", type: "email", placeholder: "example@email.com" },
password: { label: "Password", type: "password", placeholder: "******" },
},
authorize: async (credentials) => {
let user = null;
if (!credentials || typeof credentials.password !== "string" || typeof credentials.email !== "string") {
return Promise.reject(new Error("Invalid credentials"));
}
// logic to verify if the user exists
user = await prisma.user.findUnique({
where: {
email: credentials.email,
},
});
if (!user) {
return Promise.reject(new Error("User not found."));
}
return user;
},
}),
],
secret: process.env.AUTH_SECRET,
session: { strategy: "jwt", maxAge: 60 * 60 * 24 },
callbacks: {
session: ({ session }) => {
return session;
},
jwt: ({ token, user }) => {
return token;
}
},
});