-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.config.ts
52 lines (45 loc) · 1.43 KB
/
auth.config.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
import Credentials from "next-auth/providers/credentials";
import Google from "next-auth/providers/google";
import Github from "next-auth/providers/github";
import { type NextAuthConfig } from "next-auth";
import prisma from "./lib/prisma";
import bcryptjs from "bcryptjs";
import { hashPassword } from "./lib/utils";
import { AuthSchema } from "./lib/schema";
import {
IncorrectPasswordError,
InvalidFieldsError,
UserNotFoundError,
} from "./lib/custom-errors";
export default {
providers: [
Google,
Github,
Credentials({
name: "credentials",
credentials: {
email: { label: "email", type: "text" },
password: { label: "password", type: "password" },
},
async authorize(credentials) {
const validatedFields = AuthSchema.safeParse(credentials);
if (validatedFields.success) {
const { email, password } = validatedFields.data;
const user = await prisma.user.findUnique({
where: {
email: email, // remind to convert to string
},
});
if (!user || !user.hashedPassword) throw new UserNotFoundError();
const passwordsMatch = bcryptjs.compareSync(
password,
user.hashedPassword
);
if (!passwordsMatch) throw new IncorrectPasswordError();
return user;
}
throw new InvalidFieldsError();
},
}),
],
} satisfies NextAuthConfig;