Files
Picsur/backend/src/managers/auth/guards/localauth.strategy.ts

23 lines
779 B
TypeScript
Raw Normal View History

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-03 22:48:03 +01:00
import { EUserBackend } from '../../../models/entities/user.entity';
2022-03-07 20:19:58 +01:00
import { AuthManagerService } from '../auth.service';
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-07 20:19:58 +01:00
constructor(private authService: AuthManagerService) {
2022-02-21 14:53:21 +01:00
super();
}
2022-03-01 22:05:59 +01:00
async validate(username: string, password: string): AsyncFailable<EUserBackend> {
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;
}
}