corregido refreshtoken y mejorado ver informacion ui por roles
This commit is contained in:
2
apps/api/src/database/migrations/0017_mute_mole_man.sql
Normal file
2
apps/api/src/database/migrations/0017_mute_mole_man.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE "auth"."sessions" ADD COLUMN "previous_session_token" varchar;--> statement-breakpoint
|
||||
ALTER TABLE "auth"."sessions" ADD COLUMN "last_rotated_at" timestamp;
|
||||
2006
apps/api/src/database/migrations/meta/0017_snapshot.json
Normal file
2006
apps/api/src/database/migrations/meta/0017_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -120,6 +120,13 @@
|
||||
"when": 1769653021994,
|
||||
"tag": "0016_silent_tag",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 17,
|
||||
"version": "7",
|
||||
"when": 1770774052351,
|
||||
"tag": "0017_mute_mole_man",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
import * as t from 'drizzle-orm/pg-core';
|
||||
import { sql } from 'drizzle-orm';
|
||||
import { authSchema } from './schemas';
|
||||
import * as t from 'drizzle-orm/pg-core';
|
||||
import { timestamps } from '../timestamps';
|
||||
import { states, municipalities, parishes } from './general';
|
||||
|
||||
import { municipalities, parishes, states } from './general';
|
||||
import { authSchema } from './schemas';
|
||||
|
||||
// Tabla de Usuarios sistema
|
||||
export const users = authSchema.table(
|
||||
@@ -15,9 +14,15 @@ export const users = authSchema.table(
|
||||
fullname: t.text('fullname').notNull(),
|
||||
phone: t.text('phone'),
|
||||
password: t.text('password').notNull(),
|
||||
state: t.integer('state').references(() => states.id, { onDelete: 'set null' }),
|
||||
municipality: t.integer('municipality').references(() => municipalities.id, { onDelete: 'set null' }),
|
||||
parish: t.integer('parish').references(() => parishes.id, { onDelete: 'set null' }),
|
||||
state: t
|
||||
.integer('state')
|
||||
.references(() => states.id, { onDelete: 'set null' }),
|
||||
municipality: t
|
||||
.integer('municipality')
|
||||
.references(() => municipalities.id, { onDelete: 'set null' }),
|
||||
parish: t
|
||||
.integer('parish')
|
||||
.references(() => parishes.id, { onDelete: 'set null' }),
|
||||
isTwoFactorEnabled: t
|
||||
.boolean('is_two_factor_enabled')
|
||||
.notNull()
|
||||
@@ -32,7 +37,6 @@ export const users = authSchema.table(
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
// Tabla de Roles
|
||||
export const roles = authSchema.table(
|
||||
'roles',
|
||||
@@ -46,8 +50,6 @@ export const roles = authSchema.table(
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
|
||||
//tabla User_roles
|
||||
export const usersRole = authSchema.table(
|
||||
'user_role',
|
||||
@@ -88,7 +90,6 @@ LEFT JOIN
|
||||
LEFT JOIN
|
||||
auth.roles r ON ur.role_id = r.id`);
|
||||
|
||||
|
||||
// Tabla de Sesiones
|
||||
export const sessions = authSchema.table(
|
||||
'sessions',
|
||||
@@ -103,6 +104,9 @@ export const sessions = authSchema.table(
|
||||
.notNull(),
|
||||
sessionToken: t.text('session_token').notNull(),
|
||||
expiresAt: t.integer('expires_at').notNull(),
|
||||
previousSessionToken: t.varchar('previous_session_token'),
|
||||
lastRotatedAt: t.timestamp('last_rotated_at'),
|
||||
|
||||
...timestamps,
|
||||
},
|
||||
(sessions) => ({
|
||||
@@ -110,8 +114,6 @@ export const sessions = authSchema.table(
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
|
||||
//tabla de tokens de verificación
|
||||
export const verificationTokens = authSchema.table(
|
||||
'verificationToken',
|
||||
|
||||
@@ -1,20 +1,9 @@
|
||||
// api/src/feacture/auth/auth.controller.ts
|
||||
import { Public } from '@/common/decorators';
|
||||
import { JwtRefreshGuard } from '@/common/guards/jwt-refresh.guard';
|
||||
import { RefreshTokenDto } from '@/features/auth/dto/refresh-token.dto';
|
||||
import { SignInUserDto } from '@/features/auth/dto/signIn-user.dto';
|
||||
import { SignOutUserDto } from '@/features/auth/dto/signOut-user.dto';
|
||||
import { SingUpUserDto } from '@/features/auth/dto/signUp-user.dto';
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
Patch,
|
||||
Post,
|
||||
Req,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { Body, Controller, HttpCode, Patch, Post } from '@nestjs/common';
|
||||
import { AuthService } from './auth.service';
|
||||
|
||||
@Controller('auth')
|
||||
@@ -58,17 +47,7 @@ export class AuthController {
|
||||
@Patch('refresh')
|
||||
//@RequirePermissions('auth:refresh-token')
|
||||
async refreshToken(@Body() refreshTokenDto: any) {
|
||||
|
||||
console.log('refreshTokenDto', refreshTokenDto);
|
||||
|
||||
const data = await this.authService.refreshToken(refreshTokenDto);
|
||||
|
||||
// console.log('data', data);
|
||||
|
||||
|
||||
if (!data) return null;
|
||||
|
||||
return {tokens: data}
|
||||
return await this.authService.refreshToken(refreshTokenDto);
|
||||
}
|
||||
|
||||
// @Public()
|
||||
|
||||
@@ -27,7 +27,7 @@ import { ConfigService } from '@nestjs/config';
|
||||
import { JwtService, JwtSignOptions } from '@nestjs/jwt';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import crypto from 'crypto';
|
||||
import { and, eq, or } from 'drizzle-orm';
|
||||
import { eq, or } from 'drizzle-orm';
|
||||
import { NodePgDatabase } from 'drizzle-orm/node-postgres';
|
||||
import * as schema from 'src/database/index';
|
||||
import { roles, sessions, users, usersRole } from 'src/database/index';
|
||||
@@ -273,48 +273,116 @@ export class AuthService {
|
||||
|
||||
//Refresh User Access Token
|
||||
async refreshToken(dto: RefreshTokenDto): Promise<RefreshTokenInterface> {
|
||||
const secret = envs.refresh_token_secret;
|
||||
const { user_id, token } = dto;
|
||||
const { refreshToken } = dto;
|
||||
|
||||
console.log('secret', secret);
|
||||
console.log('refresh_token', token);
|
||||
// 1. Validar firma del token (Crypto check)
|
||||
let payload: any;
|
||||
try {
|
||||
payload = await this.jwtService.verifyAsync(refreshToken, {
|
||||
secret: envs.refresh_token_secret,
|
||||
});
|
||||
} catch (e) {
|
||||
throw new UnauthorizedException('Invalid Refresh Token Signature');
|
||||
}
|
||||
|
||||
const validation = await this.jwtService.verifyAsync(token, {
|
||||
secret,
|
||||
});
|
||||
const userId = Number(payload.sub); // Es más seguro usar el ID del token que el del DTO
|
||||
|
||||
if (!validation) throw new UnauthorizedException('Invalid refresh token');
|
||||
|
||||
const session = await this.drizzle
|
||||
// 2. Buscar la sesión por UserID (SIN filtrar por token todavía)
|
||||
// Esto es clave: traemos la sesión para ver qué está pasando
|
||||
const [currentSession] = await this.drizzle
|
||||
.select()
|
||||
.from(sessions)
|
||||
.where(
|
||||
and(eq(sessions.userId, user_id), eq(sessions.sessionToken, token)),
|
||||
.where(eq(sessions.userId, userId));
|
||||
|
||||
if (!currentSession) throw new NotFoundException('Session not found');
|
||||
|
||||
// CONFIGURACIÓN: Tiempo de gracia en milisegundos (ej: 15 segundos)
|
||||
const GRACE_PERIOD_MS = 15000;
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// ESCENARIO A: Rotación Normal (El token coincide con el actual)
|
||||
// -------------------------------------------------------------------
|
||||
if (currentSession.sessionToken === refreshToken) {
|
||||
const user = await this.findUserById(userId);
|
||||
if (!user) throw new NotFoundException('User not found');
|
||||
|
||||
// Generar nuevos tokens (A -> B)
|
||||
const tokensNew = await this.generateTokens(user);
|
||||
const decodeAccess = this.decodeToken(tokensNew.access_token);
|
||||
const decodeRefresh = this.decodeToken(tokensNew.refresh_token);
|
||||
|
||||
// Actualizamos DB guardando el token "viejo" como "previous"
|
||||
await this.drizzle
|
||||
.update(sessions)
|
||||
.set({
|
||||
sessionToken: tokensNew.refresh_token, // Nuevo (B)
|
||||
previousSessionToken: refreshToken, // Viejo (A)
|
||||
lastRotatedAt: new Date(), // Marca de tiempo
|
||||
expiresAt: decodeRefresh.exp,
|
||||
})
|
||||
.where(eq(sessions.userId, userId));
|
||||
|
||||
return {
|
||||
access_token: tokensNew.access_token,
|
||||
access_expire_in: decodeAccess.exp,
|
||||
refresh_token: tokensNew.refresh_token,
|
||||
refresh_expire_in: decodeRefresh.exp,
|
||||
};
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------
|
||||
// ESCENARIO B: Periodo de Gracia (Condición de Carrera)
|
||||
// -------------------------------------------------------------------
|
||||
// El token no coincide con el actual, ¿pero coincide con el anterior?
|
||||
const isPreviousToken =
|
||||
currentSession.previousSessionToken === refreshToken;
|
||||
|
||||
// Calculamos cuánto tiempo ha pasado desde la rotación
|
||||
const timeSinceRotation = currentSession.lastRotatedAt
|
||||
? Date.now() - new Date(currentSession.lastRotatedAt).getTime()
|
||||
: Infinity;
|
||||
|
||||
if (isPreviousToken && timeSinceRotation < GRACE_PERIOD_MS) {
|
||||
// ¡Es una condición de carrera! El usuario envió 'A' pero ya rotamos a 'B'.
|
||||
// Le devolvemos 'B' (el actual en DB) para que se sincronice.
|
||||
|
||||
const user = await this.findUserById(userId);
|
||||
|
||||
if (!user) throw new NotFoundException('User not found');
|
||||
|
||||
// Generamos un access token nuevo fresco (barato)
|
||||
const accessTokenPayload = { sub: user.id, username: user.username };
|
||||
const newAccessToken = await this.jwtService.signAsync(
|
||||
accessTokenPayload,
|
||||
{
|
||||
secret: envs.access_token_secret,
|
||||
expiresIn: envs.access_token_expiration,
|
||||
} as JwtSignOptions,
|
||||
);
|
||||
const decodeAccess = this.decodeToken(newAccessToken);
|
||||
|
||||
// console.log(session.length);
|
||||
// IMPORTANTE: Devolvemos el refresh token QUE YA ESTÁ EN LA BASE DE DATOS
|
||||
// No generamos uno nuevo para no romper la cadena de la otra petición que ganó.
|
||||
return {
|
||||
access_token: newAccessToken,
|
||||
access_expire_in: decodeAccess.exp,
|
||||
refresh_token: currentSession.sessionToken!, // Devolvemos el token 'B'
|
||||
refresh_expire_in: currentSession.expiresAt as number,
|
||||
};
|
||||
}
|
||||
|
||||
if (session.length === 0) throw new NotFoundException('session not found');
|
||||
const user = await this.findUserById(user_id);
|
||||
if (!user) throw new NotFoundException('User not found');
|
||||
// -------------------------------------------------------------------
|
||||
// ESCENARIO C: Robo de Token (Reuse Detection)
|
||||
// -------------------------------------------------------------------
|
||||
// Si el token no es el actual, ni el anterior válido... ALGUIEN LO ROBÓ.
|
||||
// O el usuario está intentando reusar un token muy viejo.
|
||||
|
||||
// Genera token
|
||||
const tokens = await this.generateTokens(user);
|
||||
const decodeAccess = this.decodeToken(tokens.access_token);
|
||||
const decodeRefresh = this.decodeToken(tokens.refresh_token);
|
||||
// Medida de seguridad: Borrar todas las sesiones del usuario
|
||||
await this.drizzle.delete(sessions).where(eq(sessions.userId, userId));
|
||||
|
||||
// Actualiza session
|
||||
await this.drizzle
|
||||
.update(sessions)
|
||||
.set({ sessionToken: tokens.refresh_token, expiresAt: decodeRefresh.exp })
|
||||
.where(eq(sessions.userId, user_id));
|
||||
|
||||
return {
|
||||
access_token: tokens.access_token,
|
||||
access_expire_in: decodeAccess.exp,
|
||||
refresh_token: tokens.refresh_token,
|
||||
refresh_expire_in: decodeRefresh.exp,
|
||||
};
|
||||
throw new UnauthorizedException(
|
||||
'Refresh token reuse detected. Access revoked.',
|
||||
);
|
||||
}
|
||||
|
||||
async singUp(createUserDto: SingUpUserDto): Promise<User> {
|
||||
|
||||
@@ -7,9 +7,9 @@ export class RefreshTokenDto {
|
||||
@IsString({
|
||||
message: 'Refresh token must be a string',
|
||||
})
|
||||
token: string;
|
||||
refreshToken: string;
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
user_id: number;
|
||||
userId: number;
|
||||
}
|
||||
|
||||
@@ -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...");
|
||||
fetchApi.interceptors.request.use(
|
||||
async (config: InternalAxiosRequestConfig) => {
|
||||
try {
|
||||
const { getValidAccessToken } = await import('@/lib/auth-token');
|
||||
const token = await getValidAccessToken();
|
||||
|
||||
const { auth } = await import('@/lib/auth'); // Importación dinámica
|
||||
const session = await auth();
|
||||
const token = session?.access_token;
|
||||
|
||||
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>>(
|
||||
|
||||
@@ -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
|
||||
|
||||
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