feat: local strategy for passport

This commit is contained in:
Louis Auzuret
2022-06-20 15:48:36 +02:00
parent 5cff56d930
commit 70ed6328d8
2 changed files with 27 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
import { Injectable } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class LocalAuthGuard extends AuthGuard('local') {}
+21
View File
@@ -0,0 +1,21 @@
import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from './auth.service';
import PayloadInterface from './interface/payload.interface';
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
super();
}
async validate(username: string, password: string): Promise<PayloadInterface> {
const user = await this.authService.validateUser(username, password);
if (!user) {
throw new UnauthorizedException();
}
return user;
}
}