itjuba
itjuba

Reputation: 528

Unable to log in with provided credentials django DRF

My first time trying django REST api , I am doing some athentification , now i facing this error whene i run this commande : [curl -X POST -d "username=&password=" http://127.0.0.1:8000/api/auth/login/] error : {"non_field_errors":["Unable to log in with provided credentials."]}

views.py :

class CreateUserAPIView(CreateAPIView):
    serializer_class = CreateUserSerializer
    permission_classes = [AllowAny]

    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        # We create a token than will be used for future auth
        token = Token.objects.create(user=serializer.instance)
        token_data = {"token": token.key}
        return Response(
            {**serializer.data, **token_data},
            status=status.HTTP_201_CREATED,
            headers=headers
        )

serializers.py

class CreateUserSerializer(serializers.ModelSerializer):
    username = serializers.CharField()
    password = serializers.CharField(write_only=True,
                                     style={'input_type': 'password'})

    class Meta:
        model = get_user_model()
        fields = ('username', 'password', 'first_name', 'last_name')
        write_only_fields = ('password')
        read_only_fields = ('is_staff', 'is_superuser', 'is_active',)

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

urls.py


urlpatterns = [
    path('auth/login/',
        obtain_auth_token,
        name='auth_user_login'),
    path('auth/register/',
        CreateUserAPIView.as_view(),
        name='auth_user_create'),
    path('auth/logout/',
        LogoutUserAPIView.as_view(),
        name='auth_user_logout')
]

Upvotes: 1

Views: 1912

Answers (2)

Lyle Rogers
Lyle Rogers

Reputation: 41

Adding this in settings.py fixed it for me

AUTHENTICATION_BACKENDS = (
    "django.contrib.auth.backends.ModelBackend",
    "allauth.account.auth_backends.AuthenticationBackend"
)

Upvotes: 0

Bernardo Duarte
Bernardo Duarte

Reputation: 4264

The tutorial you've pointed requires you to create a superuser in section 1.2, so create one for yourself and then use those informations on your login.

In your terminal you will type: python manage.py createsuperuser

Then you will be prompted as follows:

Username (leave blank to use 'ubuntu'): <YOUR_USERNAME>
Email address: <email> // you can leave it as blank
Password: <YOUR_PASSWORD>
Password (again): <YOUR_PASSWORD>

Then call again, replacing those values between <> with your input values:

curl -X POST -d "username=<YOUR_USERNAME>&password=<YOUR_PASSWORD>" http://127.0.0.1:8000/api/auth/login/

Upvotes: 1

Related Questions