LoginSignup
0
0

😂passport-localで401 Unauthorizedが出てハマった話

Last updated at Posted at 2023-07-21

結論

passport-localはデフォルトでは以下のプロパティ名を持つデータしか受け取ることができません!

  • username
  • password
悪い例
{
  "email": "user@example.com",
  "password": "hogefuga"
}
良い例
{
  "username": "user@example.com", // ← usernameを使う
  "password": "hogefuga"
}

カスタマイズもできる

プロパティ名をusername以外にしたい時はusernameFieldオプションで明示的に指定する必要があります。

NestJSの場合は以下のように変更します。

NestJSでの例
...
@Injectable()
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super({ usernameField: 'email' }); // ←emailを使いたい場合
  }
...

このことを知らず半日を費やしてしまったため戒めの意味で記事を書きました😅

参考

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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0