Adding the Login Feature

Learn to implement the user login feature using JWT authentication.

We'll cover the following

We are now sure that the endpoint behaves as we wish. The next step is adding the login endpoint following the same process: writing the serializer and the viewset, and then registering the route.

The login feature will require the email or the username with the password. Using the djangorestframework-simplejwt package, which provides a serializer called TokenObtainPairSerializer, we’ll write a serializer to check for user authentication but also return a response containing access and refresh tokens. For this, we will rewrite the validate method from the TokenObtainPairSerializer class. Inside the core/auth/serializers directory, create a new file called login.py (this file will contain LoginSerializer, a subclass of TokenObtainPairSerializer):

Get hands-on with 1200+ tech skills courses.