corregido refreshtoken y mejorado ver informacion ui por roles
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
'use server';
|
||||
import { safeFetchApi } from '@/lib';
|
||||
import { cookies } from 'next/headers';
|
||||
import { loginResponseSchema, UserFormValue } from '../schemas/login';
|
||||
|
||||
type LoginActionSuccess = {
|
||||
@@ -17,18 +18,18 @@ type LoginActionSuccess = {
|
||||
refresh_token: string;
|
||||
refresh_expire_in: number;
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
type LoginActionError = {
|
||||
type: 'API_ERROR' | 'VALIDATION_ERROR' | 'UNKNOWN_ERROR'; // **Asegúrate de que el tipo de `type` sea este aquí**
|
||||
message: string;
|
||||
details?: any;
|
||||
type: 'API_ERROR' | 'VALIDATION_ERROR' | 'UNKNOWN_ERROR'; // **Asegúrate de que el tipo de `type` sea este aquí**
|
||||
message: string;
|
||||
details?: any;
|
||||
};
|
||||
|
||||
// Si SignInAction también puede devolver null, asegúralo en su tipo de retorno
|
||||
type LoginActionResult = LoginActionSuccess | LoginActionError | null;
|
||||
|
||||
export const SignInAction = async (payload: UserFormValue): Promise<LoginActionResult> => {
|
||||
export const SignInAction = async (payload: UserFormValue) => {
|
||||
const [error, data] = await safeFetchApi(
|
||||
loginResponseSchema,
|
||||
'/auth/sign-in',
|
||||
@@ -36,12 +37,22 @@ export const SignInAction = async (payload: UserFormValue): Promise<LoginActionR
|
||||
payload,
|
||||
);
|
||||
if (error) {
|
||||
return {
|
||||
type: error.type as 'API_ERROR' | 'VALIDATION_ERROR' | 'UNKNOWN_ERROR',
|
||||
message: error.message,
|
||||
details: error.details
|
||||
};
|
||||
return error;
|
||||
} else {
|
||||
// 2. GUARDAR REFRESH TOKEN EN COOKIE (La clave del cambio)
|
||||
|
||||
(await cookies()).set(
|
||||
'refresh_token',
|
||||
String(data?.tokens?.refresh_token),
|
||||
{
|
||||
httpOnly: true, // JavaScript no puede leerla
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
maxAge: 7 * 24 * 60 * 60, // Ej: 7 días (debe coincidir con tu backend)
|
||||
},
|
||||
);
|
||||
|
||||
return data;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user