Skip to content
thesarfo

Note

Logging In with JWT

The access/refresh token lifecycle with djoser + simplejwt, and how to override their default lifetimes.

views 0

The endpoint for logging in is “/auth/jwt/create” where when we visit, we will get fields for logging a user in with their username and password. After a successful login, we get two jwt tokens, an access and refresh token.

The access token is a short lived token that we use for calling secure api endpoints, but when it expires we use a refresh token to get a new access token. By default, the refresh token is valid for one day whilst the access token is valid for 5 minutes. But these settings can easily be overridden. Simply by creating a new setting in the settings.py file and

settings.py
from datetime import timedelta
SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(days=1)
}

All these can be seen in the simplejwt docs on readthedocs.io

To logout, there is no specific endpoint you have to hit, you just have to remove the token from the clientside, because jwt’s arent stored in the database.

Now when our access token expires, we have to refresh it in order to get a new one. So when we go to “/auth/jwt/refresh”, we will be prompted to type in our refresh token, and when we do it, we will then get a new access token that we can use.