Quickstart¶
A project that signs people in with a username and password, from the
installation onwards. The sign-in is the project's - here
Django's own authenticate; the example
does it with a one-time code. Everything after it is the library's.
The sign-in, and a page to read¶
# accounts/views.py
from django.contrib.auth import authenticate
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from httponly_jwt.sessions import start_session
class SignIn(APIView):
permission_classes = (AllowAny,)
def post(self, request):
person = authenticate(
request,
username=request.data.get('username'),
password=request.data.get('password'),
)
if person is None:
return Response({'detail': 'Wrong username or password.'}, status=400)
return start_session(Response({'id': person.pk}), person, request=request)
class Me(APIView):
permission_classes = (IsAuthenticated,)
def get(self, request):
return Response({'username': request.user.get_username()})
start_session mints a pair for the person and sets both cookies on the
response. The body says nothing about tokens.
# urls.py
from django.urls import path
from accounts.views import Me, SignIn
from httponly_jwt.views import RefreshView, SignOutView
urlpatterns = [
path('auth/sign-in/', SignIn.as_view()),
path('auth/refresh/', RefreshView.as_view()),
path('auth/sign-out/', SignOutView.as_view()),
path('me/', Me.as_view()),
]
Trying it¶
Sign in. The answer is {"id": 1} and two Set-Cookie lines, both
HttpOnly:
The cookies are the session:
When the access token has run out, the same request is still answered: the middleware renews it on the way through, and the answer sets the new one. Signing out blacklists the refresh token and clears both cookies:
The frontend¶
const answer = await fetch('/me/', { credentials: 'include' })
if (answer.status === 401) location.assign('/sign-in')
That is the whole of it: send the cookies, and go to the sign-in screen on a 401. The frontend never reads, stores or refreshes a token. For a frontend on another site, for apps, and for everything else, see the guide.