added new Ep
This commit is contained in:
+10
-2
@@ -1,5 +1,13 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
// jwt-auth.guard.ts
|
||||
import { ExecutionContext, Injectable, ForbiddenException } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {}
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {
|
||||
handleRequest(err: any, user: any, info: any, context: ExecutionContext) {
|
||||
if (err || !user) {
|
||||
throw new ForbiddenException('Acceso denegado: token inválido o ausente');
|
||||
}
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,28 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
// jwt.strategy.ts
|
||||
import { Injectable, ForbiddenException } from '@nestjs/common';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
export interface JwtPayload {
|
||||
id: string;
|
||||
usuario: string;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class JwtStrategy extends PassportStrategy(Strategy) {
|
||||
constructor(private configService: ConfigService) {
|
||||
super({
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
ignoreExpiration: false,
|
||||
secretOrKey: configService.get('JWT_SECRET')!,
|
||||
secretOrKey: configService.get<string>('JWT_SECRET')!,
|
||||
});
|
||||
}
|
||||
|
||||
async validate(payload: any) {
|
||||
async validate(payload: JwtPayload) {
|
||||
if (!payload || !payload.id || !payload.usuario) {
|
||||
throw new ForbiddenException('Token inválido o corrupto');
|
||||
}
|
||||
return { id: payload.id, usuario: payload.usuario };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import {
|
||||
import type { Response } from 'express';
|
||||
import { UserService } from './user.service';
|
||||
import { changePasswordDto, CreateUserDto, Login } from './dto/create-user.dto';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { JwtAuthGuard } from './jwt.guard';
|
||||
|
||||
@Controller('user')
|
||||
@@ -23,7 +22,7 @@ export class UserController {
|
||||
return this.userService.Login(data, res);
|
||||
}
|
||||
|
||||
@UseGuards(AuthGuard('jwt'))
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('/validate-token')
|
||||
getProfile(@Req() req) {
|
||||
return req.user;
|
||||
@@ -35,7 +34,7 @@ export class UserController {
|
||||
}
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post('/change-password')
|
||||
@Post('/changePassword')
|
||||
async changePassword(@Body() data: changePasswordDto, @Req() req) {
|
||||
const id_usuario = req.user.id;
|
||||
await this.userService.changePassword(data, id_usuario);
|
||||
|
||||
Reference in New Issue
Block a user