jira-5 google login and apple login

This commit is contained in:
jain
2023-06-30 21:25:43 +05:30
parent 79ac140ddd
commit d2498f82ad
5 changed files with 139 additions and 54 deletions

View File

@ -2,6 +2,7 @@ from rest_framework import viewsets, status, views
from rest_framework.decorators import action
import random
import logging
import jwt
from django.contrib.auth import authenticate, login
from guardian.models import Guardian
from junior.models import Junior
@ -13,22 +14,17 @@ from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSeriali
from rest_framework_simplejwt.tokens import RefreshToken
from base.messages import ERROR_CODE, SUCCESS_CODE
from guardian.tasks import generate_otp
from django.conf import settings
from account.utils import send_otp_email
from account.utils import custom_response, custom_error_response
from django.core.mail import EmailMessage
from django.core.mail import send_mail
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from templated_email import send_templated_mail
import google.oauth2.credentials
import google.auth.transport.requests
from rest_framework import status
from rest_framework.response import Response
import requests
from django.conf import settings
# from apps.accounts.utility import get_token
from .utils import get_token
class GoogleLoginMixin:
@ -65,9 +61,18 @@ class GoogleLoginMixin:
# Check if the user exists in your database or create a new user
# ...
if User.objects.filter(email__iexact=email).exists():
user_data = User.objects.filter(email__iexact=email)
if user_data.exists():
print("00000000000")
return custom_response(SUCCESS_CODE['3003'], response_status=status.HTTP_200_OK)
if str(user_type) == '1':
junior_query = Junior.objects.filter(auth=user_data.last()).last()
serializer = JuniorSerializer(junior_query)
if str(user_type) == '2':
guardian_query = Guardian.objects.filter(user=user_data.last()).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)
if not User.objects.filter(email__iexact=email).exists():
print("999999999999999")
@ -80,9 +85,11 @@ class GoogleLoginMixin:
# Return a JSON response with the user's email and name
return Response({'token': "get_token()", 'name': name, 'email': email, 'profile_picture': profile_picture})
return custom_response(SUCCESS_CODE['3003'], {'auth_token': get_token(), 'name': name, 'email': email,
'profile_picture': profile_picture, "user_type":user_type},
response_status=status.HTTP_200_OK)
class GoogleLoginViewSet1(GoogleLoginMixin, viewsets.GenericViewSet):
class GoogleLoginViewSet(GoogleLoginMixin, viewsets.GenericViewSet):
serializer_class = GoogleLoginSerializer1
def create(self, request):
@ -91,27 +98,46 @@ class GoogleLoginViewSet1(GoogleLoginMixin, viewsets.GenericViewSet):
print("88888888888888888888888888")
return self.google_login(request)
# class GoogleLoginAPIViewset(viewsets.ModelViewSet):
# """Google Login"""
# serializer_class = GoogleSignInSerializer
#
# def create(self, request, *args, **kwargs):
# """
# Override default behaviour of create method
# """
# provider_type = []
# serializer = self.get_serializer(data=request.data)
# if serializer.is_valid(raise_exception=True):
# # provider = self.get_provider_view(request.data.get('provider'))
# # if User is not authenticated then send error message
# # if not provider.is_authenticated(request):
# # return custom_error_response({}, status.HTTP_400_BAD_REQUEST)
#
# user = serializer.save()
# if User.objects.filter(email__iexact=user.email).exists():
# print("ppppppppppppp")
# return custom_response(SUCCESS_CODE["3003"], response_status=status.HTTP_200_OK)
# return custom_response(ERROR_CODE["2002"], response_status=status.HTTP_400_BAD_REQUEST)
class SigninWithApple(views.APIView):
"""This API is for sign in with Apple for app."""
def post(self, request):
token = request.data.get("identityToken")
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)
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)
class UpdateProfileImage(views.APIView):
permission_classes = [IsAuthenticated]