2022-02-21 14:53:21 +01:00
|
|
|
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
2022-03-06 12:34:33 +01:00
|
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
|
|
|
import { Strategy } from 'passport-local';
|
2022-02-27 20:27:22 +01:00
|
|
|
import { AsyncFailable, HasFailed } from 'picsur-shared/dist/types';
|
2022-03-12 15:10:22 +01:00
|
|
|
import { UsersService } from '../../../collections/userdb/userdb.service';
|
2022-03-03 22:48:03 +01:00
|
|
|
import { EUserBackend } from '../../../models/entities/user.entity';
|
2022-02-21 14:53:21 +01:00
|
|
|
|
|
|
|
|
@Injectable()
|
2022-03-03 22:48:03 +01:00
|
|
|
export class LocalAuthStrategy extends PassportStrategy(Strategy, 'local') {
|
2022-03-12 15:10:22 +01:00
|
|
|
constructor(private usersService: UsersService) {
|
2022-02-21 14:53:21 +01:00
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-28 13:44:33 +02:00
|
|
|
async validate(
|
|
|
|
|
username: string,
|
|
|
|
|
password: string,
|
|
|
|
|
): AsyncFailable<EUserBackend> {
|
|
|
|
|
|
|
|
|
|
// All this does is call the usersservice authenticate for authentication
|
2022-03-12 15:10:22 +01:00
|
|
|
const user = await this.usersService.authenticate(username, password);
|
2022-02-26 16:06:32 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|