3
1

More than 3 years have passed since last update.

カスタムユーザーでdjango-rest_framework_simple-jwtを使用してユーザー認証しようとした時にハマった話

Posted at

エラーが出た!!

DjangoREST Frameworkのプロジェクトにdjoserを導入し、api/auth/jwt/createに対してPOSTしてもトークンが発行されず、何事かと思いひとまずコンソールにアクセスしてアクティブなユーザーでPOSTしたところ、以下のようなエラーが出ました。

"detail": "No active account found with the given credentials"

与えられた情報ではアクティブユーザー見つけられません!ってことなので早速エラー文でググってみると以下の記事が

Django drf simple-jwt authentication“detail”: “No active account found with the given credentials”

どうやらパスワードがDBに入る前にハッシュ化されておらず、シリアライザーに以下を追加すればちゃんと動いてくれるようになる様子。
なので同記事の中にある関数を追加。

serializers.py
from django.contrib.auth.hashers import make_password #追加
from djoser.serializers import UserSerializer

from .models import User

class CustomUserSerializer(UserSerializer):
    """
    ユーザー情報獲得のためのシリアライザー
    """
    class Meta:
        model = User
        fields = '__all__'

  # 追加
    def validate_password(self,value:str) ->str:
        """
        ハッシュ値に変換する
        """
        return make_password(value)

すると・・・

スクリーンショット 2020-04-18 21.07.38.png

無事トークンが発行されました。

ハマったときはびっくりしましたがなんとかなりました、良かったです。

3
1
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
3
1