-
Notifications
You must be signed in to change notification settings - Fork 1
/
views.py
50 lines (35 loc) · 1.56 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from django.conf import settings
from django.contrib.auth import login, logout
from rest_framework import views, generics, response, permissions, authentication
from rest_framework.response import Response
from .serializers import UserSerializer, LoginSerializer
class CsrfExemptSessionAuthentication(authentication.SessionAuthentication):
def enforce_csrf(self, request):
return
class LoginView(views.APIView):
permission_classes = (permissions.AllowAny,)
authentication_classes = (CsrfExemptSessionAuthentication,)
def post(self, request):
serializer = LoginSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
login(request, user)
return response.Response(UserSerializer(user).data)
class LogoutView(views.APIView):
authentication_classes = (CsrfExemptSessionAuthentication,)
def post(self, request):
logout(request)
return response.Response()
class RegisterView(generics.CreateAPIView):
serializer_class = UserSerializer
permission_classes = (permissions.AllowAny,)
authentication_classes = (CsrfExemptSessionAuthentication,)
def perform_create(self, serializer):
user = serializer.save()
user.backend = settings.AUTHENTICATION_BACKENDS[0]
login(self.request, user)
class CheckAuth(views.APIView):
def get(self, request, *args, **kwargs):
if request.user.is_authenticated:
return Response({'is_authenticated': True})
return Response({'is_authenticated': False})