21 lines
715 B
TypeScript
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;
|
|
}
|
|
} |