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-24 22:56:27 +01:00
|
|
|
import { AsyncFailable, HasFailed } from 'imagur-shared/dist/types';
|
2022-02-25 12:22:00 +01:00
|
|
|
import { User } from 'imagur-shared/dist/dto/user.dto';
|
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-24 22:56:27 +01:00
|
|
|
async validate(username: string, password: string): AsyncFailable<User> {
|
|
|
|
|
const userEntity = await this.authService.authenticate(username, password);
|
|
|
|
|
if (HasFailed(userEntity)) {
|
2022-02-21 14:53:21 +01:00
|
|
|
throw new UnauthorizedException();
|
|
|
|
|
}
|
2022-02-24 22:56:27 +01:00
|
|
|
const user: User = {
|
|
|
|
|
username: userEntity.username,
|
|
|
|
|
isAdmin: userEntity.isAdmin,
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-21 14:53:21 +01:00
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
}
|