-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.config.ts
More file actions
58 lines (48 loc) · 1.61 KB
/
Copy pathauth.config.ts
File metadata and controls
58 lines (48 loc) · 1.61 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
55
56
57
58
import Credentials from "next-auth/providers/credentials";
import bcrypt from "bcryptjs";
import { prisma } from "@/lib/prisma";
import type { NextAuthConfig } from "next-auth";
import { LoginSchema } from "./app/_lib/definitions";
// Notice this is only an object, not a full Auth.js instance
export default {
providers: [
Credentials({
authorize: async (credentials) => {
try {
let user = null;
// 1. Validate form fields
const validatedFields = await LoginSchema.validate(credentials);
// If any form fields are invalid, return early
if (!validatedFields.username || !validatedFields.password) {
return {
errors: validatedFields,
};
}
// 2. Prepare data for insertion into database
const { username, password } = validatedFields;
// 3. Insert the user into the database or verify if the user exists
user = await prisma.user.findFirst({
where: {
OR: [
{username},
{email: username}
]
},
});
if (!user) {
throw new Error("Invalid credentials.");
}
const isPasswordValid = await bcrypt.compare(password, user.password);
if (!isPasswordValid) {
throw new Error("Invalid credentials.");
}
// return JSON object with the user data
return user;
} catch (error) {
console.error("ERROR: ", error);
return null;
}
},
}),
],
} satisfies NextAuthConfig;