2022-02-21 14:53:21 +01:00
|
|
|
import { Strategy } from 'passport-local';
|
|
|
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
|
|
|
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
|
|
|
import { AuthService } from './auth.service';
|
2022-02-27 20:27:22 +01:00
|
|
|
import { AsyncFailable, HasFailed } from 'picsur-shared/dist/types';
|
|
|
|
|
import { EUser } from 'picsur-shared/dist/entities/user.entity';
|
2022-02-21 14:53:21 +01:00
|
|
|
|
|
|
|
|
@Injectable()
|
2022-02-23 11:10:25 +01:00
|
|
|
export class LocalStrategy extends PassportStrategy(Strategy, 'local') {
|
2022-02-21 14:53:21 +01:00
|
|
|
constructor(private authService: AuthService) {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-26 16:06:32 +01:00
|
|
|
async validate(username: string, password: string): AsyncFailable<EUser> {
|
|
|
|
|
const user = await this.authService.authenticate(username, password);
|
|
|
|
|
if (HasFailed(user)) {
|
2022-02-21 14:53:21 +01:00
|
|
|
throw new UnauthorizedException();
|
|
|
|
|
}
|
2022-02-24 22:56:27 +01:00
|
|
|
|
2022-02-21 14:53:21 +01:00
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
}
|