Created
August 31, 2023 09:37
-
-
Save luzdelsol668/28aa22158e4a8b1f3ba2835ab8488ea8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| @api_view(['POST']) | |
| def login(request): | |
| phone = request.data['phone'] | |
| password = request.data['password'] | |
| try: | |
| if User.objects.filter(phone=phone).exists(): | |
| user = User.objects.get(phone=phone) | |
| if user.is_banned: | |
| data = { | |
| "status": 0, | |
| "message": "Votre Compte est désactivé. veuillez contacter le support" | |
| } | |
| return Response(data) | |
| else: | |
| if authenticate(request, email=user.email, password=password): | |
| if Token.objects.filter(user_id=user.id).exists(): | |
| key = binascii.hexlify(os.urandom(20)).decode() | |
| Token.objects.filter(user_id=user.id).update(key=key) | |
| token = Token.objects.get(user_id=user.id) | |
| update_last_login(None, user) | |
| else: | |
| key = binascii.hexlify(os.urandom(20)).decode() | |
| Token.objects.create(user_id=user.id, key=key) | |
| token = Token.objects.get(user_id=user.id) | |
| data = { | |
| 'status': 1, | |
| 'token': str(token.key), | |
| 'message': "Vous êtes connecté avec succès" | |
| } | |
| return Response(data) | |
| else: | |
| data = { | |
| "status": 0, | |
| "message": "Erreur de connexion! Vos informations fournies sont incorrectes." | |
| } | |
| return Response(data) | |
| else: | |
| data = { | |
| "status": 0, | |
| "message": "Nous n'avons pas trouvé un compte lié avec ces informations" | |
| } | |
| return Response(data) | |
| except KeyError as e: | |
| return Response({"da": 'I got a KeyError - reason "%s"' % str(e)}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment