local.strategy.ts 597 B

1234567891011121314151617181920
  1. import { Strategy } from 'passport-local';
  2. import { PassportStrategy } from '@nestjs/passport';
  3. import { Injectable, UnauthorizedException } from '@nestjs/common';
  4. import { AuthService } from './auth.service';
  5. @Injectable()
  6. export class LocalStrategy extends PassportStrategy(Strategy) {
  7. constructor(private readonly authService: AuthService) {
  8. super();
  9. }
  10. async validate(username: string, password: string): Promise<any> {
  11. const user = await this.authService.validateUser(username, password);
  12. if (!user) {
  13. throw new UnauthorizedException();
  14. }
  15. return user;
  16. }
  17. }