Skip to content

Instantly share code, notes, and snippets.

@msukmanowsky
Created September 15, 2020 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msukmanowsky/11e667954161b1d256667d4a4d977012 to your computer and use it in GitHub Desktop.
Save msukmanowsky/11e667954161b1d256667d4a4d977012 to your computer and use it in GitHub Desktop.
from django.contrib.auth import login as django_login, logout as django_logout
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.decorators import (
api_view,
authentication_classes,
permission_classes,
)
from rest_framework.authentication import SessionAuthentication
from rest_framework.permissions import IsAuthenticated
from rest_framework import exceptions
from api.authentication import CustomBasicAuthentication
from api.serializers import UserSerializer
@api_view(["POST"])
@authentication_classes([CustomBasicAuthentication, SessionAuthentication])
def login(request: Request) -> Response:
# Important, this sets the session cookie on the response
django_login(request, request.user)
return Response(UserSerializer(request.user).data)
@api_view(["POST"])
@authentication_classes([SessionAuthentication])
@permission_classes([IsAuthenticated])
def logout(request: Request) -> Response:
django_logout(request)
return Response({})
@api_view(["GET"])
@authentication_classes([SessionAuthentication])
@permission_classes([IsAuthenticated])
def me(request: Request) -> Response:
return Response(UserSerializer(request.user).data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment