52 lines
2.2 KiB
Python
52 lines
2.2 KiB
Python
from django.shortcuts import render, redirect
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
from rest_framework import status, authentication
|
|
from rest_framework.authtoken.models import Token
|
|
from rest_framework.authtoken.views import ObtainAuthToken
|
|
from django.contrib.auth import authenticate
|
|
from .serializer import LoginSerializer
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
class LoginView(APIView):
|
|
serializer_class = LoginSerializer
|
|
|
|
def post(self, request):
|
|
serializer = self.serializer_class(data=request.data)
|
|
|
|
if serializer.is_valid():
|
|
email = serializer.validated_data['email']
|
|
password = serializer.validated_data['password']
|
|
user = authenticate(request, email=email, password=password)
|
|
|
|
if user:
|
|
if not user.is_active:
|
|
return Response({'error': 'Account is inactive, please check your mails'}, status=status.HTTP_403_FORBIDDEN)
|
|
return self._create_token_response(user)
|
|
return Response({'error': 'Unable to login with provided credentials.'}, status=status.HTTP_401_UNAUTHORIZED)
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
def _create_token_response(self, user):
|
|
token, created = Token.objects.get_or_create(user=user)
|
|
return Response({'token': token.key}, status=status.HTTP_200_OK)
|
|
|
|
class LogoutView(APIView):
|
|
authentication_classes = [authentication.TokenAuthentication]
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def post(self, request):
|
|
try:
|
|
request.user.auth_token.delete()
|
|
return Response({"message": "Successfully logged out"}, status=status.HTTP_200_OK)
|
|
except Exception as e:
|
|
return Response({"error": "Logout failed"}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
class AuthView(APIView):
|
|
authentication_classes = [authentication.TokenAuthentication]
|
|
|
|
def get(self, request):
|
|
if request.user.is_authenticated:
|
|
return Response({"user_id": request.user.id}, status=status.HTTP_200_OK)
|
|
return Response({"error": "User is not logged in"}, status=status.HTTP_401_UNAUTHORIZED)
|
|
|