-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.config.ts
More file actions
88 lines (76 loc) · 2.44 KB
/
auth.config.ts
File metadata and controls
88 lines (76 loc) · 2.44 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import type { NextAuthConfig } from "next-auth";
import NextAuth from "next-auth";
import Credentials from "next-auth/providers/credentials";
import { z } from "zod";
import prisma from "./lib/database/prisma";
import bcrypt from "bcryptjs";
// Esquema de validación para las credenciales
const CredentialsSchema = z.object({
email: z.string().email({ message: "Correo electrónico inválido" }),
password: z
.string()
.min(6, { message: "La contraseña debe tener al menos 6 caracteres" }),
});
// Interfaz para el modelo Usuario (basada en tu esquema)
/*
interface User {
id: string;
firstName: string;
lastName: string;
email: string;
password: string;
confirmPassword: string;
emailVerified?: Date | null;
rol: "ADMIN" | "USER";
image?: string | null;
}
*/
export const authConfig: NextAuthConfig = {
pages: {
signIn: "/auth/login",
newUser: "/auth/sign",
},
providers: [
Credentials({
// Define las credenciales esperadas
credentials: {
email: { label: "Email", type: "email" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
// Valida las credenciales
const parsedCredentials = CredentialsSchema.safeParse(credentials);
if (!parsedCredentials.success) {
throw new Error(parsedCredentials.error.issues[0].message);
}
const { email, password } = parsedCredentials.data;
try {
// Busca el usuario por correo (normalizado a minúsculas)
const user = await prisma.users.findUnique({
where: { email: email.toLowerCase() },
});
if (!user) {
throw new Error("Usuario no encontrado");
}
// Verifica la contraseña
const isPasswordValid = await bcrypt.compare(password, user.password);
if (!isPasswordValid) {
throw new Error("Contraseña incorrecta");
}
const {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
password: _password,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
confirmPassword: _confirmPassword,
...safeUser
} = user;
return safeUser;
} catch (error) {
console.error("Error en la autenticación:", error);
return null;
}
},
}),
],
};
export const { signIn, signOut, auth } = NextAuth(authConfig);