From 014b5fe183cb58ea6f57ac4b549d8bb831d67999 Mon Sep 17 00:00:00 2001 From: jain Date: Mon, 3 Jul 2023 12:58:06 +0530 Subject: [PATCH] jira-5 apple login with first and last name --- account/views.py | 95 +++++++++++++++++++++++++----------------------- base/messages.py | 11 +++--- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/account/views.py b/account/views.py index 106dfce..f645080 100644 --- a/account/views.py +++ b/account/views.py @@ -31,6 +31,7 @@ class GoogleLoginMixin: def google_login(self, request): access_token = request.data.get('access_token') user_type = request.data.get('user_type') + # decoded_data = jwt.decode(access_token, options={"verify_signature": False}) if not access_token: return Response({'error': 'Access token is required.'}, status=status.HTTP_400_BAD_REQUEST) @@ -40,7 +41,6 @@ class GoogleLoginMixin: info={ 'access_token': access_token, 'token_uri': 'https://oauth2.googleapis.com/token', - # 'token_uri': 'https://auth.googleapis.com/token', 'client_id': settings.GOOGLE_CLIENT_ID, 'client_secret': settings.GOOGLE_CLIENT_SECRET, 'refresh_token': None, @@ -48,16 +48,17 @@ class GoogleLoginMixin: ) print("credentials===>",credentials, '===>',credentials.token) user_info_endpoint = f'https://www.googleapis.com/oauth2/v3/userinfo?access_token={access_token}' - # user_info_endpoint = f'https://www.googleapis.com/auth/userinfo?access_token={access_token}' headers = {'Authorization': f'Bearer {credentials.token}'} response = requests.get(user_info_endpoint, headers=headers) response.raise_for_status() user_info = response.json() + print("user_info===>",user_info,'==>',type(user_info)) email = user_info['email'] - name = user_info['name'] + first_name = user_info['given_name'] + last_name = user_info['family_name'] profile_picture = user_info['picture'] except Exception as e: - return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST) + return custom_error_response(str(e), response_status=status.HTTP_400_BAD_REQUEST) # Check if the user exists in your database or create a new user # ... @@ -76,19 +77,20 @@ class GoogleLoginMixin: if not User.objects.filter(email__iexact=email).exists(): print("999999999999999") - user_obj = User.objects.create(username=email, - email=email) + user_obj = User.objects.create(username=email, email=email, first_name=first_name, last_name=last_name) if str(user_type) == '1': - Junior.objects.create(auth=user_obj, is_verified=True) + junior_query = Junior.objects.create(auth=user_obj, is_verified=True, is_active=True, + image=profile_picture) + serializer = JuniorSerializer(junior_query) if str(user_type) == '2': - Guardian.objects.create(user=user_obj, is_verified=True) - - + guardian_query = Guardian.objects.create(user=user_obj, is_verified=True, is_active=True, + image=profile_picture) + serializer = GuardianSerializer(guardian_query) # Return a JSON response with the user's email and name - return custom_response(SUCCESS_CODE['3003'], {'auth_token': get_token(), 'name': name, 'email': email, - 'profile_picture': profile_picture, "user_type":user_type}, + return custom_response(SUCCESS_CODE['3003'], serializer.data, response_status=status.HTTP_200_OK) + class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet): serializer_class = GoogleLoginSerializer1 @@ -101,42 +103,45 @@ class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet): class SigninWithApple(views.APIView): """This API is for sign in with Apple for app.""" def post(self, request): - token = request.data.get("identityToken") + token = request.data.get("access_token") user_type = request.data.get("user_type") if not token: - return Response({"message": "data should contain `identityToken`"}) - decoded_data = jwt.decode(token, options={"verify_signature": False}) - print("decoded_data===>",decoded_data) - user_data = {"email": decoded_data.get('email'),"username": decoded_data.get('email'), - "first_name": request.data.get("fullName").get("givenName"),"is_active": True, - "last_name": request.data.get("fullName").get("familyName"),} - if user_data['email'] and not user_data['first_name']: - user_data['first_name'] = user_data['email'].split("@")[0] - user_data['last_name'] = user_data['email'].split("@")[0] - if decoded_data.get("email"): - try: - user = User.objects.get(email=decoded_data.get("email")) - if str(user_type) == '1': - junior_query = Junior.objects.filter(auth=user).last() - print("junior_query==>", junior_query, '====>', type(junior_query)) - serializer = JuniorSerializer(junior_query) - if str(user_type) == '2': - guardian_query = Guardian.objects.filter(user=user).last() - print("guardian_query==>", guardian_query, '====>', type(guardian_query)) - serializer = GuardianSerializer(guardian_query) - return custom_response(SUCCESS_CODE['3003'], serializer.data, - response_status=status.HTTP_200_OK) + return custom_error_response(ERROR_CODE['2027'], response_status=status.HTTP_400_BAD_REQUEST) + try: + decoded_data = jwt.decode(token, options={"verify_signature": False}) + print("decoded_data===>",decoded_data) + user_data = {"email": decoded_data.get('email'),"username": decoded_data.get('email'), + "first_name": request.data.get("fullName").get("givenName"),"is_active": True, + "last_name": request.data.get("fullName").get("familyName"),} + if user_data['email'] and not user_data['first_name']: + user_data['first_name'] = user_data['email'].split("@")[0] + user_data['last_name'] = user_data['email'].split("@")[0] + if decoded_data.get("email"): + try: + user = User.objects.get(email=decoded_data.get("email")) + if str(user_type) == '1': + junior_query = Junior.objects.filter(auth=user).last() + print("junior_query==>", junior_query, '====>', type(junior_query)) + serializer = JuniorSerializer(junior_query) + if str(user_type) == '2': + guardian_query = Guardian.objects.filter(user=user).last() + print("guardian_query==>", guardian_query, '====>', type(guardian_query)) + serializer = GuardianSerializer(guardian_query) + return custom_response(SUCCESS_CODE['3003'], serializer.data, + response_status=status.HTTP_200_OK) - except User.DoesNotExist: - user = User.objects.create(**user_data) - if str(user_type) == '1': - junior_query = Junior.objects.create(auth=user, is_verified=True, is_active=True) - serializer = JuniorSerializer(junior_query) - if str(user_type) == '2': - guardian_query = Guardian.objects.create(user=user, is_verified=True, is_active=True) - serializer = GuardianSerializer(guardian_query) - return custom_response(SUCCESS_CODE['3003'], serializer.data, - response_status=status.HTTP_200_OK) + except User.DoesNotExist: + user = User.objects.create(**user_data) + if str(user_type) == '1': + junior_query = Junior.objects.create(auth=user, is_verified=True, is_active=True) + serializer = JuniorSerializer(junior_query) + if str(user_type) == '2': + guardian_query = Guardian.objects.create(user=user, is_verified=True, is_active=True) + serializer = GuardianSerializer(guardian_query) + return custom_response(SUCCESS_CODE['3003'], serializer.data, + response_status=status.HTTP_200_OK) + except Exception as e: + logging.error(e) class UpdateProfileImage(views.APIView): diff --git a/base/messages.py b/base/messages.py index 7d48f97..83e221b 100644 --- a/base/messages.py +++ b/base/messages.py @@ -45,11 +45,12 @@ ERROR_CODE = { "2019": "Either File extension or File size doesn't meet the requirements", "2020": "Enter valid mobile number", "2021": "Already register", - "2022":"Invalid Guardian code", - "2023":"Invalid user", - "2024":"Email not verified", - "2025":"Invalid input. Expected a list of strings.", - "2026" : "New password should not same as old password" + "2022": "Invalid Guardian code", + "2023": "Invalid user", + "2024": "Email not verified", + "2025": "Invalid input. Expected a list of strings.", + "2026": "New password should not same as old password", + "2027": "data should contain `identityToken`" } SUCCESS_CODE = { # Success code for password