Commit 48963cf0 authored by Diego Iván's avatar Diego Iván
Browse files

agregando modulo general para la auth

parent 2c5b792d
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
import { Module } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { AuthService } from './auth.service';

@Module({
  controllers: [],
  providers: [JwtService, AuthService],
  imports: [],
  exports: [AuthService],
})
export class AuthModule {}
+18 −0
Original line number Diff line number Diff line
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { JwtConstants } from 'src/constants/jwt.constants';
import { PayloadJwtDto } from 'src/shared/dto/payload-jwt.dto';

@Injectable()
export class AuthService {
  constructor(private jwtService: JwtService) {}

  async validateToken(token: string): Promise<string | null> {
    try {
      const payload: PayloadJwtDto = await this.jwtService.verify(token, { secret: JwtConstants.SECRET });
      return payload.role;
    } catch (error) {
      throw new UnauthorizedException('Invalid token');
    }
  }
}