mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-11-27 00:54:54 +00:00
jira-5 apple login with first and last name
This commit is contained in:
@ -31,6 +31,7 @@ class GoogleLoginMixin:
|
|||||||
def google_login(self, request):
|
def google_login(self, request):
|
||||||
access_token = request.data.get('access_token')
|
access_token = request.data.get('access_token')
|
||||||
user_type = request.data.get('user_type')
|
user_type = request.data.get('user_type')
|
||||||
|
# decoded_data = jwt.decode(access_token, options={"verify_signature": False})
|
||||||
if not access_token:
|
if not access_token:
|
||||||
return Response({'error': 'Access token is required.'}, status=status.HTTP_400_BAD_REQUEST)
|
return Response({'error': 'Access token is required.'}, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
@ -40,7 +41,6 @@ class GoogleLoginMixin:
|
|||||||
info={
|
info={
|
||||||
'access_token': access_token,
|
'access_token': access_token,
|
||||||
'token_uri': 'https://oauth2.googleapis.com/token',
|
'token_uri': 'https://oauth2.googleapis.com/token',
|
||||||
# 'token_uri': 'https://auth.googleapis.com/token',
|
|
||||||
'client_id': settings.GOOGLE_CLIENT_ID,
|
'client_id': settings.GOOGLE_CLIENT_ID,
|
||||||
'client_secret': settings.GOOGLE_CLIENT_SECRET,
|
'client_secret': settings.GOOGLE_CLIENT_SECRET,
|
||||||
'refresh_token': None,
|
'refresh_token': None,
|
||||||
@ -48,16 +48,17 @@ class GoogleLoginMixin:
|
|||||||
)
|
)
|
||||||
print("credentials===>",credentials, '===>',credentials.token)
|
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/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}'}
|
headers = {'Authorization': f'Bearer {credentials.token}'}
|
||||||
response = requests.get(user_info_endpoint, headers=headers)
|
response = requests.get(user_info_endpoint, headers=headers)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
user_info = response.json()
|
user_info = response.json()
|
||||||
|
print("user_info===>",user_info,'==>',type(user_info))
|
||||||
email = user_info['email']
|
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']
|
profile_picture = user_info['picture']
|
||||||
except Exception as e:
|
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
|
# 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():
|
if not User.objects.filter(email__iexact=email).exists():
|
||||||
print("999999999999999")
|
print("999999999999999")
|
||||||
user_obj = User.objects.create(username=email,
|
user_obj = User.objects.create(username=email, email=email, first_name=first_name, last_name=last_name)
|
||||||
email=email)
|
|
||||||
if str(user_type) == '1':
|
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':
|
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 a JSON response with the user's email and name
|
||||||
return custom_response(SUCCESS_CODE['3003'], {'auth_token': get_token(), 'name': name, 'email': email,
|
return custom_response(SUCCESS_CODE['3003'], serializer.data,
|
||||||
'profile_picture': profile_picture, "user_type":user_type},
|
|
||||||
response_status=status.HTTP_200_OK)
|
response_status=status.HTTP_200_OK)
|
||||||
|
|
||||||
|
|
||||||
class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet):
|
class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet):
|
||||||
serializer_class = GoogleLoginSerializer1
|
serializer_class = GoogleLoginSerializer1
|
||||||
|
|
||||||
@ -101,42 +103,45 @@ class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet):
|
|||||||
class SigninWithApple(views.APIView):
|
class SigninWithApple(views.APIView):
|
||||||
"""This API is for sign in with Apple for app."""
|
"""This API is for sign in with Apple for app."""
|
||||||
def post(self, request):
|
def post(self, request):
|
||||||
token = request.data.get("identityToken")
|
token = request.data.get("access_token")
|
||||||
user_type = request.data.get("user_type")
|
user_type = request.data.get("user_type")
|
||||||
if not token:
|
if not token:
|
||||||
return Response({"message": "data should contain `identityToken`"})
|
return custom_error_response(ERROR_CODE['2027'], response_status=status.HTTP_400_BAD_REQUEST)
|
||||||
decoded_data = jwt.decode(token, options={"verify_signature": False})
|
try:
|
||||||
print("decoded_data===>",decoded_data)
|
decoded_data = jwt.decode(token, options={"verify_signature": False})
|
||||||
user_data = {"email": decoded_data.get('email'),"username": decoded_data.get('email'),
|
print("decoded_data===>",decoded_data)
|
||||||
"first_name": request.data.get("fullName").get("givenName"),"is_active": True,
|
user_data = {"email": decoded_data.get('email'),"username": decoded_data.get('email'),
|
||||||
"last_name": request.data.get("fullName").get("familyName"),}
|
"first_name": request.data.get("fullName").get("givenName"),"is_active": True,
|
||||||
if user_data['email'] and not user_data['first_name']:
|
"last_name": request.data.get("fullName").get("familyName"),}
|
||||||
user_data['first_name'] = user_data['email'].split("@")[0]
|
if user_data['email'] and not user_data['first_name']:
|
||||||
user_data['last_name'] = user_data['email'].split("@")[0]
|
user_data['first_name'] = user_data['email'].split("@")[0]
|
||||||
if decoded_data.get("email"):
|
user_data['last_name'] = user_data['email'].split("@")[0]
|
||||||
try:
|
if decoded_data.get("email"):
|
||||||
user = User.objects.get(email=decoded_data.get("email"))
|
try:
|
||||||
if str(user_type) == '1':
|
user = User.objects.get(email=decoded_data.get("email"))
|
||||||
junior_query = Junior.objects.filter(auth=user).last()
|
if str(user_type) == '1':
|
||||||
print("junior_query==>", junior_query, '====>', type(junior_query))
|
junior_query = Junior.objects.filter(auth=user).last()
|
||||||
serializer = JuniorSerializer(junior_query)
|
print("junior_query==>", junior_query, '====>', type(junior_query))
|
||||||
if str(user_type) == '2':
|
serializer = JuniorSerializer(junior_query)
|
||||||
guardian_query = Guardian.objects.filter(user=user).last()
|
if str(user_type) == '2':
|
||||||
print("guardian_query==>", guardian_query, '====>', type(guardian_query))
|
guardian_query = Guardian.objects.filter(user=user).last()
|
||||||
serializer = GuardianSerializer(guardian_query)
|
print("guardian_query==>", guardian_query, '====>', type(guardian_query))
|
||||||
return custom_response(SUCCESS_CODE['3003'], serializer.data,
|
serializer = GuardianSerializer(guardian_query)
|
||||||
response_status=status.HTTP_200_OK)
|
return custom_response(SUCCESS_CODE['3003'], serializer.data,
|
||||||
|
response_status=status.HTTP_200_OK)
|
||||||
|
|
||||||
except User.DoesNotExist:
|
except User.DoesNotExist:
|
||||||
user = User.objects.create(**user_data)
|
user = User.objects.create(**user_data)
|
||||||
if str(user_type) == '1':
|
if str(user_type) == '1':
|
||||||
junior_query = Junior.objects.create(auth=user, is_verified=True, is_active=True)
|
junior_query = Junior.objects.create(auth=user, is_verified=True, is_active=True)
|
||||||
serializer = JuniorSerializer(junior_query)
|
serializer = JuniorSerializer(junior_query)
|
||||||
if str(user_type) == '2':
|
if str(user_type) == '2':
|
||||||
guardian_query = Guardian.objects.create(user=user, is_verified=True, is_active=True)
|
guardian_query = Guardian.objects.create(user=user, is_verified=True, is_active=True)
|
||||||
serializer = GuardianSerializer(guardian_query)
|
serializer = GuardianSerializer(guardian_query)
|
||||||
return custom_response(SUCCESS_CODE['3003'], serializer.data,
|
return custom_response(SUCCESS_CODE['3003'], serializer.data,
|
||||||
response_status=status.HTTP_200_OK)
|
response_status=status.HTTP_200_OK)
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(e)
|
||||||
|
|
||||||
|
|
||||||
class UpdateProfileImage(views.APIView):
|
class UpdateProfileImage(views.APIView):
|
||||||
|
|||||||
@ -45,11 +45,12 @@ ERROR_CODE = {
|
|||||||
"2019": "Either File extension or File size doesn't meet the requirements",
|
"2019": "Either File extension or File size doesn't meet the requirements",
|
||||||
"2020": "Enter valid mobile number",
|
"2020": "Enter valid mobile number",
|
||||||
"2021": "Already register",
|
"2021": "Already register",
|
||||||
"2022":"Invalid Guardian code",
|
"2022": "Invalid Guardian code",
|
||||||
"2023":"Invalid user",
|
"2023": "Invalid user",
|
||||||
"2024":"Email not verified",
|
"2024": "Email not verified",
|
||||||
"2025":"Invalid input. Expected a list of strings.",
|
"2025": "Invalid input. Expected a list of strings.",
|
||||||
"2026" : "New password should not same as old password"
|
"2026": "New password should not same as old password",
|
||||||
|
"2027": "data should contain `identityToken`"
|
||||||
}
|
}
|
||||||
SUCCESS_CODE = {
|
SUCCESS_CODE = {
|
||||||
# Success code for password
|
# Success code for password
|
||||||
|
|||||||
Reference in New Issue
Block a user