28 lines
893 B
TypeScript
28 lines
893 B
TypeScript
// auth/actions/refresh-token-action.ts
|
|
'use server';
|
|
import { refreshApi } from '@/lib/refreshApi'; // Importa la nueva instancia
|
|
import {
|
|
RefreshTokenResponseSchema,
|
|
RefreshTokenValue,
|
|
} from '../schemas/refreshToken';
|
|
|
|
export const resfreshTokenAction = async (refreshToken: RefreshTokenValue) => {
|
|
try {
|
|
const response = await refreshApi.patch('/auth/refresh', refreshToken);
|
|
|
|
const parsed = RefreshTokenResponseSchema.safeParse(response.data);
|
|
|
|
if (!parsed.success) {
|
|
console.error('Error de validación en la respuesta de refresh token:', {
|
|
errors: parsed.error.errors,
|
|
receivedData: response.data,
|
|
});
|
|
return null;
|
|
}
|
|
|
|
return parsed.data;
|
|
} catch (error: any) { // Captura el error para acceso a error.response
|
|
console.error('Error al renovar el token:', error.response?.data || error.message);
|
|
return null;
|
|
}
|
|
}; |