In the following code I write the Login API but when I print the user it's give me None result
In the following code I created a Login API but when I hit the request in Postman it's always give me Error Response anyone help me out to rectify the problem. I post all of the code and dependencies so anyone can check and give me the solutions.
This is my views.py file
from django.shortcuts import render
from rest_framework.permissions import AllowAny
from rest_framework.views import APIView
from rest_framework.response import Response
from .serializers import UserSerializer, RegisterSerializer, UserLoginSerializer
from django.contrib.auth.models import User
from rest_framework import generics, permissions
from rest_framework import status
from django.contrib.auth import authenticate,login
from django.contrib import auth
# from knox.models import AuthToken
# Create your views here.
from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
#Login Credentials
"//Create a Login API in Django?"
class UserLoginView(APIView):
permission_classes = (AllowAny,)
serializer_class = UserLoginSerializer
def get(self,request,format=None):
serializer=UserLoginSerializer(data=request.data)
# print(serializer.is_valid(raise_exception=True));
if serializer.is_valid(raise_exception=True):
email = serializer.data.get('email')
password = serializer.data.get('password')
print(email)
print(password)
user=authenticate(email=email,password=password)
print(user)
if user is not None:
login(request, user)
return Response({'msg':'Login Success'},status=status.HTTP_200_OK)
else:
return Response({'msg':{'Email or Password Does not match'}},status=status.HTTP_404_NOT_FOUND)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#This is my serializer.py file.
class UserLoginSerializer(serializers.ModelSerializer):
email=serializers.EmailField(max_length=200)
class Meta:
model = User
fields= ['email','password']
When I hit the request then its always give me error so please check my login api code and resolve it asap.
Django default User model takes the (username & password) field for Authentication.
in your code, you tried to authenticate with (email & password) so it gives None on every login request.
try for Authentication (username & password) instead of (email & password) I mean change like this...
user=authenticate(username=username,password=password)
Instead of...
user=authenticate(email=email,password=password)
If you still want to Authenticate with email then you need to Customize the Default User model
for Customizing Django User Model refer to the link here
User Model Cutomizaion code is here...
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db import models
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from .managers import CustomUserManager
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
def __str__(self):
return self.email
In the above code this line USERNAME_FIELD = 'email' give permission to authenticate with Email field Insted of Username field
