corregido errores de compilacion para next en web
This commit is contained in:
@@ -4,6 +4,35 @@ import { CredentialsSignin, NextAuthConfig, Session, User } from 'next-auth';
|
||||
import { DefaultJWT } from 'next-auth/jwt';
|
||||
import CredentialProvider from 'next-auth/providers/credentials';
|
||||
|
||||
|
||||
// Define los tipos para tus respuestas de SignInAction
|
||||
interface SignInSuccessResponse {
|
||||
message: string;
|
||||
user: {
|
||||
email: string;
|
||||
username: string;
|
||||
id: number;
|
||||
rol: Array<{ id: number; rol: string }>;
|
||||
fullname: string;
|
||||
};
|
||||
tokens: {
|
||||
access_token: string;
|
||||
access_expire_in: number;
|
||||
refresh_token: string;
|
||||
refresh_expire_in: number;
|
||||
};
|
||||
}
|
||||
|
||||
// **CAMBIO AQUÍ**: `type: string;` en lugar de una unión de literales
|
||||
interface SignInErrorResponse {
|
||||
type: string; // Si SignInAction puede devolver cualquier string aquí
|
||||
message: string;
|
||||
details?: any;
|
||||
}
|
||||
|
||||
// Unión de tipos para el resultado de SignInAction, AHORA INCLUYE `null`
|
||||
type SignInActionResult = SignInSuccessResponse | SignInErrorResponse | null;
|
||||
|
||||
const authConfig: NextAuthConfig = {
|
||||
providers: [
|
||||
CredentialProvider({
|
||||
@@ -17,24 +46,39 @@ const authConfig: NextAuthConfig = {
|
||||
},
|
||||
async authorize(
|
||||
credentials: Partial<Record<'username' | 'password', unknown>>,
|
||||
request: Request,
|
||||
): Promise<User | null> {
|
||||
const credential = {
|
||||
username: credentials?.username as string,
|
||||
password: credentials?.password as string,
|
||||
};
|
||||
|
||||
const response = await SignInAction(credential);
|
||||
|
||||
// Asigna el tipo `SignInActionResult` que ahora incluye `null`
|
||||
const response: SignInActionResult = await SignInAction(credential);
|
||||
|
||||
// **NUEVO: Manejar el caso `null` primero**
|
||||
if (response === null) {
|
||||
console.error("SignInAction returned null, indicating a potential issue before API call or generic error.");
|
||||
throw new CredentialsSignin("Error de inicio de sesión inesperado.");
|
||||
}
|
||||
|
||||
// Tipo Guarda: Verificar la respuesta de error
|
||||
if (
|
||||
response &&
|
||||
'type' in response &&
|
||||
(response.type === 'API_ERROR' ||
|
||||
response.type === 'VALIDATION_ERROR')
|
||||
response.type === 'VALIDATION_ERROR' ||
|
||||
response.type === 'UNKNOWN_ERROR') // Incluye todos los tipos de error posibles
|
||||
) {
|
||||
// Si es un error, lánzalo. Este camino termina aquí.
|
||||
throw new CredentialsSignin(response.message);
|
||||
}
|
||||
|
||||
if (!('user' in response)) {
|
||||
// Esto solo ocurriría si SignInAction devolvió un objeto que no es null,
|
||||
// no es un error conocido por 'type', PERO tampoco tiene la propiedad 'user'.
|
||||
// Es un caso de respuesta inesperada del API.
|
||||
console.error("Respuesta de SignInAction con formato inesperado: falta la propiedad 'user'.");
|
||||
throw new CredentialsSignin("Error en el formato de la respuesta del servidor.");
|
||||
}
|
||||
|
||||
return {
|
||||
id: response?.user.id?.toString() ?? '0',
|
||||
@@ -58,12 +102,10 @@ const authConfig: NextAuthConfig = {
|
||||
callbacks: {
|
||||
async jwt({
|
||||
token,
|
||||
user,
|
||||
account,
|
||||
user
|
||||
}: {
|
||||
token: any;
|
||||
user: User;
|
||||
account: any;
|
||||
}) {
|
||||
// Si es un nuevo login, asignamos los datos
|
||||
if (user) {
|
||||
@@ -94,6 +136,7 @@ const authConfig: NextAuthConfig = {
|
||||
token.refresh_token = res.tokens.refresh_token;
|
||||
token.refresh_expire_in = res.tokens.refresh_expire_in;
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user