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
text/typescript
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';
()
export class LocalStrategy extends PassportStrategy(Strategy, 'local') {
constructor(
(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;
}
}