エラーが出た!!
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)
すると・・・
無事トークンが発行されました。
ハマったときはびっくりしましたがなんとかなりました、良かったです。