Nest.js passport-local email 使う

updated Oct 1, 2021created Oct 1, 2021

passportのLocalStrategyのデフォルト値は、usernamepasswordなので、emailに変更する。変えないと、401が返ってくる。

js
import { Strategy } from 'passport-local'; import { PassportStrategy } from '@nestjs/passport'; import { Injectable, UnauthorizedException } from '@nestjs/common'; import { AuthService } from './auth.service'; @Injectable() export class LocalStrategy extends PassportStrategy(Strategy) { constructor(private authService: AuthService) { super({ usernameField: 'email' }); // 👈 追記 } async validate(email: string, password: string): Promise<any> { const user = await this.authService.validateUser(email, password); if (!user) { throw new UnauthorizedException(); } return user; } }

passport.js - NestJS passport authentication returns 401 when using email for authentication - Stack Overflow