nestjs_auth/src/auth/guards/local.strategy.ts
2022-09-01 17:18:40 +09:00

23 lines
751 B
TypeScript

import { Strategy } from 'passport-local'
import { PassportStrategy } from '@nestjs/passport'
import { Injectable, UnauthorizedException } from '@nestjs/common'
import { AuthService } from '../auth.service'
import { env } from 'process'
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
constructor(private authService: AuthService) {
//super()
//If you want to check user authenticate with custom column like 'email', try pass it.
super({ usernameField: env.AUTH_USERNAME_FIELD })
}
async validate(email: string, password: string): Promise<any> {
const user = await this.authService.validateUser(email, password)
if (!user) {
throw new UnauthorizedException()
}
return user
}
}