mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-16 02:16:16 +00:00
jira-5 google login
This commit is contained in:
@ -9,7 +9,7 @@ from account.models import UserProfile, UserPhoneOtp, UserEmailOtp
|
||||
from django.contrib.auth.models import User
|
||||
from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer,
|
||||
ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer,
|
||||
GoogleSignInSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer)
|
||||
GoogleLoginSerializer1, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer)
|
||||
from rest_framework_simplejwt.tokens import RefreshToken
|
||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||
from guardian.tasks import generate_otp
|
||||
@ -22,6 +22,73 @@ 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
|
||||
|
||||
|
||||
class GoogleLoginMixin:
|
||||
def google_login(self, request):
|
||||
access_token = request.data.get('access_token')
|
||||
user_type = request.data.get('user_type')
|
||||
if not access_token:
|
||||
return Response({'error': 'Access token is required.'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
try:
|
||||
# Validate the access token and obtain the user's email and name
|
||||
credentials = google.oauth2.credentials.Credentials.from_authorized_user_info(
|
||||
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,
|
||||
}
|
||||
)
|
||||
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()
|
||||
email = user_info['email']
|
||||
name = user_info['name']
|
||||
profile_picture = user_info['picture']
|
||||
except Exception as e:
|
||||
return Response({'error': str(e)}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
# Check if the user exists in your database or create a new user
|
||||
# ...
|
||||
if User.objects.filter(email__iexact=email).exists():
|
||||
print("00000000000")
|
||||
return custom_response(SUCCESS_CODE['3003'], response_status=status.HTTP_200_OK)
|
||||
|
||||
if not User.objects.filter(email__iexact=email).exists():
|
||||
print("999999999999999")
|
||||
user_obj = User.objects.create(username=email,
|
||||
email=email)
|
||||
if str(user_type) == '1':
|
||||
Junior.objects.create(auth=user_obj)
|
||||
|
||||
|
||||
# Return a JSON response with the user's email and name
|
||||
return Response({'token': "get_token()", 'name': name, 'email': email, 'profile_picture': profile_picture})
|
||||
|
||||
class GoogleLoginViewSet1(GoogleLoginMixin, viewsets.GenericViewSet):
|
||||
serializer_class = GoogleLoginSerializer1
|
||||
|
||||
def create(self, request):
|
||||
serializer = self.get_serializer(data=request.data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
print("88888888888888888888888888")
|
||||
return self.google_login(request)
|
||||
|
||||
# class GoogleLoginAPIViewset(viewsets.ModelViewSet):
|
||||
# """Google Login"""
|
||||
# serializer_class = GoogleSignInSerializer
|
||||
|
Reference in New Issue
Block a user