Last active
October 25, 2018 15:31
-
-
Save yerkbn/31783c19800c4bc0a23a4a54b4678dd4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.auth import authenticate | |
from rest_framework.authtoken.models import Token | |
from rest_framework.decorators import api_view, permission_classes | |
from rest_framework.permissions import AllowAny | |
from rest_framework.status import ( | |
HTTP_400_BAD_REQUEST, | |
HTTP_404_NOT_FOUND, | |
HTTP_200_OK, | |
) | |
from rest_framework.response import Response | |
from .serializers import UserSerializer, UserSigninSerializer | |
from .authentication import token_expire_handler, expires_in | |
@api_view(["POST"]) | |
@permission_classes((AllowAny,)) # here we specify permission by default we set IsAuthenticated | |
def signin(request): | |
signin_serializer = UserSigninSerializer(data = request.data) | |
if not signin_serializer.is_valid(): | |
return Response(signin_serializer.errors, status = HTTP_400_BAD_REQUEST) | |
user = authenticate( | |
username = signin_serializer.data['username'], | |
password = signin_serializer.data['password'] | |
) | |
if not user: | |
return Response({'detail': 'Invalid Credentials or activate account'}, status=HTTP_404_NOT_FOUND) | |
#TOKEN STUFF | |
token, _ = Token.objects.get_or_create(user = user) | |
#token_expire_handler will check, if the token is expired it will generate new one | |
is_expired, token = token_expire_handler(token) # The implementation will be described further | |
user_serialized = UserSerializer(user) | |
return Response({ | |
'user': user_serialized.data, | |
'expires_in': expires_in(token), | |
'token': token.key | |
}, status=HTTP_200_OK) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment