NestJS-Boilerplate/src/application/authorization/local.strategy.ts
2024-06-10 10:21:25 -03:00

21 lines
715 B
TypeScript

import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthorizationService } from './authorization.service';
import { UserEntity } from 'src/database/user/user.entity';
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy, 'local') {
constructor(private authorizationService: AuthorizationService) {
super();
}
async validate(username: string, password: string): Promise<UserEntity> {
const user = await this.authorizationService.validateUser({ username: username, password: password });
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}