Last active
August 6, 2022 11:59
-
-
Save Angeloem/ab6605238ea5517e6535f695fefb3288 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
class UserLogin(APIView): | |
permission_classes = [AllowAny] | |
authentication_classes = [] | |
user = None | |
def call_login(self): | |
# check if PIN are correct | |
if not self.pin_correct(): | |
return Response({'value': 'Error logging in'}, status=status.HTTP_403_FORBIDDEN) | |
# get the user from the username | |
user_serialized = UserDetailsSerializer(self.user, context={'request': self.request}).data | |
# generate the JWT here | |
token = jwt.encode({'user_id': self.user.id, 'phone': self.user.username, 'exp': get_exp(), | |
'orig_iat': get_orig() | |
}, | |
settings.SECRET_KEY, | |
algorithm="HS256") | |
data = { | |
'user': user_serialized, | |
'token': token, | |
} | |
return Response(data, status=status.HTTP_200_OK) | |
def post(self, request, *args, **kwargs): | |
# perform a check to see if the phone number is verified within the database | |
try: | |
# trying to get the int value of the first part of the email before the @ | |
username = request.data['username'] | |
self.get_user(username) | |
return self.call_login() | |
except ObjectDoesNotExist: | |
# here, the user does not exist. So we return the failed response | |
return Response({'value': 'User does not exist'}, status=status.HTTP_404_NOT_FOUND) | |
def get_user(self, username: str): | |
self.user = User.objects.get(username=username) | |
def pin_correct(self): | |
pin = self.request.data['pin'] | |
# if pin match, pass else return response with error count | |
return check_password(password=pin, encoded=self.user.pin) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment