import { PassportStrategy } from '@nestjs/passport' ; import { Strategy } from 'passport-local' ; import { AuthService } from '../auth.service' ; import { User } from '../../user/entities/user.entity' ; import { Injectable , UnauthorizedException } from '@nestjs/common' ; import { ContextIdFactory , ModuleRef } from '@nestjs/core' ; import { ReportLogger } from '../../log/ReportLogger' ; @ Injectable ( ) export class LocalStrategy extends PassportStrategy ( Strategy ) { constructor ( private moduleRef , private reportLogger ) { super ( { passReqToCallback : true } ) ; this . reportLogger . setContext ( 'LocalStrategy' ) ; } async validate ( request , username , password ) { const contextId = ContextIdFactory . getByRequest ( request ) ; const authService = await this . moduleRef . resolve ( AuthService , contextId ) ; const user = await authService . validateUser ( username , password ) ; if ( ! user ) { this . reportLogger . error ( '无法登录，SB' ) ; throw new UnauthorizedException ( ) ; } return user ; } }