Search code examples
djangopython-3.xdjango-rest-frameworkjwtpyjwt

How to use JWT (JSON Web Tokens) with Django and Python for creating the REST API for signup and login


I've being trying to implement the JWT (JSON Web Tokens) in the Django project. But I was not able to achieve the same. Can you please help me with some tutorial or tips or links to study the same.

I tried using the pyjwt in my project, but the token generated every time I hit the API was same for the same user email address and password.


Solution

  • JWT are combinations of three parts: Header, Payload and Verify Signature (see the image bellow), they can carry information about the user (name, id, etc.) or even the expiration time of the token. So, if none of this information changes, the token will be the same.

    enter image description here

    Every time a user logs on the system, a token will be generated using their informations (username, id, email, etc). If theese information not change, token not change. Unless the expiration time is added to the token, so, every time that an user logs, a new expiration time will be generated and added to the token, creating a new one. When the token expires, client can request a new access token (refresh)

    Links