corregido refreshtoken y mejorado ver informacion ui por roles
This commit is contained in:
@@ -2,4 +2,5 @@ AUTH_URL = http://localhost:3000
|
||||
AUTH_SECRET=wWgIwkHIGr28ydIUPsgVGNUNxXQ+brg1XXtA8PfjJjAEPJLDP2UTghWL8aE=
|
||||
API_URL=http://localhost:8000
|
||||
NEXT_PUBLIC_API_URL=http://localhost:8000
|
||||
NODE_ENV='development' #development | production
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ export const AdministrationItems: NavItem[] = [
|
||||
url: '/dashboard/administracion/usuario',
|
||||
icon: 'userPen',
|
||||
shortcut: ['m', 'm'],
|
||||
role: ['admin', 'superadmin', 'autoridad'],
|
||||
role: ['admin', 'superadmin'],
|
||||
},
|
||||
{
|
||||
title: 'Encuestas',
|
||||
@@ -60,7 +60,7 @@ export const StatisticsItems: NavItem[] = [
|
||||
url: '#', // Placeholder as there is no direct link for the parent
|
||||
icon: 'chartColumn',
|
||||
isActive: true,
|
||||
role: ['admin', 'superadmin', 'autoridad', 'manager'],
|
||||
role: ['admin', 'superadmin', 'autoridad'],
|
||||
|
||||
items: [
|
||||
// {
|
||||
@@ -82,7 +82,7 @@ export const StatisticsItems: NavItem[] = [
|
||||
shortcut: ['s', 's'],
|
||||
url: '/dashboard/estadisticas/socioproductiva',
|
||||
icon: 'blocks',
|
||||
role: ['admin', 'superadmin', 'autoridad', 'manager'],
|
||||
role: ['admin', 'superadmin', 'autoridad'],
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
27
apps/web/feactures/auth/actions/logout-action.ts
Normal file
27
apps/web/feactures/auth/actions/logout-action.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
'use server';
|
||||
|
||||
import { safeFetchApi } from '@/lib';
|
||||
import { cookies } from 'next/headers';
|
||||
import { logoutResponseSchema } from '../schemas/logout';
|
||||
|
||||
export const logoutAction = async (user_id: string) => {
|
||||
const payload = { user_id };
|
||||
|
||||
const [error, data] = await safeFetchApi(
|
||||
logoutResponseSchema,
|
||||
'/auth/sign-out',
|
||||
'POST',
|
||||
payload,
|
||||
);
|
||||
|
||||
if (error) {
|
||||
console.error('Error:', error);
|
||||
// Devuelve un objeto con la propiedad 'type' para que el callback de NextAuth lo reconozca como un error
|
||||
return {
|
||||
type: 'API_ERROR',
|
||||
message: error.message,
|
||||
};
|
||||
}
|
||||
|
||||
(await cookies()).delete('refresh_token');
|
||||
};
|
||||
5
apps/web/feactures/auth/schemas/logout.ts
Normal file
5
apps/web/feactures/auth/schemas/logout.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import z from 'zod';
|
||||
|
||||
export const logoutResponseSchema = z.object({
|
||||
message: z.string(),
|
||||
});
|
||||
@@ -4,7 +4,6 @@ import { tokensSchema } from './login';
|
||||
|
||||
// Esquema para el refresh token
|
||||
export const refreshTokenSchema = z.object({
|
||||
user_id: z.number(),
|
||||
token: z.string(),
|
||||
});
|
||||
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
'use client';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { Button } from '@repo/shadcn/button';
|
||||
import { Heading } from '@repo/shadcn/heading';
|
||||
import { Plus } from 'lucide-react';
|
||||
|
||||
import { useSession } from 'next-auth/react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
export function SurveysHeader() {
|
||||
const router = useRouter();
|
||||
const { data: session } = useSession();
|
||||
const role = session?.user.role[0]?.rol;
|
||||
return (
|
||||
<>
|
||||
<div className="flex items-start justify-between">
|
||||
@@ -14,11 +16,18 @@ export function SurveysHeader() {
|
||||
title="Administración de Encuestas"
|
||||
description="Gestiona las encuestas disponibles en la plataforma"
|
||||
/>
|
||||
<Button onClick={() => router.push(`/dashboard/administracion/encuestas/crear`)} size="sm">
|
||||
<Plus className="h-4 w-4"/><span className='hidden sm:inline'>Agregar Encuesta</span>
|
||||
</Button>
|
||||
{['superadmin', 'admin'].includes(role ?? '') && (
|
||||
<Button
|
||||
onClick={() =>
|
||||
router.push(`/dashboard/administracion/encuestas/crear`)
|
||||
}
|
||||
size="sm"
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
<span className="hidden sm:inline">Agregar Encuesta</span>
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
</>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { AlertModal } from '@/components/modal/alert-modal';
|
||||
import { useDeleteSurvey } from '@/feactures/surveys/hooks/use-mutation-surveys';
|
||||
import { SurveyTable } from '@/feactures/surveys/schemas/survey';
|
||||
import { Button } from '@repo/shadcn/button';
|
||||
import {
|
||||
Tooltip,
|
||||
@@ -10,9 +10,9 @@ import {
|
||||
TooltipTrigger,
|
||||
} from '@repo/shadcn/tooltip';
|
||||
import { Edit, Trash } from 'lucide-react';
|
||||
import { SurveyTable } from '@/feactures/surveys/schemas/survey';
|
||||
import { useDeleteSurvey } from '@/feactures/surveys/hooks/use-mutation-surveys';
|
||||
|
||||
import { useSession } from 'next-auth/react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useState } from 'react';
|
||||
|
||||
interface CellActionProps {
|
||||
data: SurveyTable;
|
||||
@@ -23,6 +23,7 @@ export const CellAction: React.FC<CellActionProps> = ({ data }) => {
|
||||
const [open, setOpen] = useState(false);
|
||||
const { mutate: deleteSurvey } = useDeleteSurvey();
|
||||
const router = useRouter();
|
||||
const { data: session } = useSession();
|
||||
|
||||
const onConfirm = async () => {
|
||||
try {
|
||||
@@ -36,6 +37,8 @@ export const CellAction: React.FC<CellActionProps> = ({ data }) => {
|
||||
}
|
||||
};
|
||||
|
||||
const role = session?.user.role[0]?.rol;
|
||||
|
||||
return (
|
||||
<>
|
||||
<AlertModal
|
||||
@@ -47,41 +50,48 @@ export const CellAction: React.FC<CellActionProps> = ({ data }) => {
|
||||
description="Esta acción no se puede deshacer."
|
||||
/>
|
||||
|
||||
|
||||
<div className="flex gap-1">
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => router.push(`/dashboard/administracion/encuestas/editar/${data.id!}`)}
|
||||
>
|
||||
<Edit className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Editar</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
{['superadmin', 'admin'].includes(role ?? '') && (
|
||||
<>
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() =>
|
||||
router.push(
|
||||
`/dashboard/administracion/encuestas/editar/${data.id!}`,
|
||||
)
|
||||
}
|
||||
>
|
||||
<Edit className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Editar</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => setOpen(true)}
|
||||
>
|
||||
<Trash className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Eliminar</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => setOpen(true)}
|
||||
>
|
||||
<Trash className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Eliminar</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -9,7 +9,8 @@ import {
|
||||
TooltipProvider,
|
||||
TooltipTrigger,
|
||||
} from '@repo/shadcn/tooltip';
|
||||
import { Edit, Eye, Trash, FileDown } from 'lucide-react';
|
||||
import { Edit, Eye, Trash } from 'lucide-react';
|
||||
import { useSession } from 'next-auth/react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useState } from 'react';
|
||||
import { TrainingViewModal } from '../training-view-modal';
|
||||
@@ -25,6 +26,7 @@ export const CellAction: React.FC<CellActionProps> = ({ data, apiUrl }) => {
|
||||
const [viewOpen, setViewOpen] = useState(false);
|
||||
const { mutate: deleteTraining } = useDeleteTraining();
|
||||
const router = useRouter();
|
||||
const { data: session } = useSession();
|
||||
|
||||
const onConfirm = async () => {
|
||||
try {
|
||||
@@ -42,6 +44,8 @@ export const CellAction: React.FC<CellActionProps> = ({ data, apiUrl }) => {
|
||||
window.open(`${apiUrl}/training/export/${id}`, '_blank');
|
||||
};
|
||||
|
||||
const role = session?.user.role[0]?.rol;
|
||||
|
||||
return (
|
||||
<>
|
||||
<AlertModal
|
||||
@@ -60,75 +64,70 @@ export const CellAction: React.FC<CellActionProps> = ({ data, apiUrl }) => {
|
||||
/>
|
||||
|
||||
<div className="flex gap-1">
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => setViewOpen(true)}
|
||||
>
|
||||
<Eye className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Ver detalle</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
{/* VER DETALLE: superadmin, admin, autoridad, manager */}
|
||||
{['superadmin', 'admin', 'autoridad', 'manager'].includes(
|
||||
role ?? '',
|
||||
) && (
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => setViewOpen(true)}
|
||||
>
|
||||
<Eye className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Ver detalle</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
)}
|
||||
|
||||
{/* <TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => handleExport(data.id)}
|
||||
>
|
||||
<FileDown className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Exportar Excel</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider> */}
|
||||
{/* EDITAR Y ELIMINAR: Solo superadmin y admin */}
|
||||
{['superadmin', 'admin'].includes(role ?? '') && (
|
||||
<>
|
||||
{/* Editar */}
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() =>
|
||||
router.push(`/dashboard/formulario/editar/${data.id}`)
|
||||
}
|
||||
>
|
||||
<Edit className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Editar</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() =>
|
||||
router.push(`/dashboard/formulario/editar/${data.id}`)
|
||||
}
|
||||
>
|
||||
<Edit className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Editar</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => setOpen(true)}
|
||||
>
|
||||
<Trash className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Eliminar</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
{/* Eliminar */}
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button
|
||||
variant="outline"
|
||||
size="icon"
|
||||
onClick={() => setOpen(true)}
|
||||
>
|
||||
<Trash className="h-4 w-4" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent>
|
||||
<p>Eliminar</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
|
||||
@@ -3,12 +3,15 @@
|
||||
import { Button } from '@repo/shadcn/components/ui/button';
|
||||
import { DataTableSearch } from '@repo/shadcn/table/data-table-search';
|
||||
import { Plus } from 'lucide-react';
|
||||
import { useSession } from 'next-auth/react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useTrainingTableFilters } from './use-training-table-filters';
|
||||
|
||||
export default function TrainingTableAction() {
|
||||
const { searchQuery, setPage, setSearchQuery } = useTrainingTableFilters();
|
||||
const router = useRouter();
|
||||
const { data: session } = useSession();
|
||||
const role = session?.user.role[0]?.rol;
|
||||
return (
|
||||
<div className="flex items-center justify-between mt-4 ">
|
||||
<div className="flex items-center gap-4 flex-grow">
|
||||
@@ -19,13 +22,17 @@ export default function TrainingTableAction() {
|
||||
setPage={setPage}
|
||||
/>
|
||||
</div>{' '}
|
||||
<Button
|
||||
onClick={() => router.push(`/dashboard/formulario/nuevo`)}
|
||||
size="sm"
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
<span className="hidden md:inline">Nuevo Registro</span>
|
||||
</Button>
|
||||
{['superadmin', 'admin', 'manager', 'coordinators'].includes(
|
||||
role ?? '',
|
||||
) && (
|
||||
<Button
|
||||
onClick={() => router.push(`/dashboard/formulario/nuevo`)}
|
||||
size="sm"
|
||||
>
|
||||
<Plus className="h-4 w-4" />
|
||||
<span className="hidden md:inline">Nuevo Registro</span>
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
43
apps/web/lib/auth-token.ts
Normal file
43
apps/web/lib/auth-token.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { resfreshTokenAction } from '@/feactures/auth/actions/refresh-token-action';
|
||||
import { auth } from '@/lib/auth';
|
||||
import { cookies } from 'next/headers';
|
||||
import { cache } from 'react';
|
||||
|
||||
export const getValidAccessToken = cache(async () => {
|
||||
const session = await auth();
|
||||
|
||||
if (!session?.access_token) return null;
|
||||
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
// Restamos 10s para tener margen de seguridad
|
||||
const isValid = (session.access_expire_in as number) - 10 > now;
|
||||
|
||||
// A. Si es válido, lo retornamos directo
|
||||
if (isValid) return session.access_token;
|
||||
|
||||
// B. Si expiró, buscamos la cookie
|
||||
const cookieStore = cookies();
|
||||
const refreshToken = (await cookieStore).get('refresh_token')?.value;
|
||||
|
||||
if (!refreshToken) return null; // No hay refresh token, fin del juego
|
||||
|
||||
// C. Intentamos refrescar
|
||||
const newTokens = await resfreshTokenAction({ token: refreshToken });
|
||||
|
||||
if (!newTokens) {
|
||||
// Si falla el refresh (token revocado o expirado), borramos cookie
|
||||
(await cookieStore).delete('refresh_token');
|
||||
return null;
|
||||
}
|
||||
|
||||
// D. Guardamos el nuevo refresh token en cookie y retornamos el access token
|
||||
(await cookieStore).set('refresh_token', newTokens.tokens.refresh_token, {
|
||||
httpOnly: true,
|
||||
secure: process.env.NODE_ENV === 'production',
|
||||
sameSite: 'lax',
|
||||
path: '/',
|
||||
maxAge: 7 * 24 * 60 * 60,
|
||||
});
|
||||
|
||||
return newTokens.tokens.access_token;
|
||||
});
|
||||
@@ -1,11 +1,10 @@
|
||||
// lib/auth.config.ts
|
||||
import { SignInAction } from '@/feactures/auth/actions/login-action';
|
||||
import { resfreshTokenAction } from '@/feactures/auth/actions/refresh-token-action';
|
||||
import { logoutAction } from '@/feactures/auth/actions/logout-action';
|
||||
import { CredentialsSignin, NextAuthConfig, Session, User } from 'next-auth';
|
||||
import { DefaultJWT } from 'next-auth/jwt';
|
||||
import { DefaultJWT, JWT } from 'next-auth/jwt';
|
||||
import CredentialProvider from 'next-auth/providers/credentials';
|
||||
|
||||
|
||||
// Define los tipos para tus respuestas de SignInAction
|
||||
interface SignInSuccessResponse {
|
||||
message: string;
|
||||
@@ -58,8 +57,10 @@ const authConfig: NextAuthConfig = {
|
||||
|
||||
// **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.");
|
||||
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
|
||||
@@ -70,15 +71,19 @@ const authConfig: NextAuthConfig = {
|
||||
response.type === 'UNKNOWN_ERROR') // Incluye todos los tipos de error posibles
|
||||
) {
|
||||
// Si es un error, lánzalo. Este camino termina aquí.
|
||||
throw new CredentialsSignin("Error en la API:" + response.message);
|
||||
throw new CredentialsSignin('Error en la API:' + 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.");
|
||||
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 {
|
||||
@@ -89,11 +94,7 @@ const authConfig: NextAuthConfig = {
|
||||
role: response?.user.rol ?? [], // Add role array
|
||||
access_token: response?.tokens.access_token ?? '',
|
||||
access_expire_in: response?.tokens.access_expire_in ?? 0,
|
||||
refresh_token: response?.tokens.refresh_token ?? '',
|
||||
refresh_expire_in: response?.tokens.refresh_expire_in ?? 0,
|
||||
};
|
||||
|
||||
|
||||
},
|
||||
}),
|
||||
],
|
||||
@@ -101,11 +102,7 @@ const authConfig: NextAuthConfig = {
|
||||
signIn: '/', //sigin page
|
||||
},
|
||||
callbacks: {
|
||||
async jwt({ token, user }:{
|
||||
user: User
|
||||
token: any
|
||||
|
||||
}) {
|
||||
async jwt({ token, user }: { user: User; token: any }) {
|
||||
// 1. Manejar el inicio de sesión inicial
|
||||
// El `user` solo se proporciona en el primer inicio de sesión.
|
||||
if (user) {
|
||||
@@ -117,64 +114,14 @@ const authConfig: NextAuthConfig = {
|
||||
role: user.role,
|
||||
access_token: user.access_token,
|
||||
access_expire_in: user.access_expire_in,
|
||||
refresh_token: user.refresh_token,
|
||||
refresh_expire_in: user.refresh_expire_in
|
||||
}
|
||||
// return token;
|
||||
};
|
||||
}
|
||||
|
||||
// 2. Si no es un nuevo login, verificar la expiración del token
|
||||
const now = Math.floor(Date.now() / 1000); // Usar Math.floor para un número entero
|
||||
|
||||
// Verificar si el token de acceso aún es válido
|
||||
if (now < (token.access_expire_in as number)) {
|
||||
return token; // Si no ha expirado, no hacer nada y devolver el token actual
|
||||
}
|
||||
|
||||
// console.log("Now Access Expire:",token.access_expire_in);
|
||||
|
||||
|
||||
// 3. Si el token de acceso ha expirado, verificar el refresh token
|
||||
// console.log("Access token ha expirado. Verificando refresh token...");
|
||||
if (now > (token.refresh_expire_in as number)) {
|
||||
// console.log("Refresh token ha expirado. Forzando logout.");
|
||||
return null; // Forzar el logout al devolver null
|
||||
}
|
||||
|
||||
// console.log("token:", token.refresh_token);
|
||||
|
||||
|
||||
// 4. Si el token de acceso ha expirado pero el refresh token es válido, renovar
|
||||
console.log("Renovando token de acceso...");
|
||||
try {
|
||||
const refresh_token = { token: token.refresh_token as string, user_id: Number(token.id) as number}
|
||||
|
||||
const res = await resfreshTokenAction(refresh_token);
|
||||
|
||||
// console.log('res', res);
|
||||
|
||||
|
||||
if (!res || !res.tokens) {
|
||||
throw new Error('Fallo en la respuesta de la API de refresco.');
|
||||
}
|
||||
|
||||
// Actualizar el token directamente con los nuevos valores
|
||||
token.access_token = res.tokens.access_token;
|
||||
token.access_expire_in = res.tokens.access_expire_in;
|
||||
token.refresh_token = res.tokens.refresh_token;
|
||||
token.refresh_expire_in = res.tokens.refresh_expire_in;
|
||||
return token;
|
||||
|
||||
} catch (error) {
|
||||
console.error("Error al renovar el token: ", error);
|
||||
return null; // Fallo al renovar, forzar logout
|
||||
}
|
||||
return token;
|
||||
},
|
||||
async session({ session, token }: { session: Session; token: any }) {
|
||||
async session({ session, token }: { session: Session; token: DefaultJWT }) {
|
||||
session.access_token = token.access_token as string;
|
||||
session.access_expire_in = token.access_expire_in as number;
|
||||
session.refresh_token = token.refresh_token as string;
|
||||
session.refresh_expire_in = token.refresh_expire_in as number;
|
||||
session.user = {
|
||||
id: token.id as number,
|
||||
username: token.username as string,
|
||||
@@ -185,7 +132,18 @@ const authConfig: NextAuthConfig = {
|
||||
return session;
|
||||
},
|
||||
},
|
||||
|
||||
events: {
|
||||
async signOut(message) {
|
||||
// 1. verificamos que venga token (puede no venir con algunos providers)
|
||||
const token = (message as { token?: JWT }).token;
|
||||
if (!token?.access_token) return;
|
||||
try {
|
||||
await logoutAction(String(token?.id));
|
||||
} catch {
|
||||
/* silencioso para que next-auth siempre cierre */
|
||||
}
|
||||
},
|
||||
},
|
||||
} satisfies NextAuthConfig;
|
||||
|
||||
export default authConfig;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use server';
|
||||
import { env } from '@/lib/env';
|
||||
import axios from 'axios';
|
||||
import axios, { InternalAxiosRequestConfig } from 'axios';
|
||||
import { z } from 'zod';
|
||||
|
||||
// Crear instancia de Axios con la URL base validada
|
||||
@@ -10,33 +10,21 @@ const fetchApi = axios.create({
|
||||
|
||||
// Interceptor para incluir el token automáticamente en las peticiones
|
||||
// ESTE INTERCEPTOR ESTÁ BIEN PARA EL RESTO DE LAS PETICIONES AUTENTICADAS
|
||||
fetchApi.interceptors.request.use(async (config: any) => {
|
||||
try {
|
||||
// console.log("Solicitando autenticación...");
|
||||
|
||||
const { auth } = await import('@/lib/auth'); // Importación dinámica
|
||||
const session = await auth();
|
||||
const token = session?.access_token;
|
||||
fetchApi.interceptors.request.use(
|
||||
async (config: InternalAxiosRequestConfig) => {
|
||||
try {
|
||||
const { getValidAccessToken } = await import('@/lib/auth-token');
|
||||
const token = await getValidAccessToken();
|
||||
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
if (token) {
|
||||
config.headers.set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error getting auth token:', err);
|
||||
}
|
||||
|
||||
// **Importante:** Si el body es FormData, elimina el Content-Type para que Axios lo configure automáticamente.
|
||||
if (config.data instanceof FormData) {
|
||||
delete config.headers['Content-Type'];
|
||||
} else {
|
||||
config.headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
return config;
|
||||
} catch (error) {
|
||||
console.error('Error al obtener el token de autenticación para el interceptor:', error);
|
||||
// IMPORTANTE: Si ocurre un error aquí, es mejor rechazar la promesa
|
||||
// para que la solicitud no se envíe sin autorización.
|
||||
return Promise.reject(error);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// safeFetchApi sigue siendo útil para el resto de las llamadas que requieren autenticación
|
||||
export const safeFetchApi = async <T extends z.ZodSchema<any>>(
|
||||
@@ -97,4 +85,4 @@ export const safeFetchApi = async <T extends z.ZodSchema<any>>(
|
||||
}
|
||||
};
|
||||
|
||||
export { fetchApi };
|
||||
export { fetchApi };
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use server';
|
||||
import { env } from '@/lib/env'; // Importamos la configuración de entorno validada
|
||||
import axios from 'axios';
|
||||
import axios, { InternalAxiosRequestConfig } from 'axios';
|
||||
import { z } from 'zod';
|
||||
|
||||
// Crear instancia de Axios con la URL base validada
|
||||
@@ -12,22 +12,21 @@ const fetchApi = axios.create({
|
||||
});
|
||||
|
||||
// Interceptor para incluir el token automáticamente en las peticiones
|
||||
fetchApi.interceptors.request.use(async (config: any) => {
|
||||
try {
|
||||
// Importación dinámica para evitar la referencia circular
|
||||
const { auth } = await import('@/lib/auth');
|
||||
const session = await auth();
|
||||
const token = session?.access_token;
|
||||
fetchApi.interceptors.request.use(
|
||||
async (config: InternalAxiosRequestConfig) => {
|
||||
try {
|
||||
const { getValidAccessToken } = await import('@/lib/auth-token');
|
||||
const token = await getValidAccessToken();
|
||||
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
if (token) {
|
||||
config.headers.set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error getting auth token:', err);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error getting auth token:', error);
|
||||
}
|
||||
|
||||
return config;
|
||||
});
|
||||
return config;
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* Función para hacer peticiones con validación de respuesta
|
||||
@@ -96,4 +95,4 @@ export const safeFetchApi = async <T extends z.ZodSchema<any>>(
|
||||
}
|
||||
};
|
||||
|
||||
export { fetchApi };
|
||||
export { fetchApi };
|
||||
|
||||
6
apps/web/types/next-auth.d.ts
vendored
6
apps/web/types/next-auth.d.ts
vendored
@@ -4,8 +4,6 @@ declare module 'next-auth' {
|
||||
interface Session extends DefaultSession {
|
||||
access_token: string;
|
||||
access_expire_in: number;
|
||||
refresh_token: string;
|
||||
refresh_expire_in: number;
|
||||
user: {
|
||||
id: number;
|
||||
username: string;
|
||||
@@ -29,8 +27,6 @@ declare module 'next-auth' {
|
||||
}>;
|
||||
access_token: string;
|
||||
access_expire_in: number;
|
||||
refresh_token: string;
|
||||
refresh_expire_in: number;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +42,5 @@ declare module 'next-auth/jwt' {
|
||||
}>;
|
||||
access_token: string;
|
||||
access_expire_in: number;
|
||||
refresh_token: string;
|
||||
refresh_expire_in: number;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user