UNPKG

nest-authify

Version:

Complete authentication and authorization package for NestJS - Monolith and Microservices ready with OAuth, JWT, Redis sessions

27 lines (24 loc) 857 B
import { Inject, Injectable, UnauthorizedException } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { Strategy } from 'passport-local'; import { AUTH_SERVICE } from '../constants'; import { BaseAuthService } from '../services/base-auth.service'; @Injectable() export class LocalStrategy extends PassportStrategy(Strategy, 'local') { constructor( @Inject(AUTH_SERVICE) private authService: BaseAuthService, ) { super({ usernameField: 'username', // Puede ser username o email passwordField: 'password', }); } async validate(username: string, password: string): Promise<any> { const user = await this.authService.validateUser(username, password); if (!user) { throw new UnauthorizedException('Invalid credentials'); } return user; } }