Files
Picsur/backend/src/routes/auth/local.strategy.ts

25 lines
751 B
TypeScript
Raw Normal View History

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-21 22:36:47 +01:00
import { AsyncFailable, HasFailed } from 'src/types/failable';
import { UserEntity } from 'src/collections/userdb/user.entity';
2022-02-21 14:53:21 +01:00
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super();
}
2022-02-21 22:17:44 +01:00
async validate(
username: string,
password: string,
): AsyncFailable<UserEntity> {
2022-02-21 14:53:21 +01:00
const user = await this.authService.authenticate(username, password);
2022-02-21 22:17:44 +01:00
if (HasFailed(user)) {
2022-02-21 14:53:21 +01:00
throw new UnauthorizedException();
}
return user;
}
}