Search code examples
djangodjango-rest-frameworkjwtdjango-rest-framework-jwt

"No active account found with the given credentials" in django-rest-framework-simple-jwt


I can't optain jwt with django-rest-framework-jwt. After I post-requested with jwt-optaining-url, it said "No active account found with the given credentials" even though the given information(email, password) is correct.

I think I've created a user with serializer in a wrong way. but can't come up with any new way.

login view

@csrf_exempt
def facebook_login(request):
    body = dict(request.GET)
    code = body['code'][0]

    params_access = {
        "client_id": FACEBOOK_APP_ID,
        "redirect_uri": FACEBOOK_REDIRECT_URI,
        "client_secret": FACEBOOK_SECRET,
        "code": code
    }
    tokens = requests.get("https://graph.facebook.com/v5.0/oauth/access_token", params=params_access).json()
    access_token = tokens['access_token']

    params_debug = {
        "input_token": access_token,
        "access_token": f'{FACEBOOK_APP_ID}|{FACEBOOK_SECRET}'
    }
    debug = requests.get("https://graph.facebook.com/debug_token", params=params_debug).json()

    params_user = {
        "fields": ["email"],
        "access_token": access_token
    }
    user_fb_data = requests.get("https://graph.facebook.com/me", params=params_user).json()
    user_email = user_fb_data['email']

    user = User.objects.filter(email=user_email)
    if not user:
        user_data = {
            'email': user_email,
            'username': user_email,
        }
        user = UserSerializer(data=user_data, partial=True)
        if user.is_valid():
            user.save()
            print("saved!!!!!!!!!!!!!!!")
        else:
            print("error", user.errors)

    jwt_data = {
        'email': user_email,
        'password': access_token
    }
    jwt = requests.post(JWT_OPTAIN_URL, data=jwt_data).json()
    access_token = jwt['access']
    refresh_token = jwt['refresh']
    data = {
        'access_token': access_token,
        'refresh_token': refresh_token
    }
    return Response(data, status=status.HTTP_201_CREATED)

Serializer

class UserSerializer(serializers.ModelSerializer):
    def create(self, validated_data):
        user = super().create(validated_data)
        user.set_password(self)
        user.save()
        return user

    class Meta:
        model = User
        fields = ('email', 'username', 'refreshToken', 'password')
        extra_kwargs = {'password': {'write_only': True}}

I expect the jwt is created with email and password information.


Solution

  • I finally resolved it with modifying some code and then creating a new user with admin page.

    def create(self, validated_data):
            user = super().create(validated_data)
            user.set_password(validated_data['password'])
            user.save()
            return user