jira-5 apple login with first and last name

This commit is contained in:
jain
2023-07-03 12:58:06 +05:30
parent d2498f82ad
commit 014b5fe183
2 changed files with 56 additions and 50 deletions

View File

@ -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):

View File

@ -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