mirror of
https://github.com/HamzaSha1/zod-backend.git
synced 2025-07-16 10:26:16 +00:00
jira-5 google login
This commit is contained in:
@ -29,6 +29,13 @@ class GoogleSignInSerializer(serializers.Serializer):
|
|||||||
email=self.validated_data['email'])
|
email=self.validated_data['email'])
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
class GoogleLoginSerializer1(serializers.Serializer):
|
||||||
|
access_token = serializers.CharField(max_length=5000, required=True)
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
"""meta class"""
|
||||||
|
fields = ('access_token',)
|
||||||
|
|
||||||
class UpdateGuardianImageSerializer(serializers.ModelSerializer):
|
class UpdateGuardianImageSerializer(serializers.ModelSerializer):
|
||||||
"""Reset Password after verification"""
|
"""Reset Password after verification"""
|
||||||
class Meta(object):
|
class Meta(object):
|
||||||
|
@ -5,14 +5,15 @@ from rest_framework.decorators import api_view
|
|||||||
"""Third party import"""
|
"""Third party import"""
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp,
|
from .views import (UserLogin, SendPhoneOtp, UserPhoneVerification, UserEmailVerification, ReSendEmailOtp,
|
||||||
ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage)
|
ForgotPasswordAPIView, ResetPasswordAPIView, ChangePasswordAPIView, UpdateProfileImage,
|
||||||
|
GoogleLoginViewSet1)
|
||||||
"""Router"""
|
"""Router"""
|
||||||
router = routers.SimpleRouter()
|
router = routers.SimpleRouter()
|
||||||
|
|
||||||
"""API End points with router"""
|
"""API End points with router"""
|
||||||
router.register('user', UserLogin, basename='user')
|
router.register('user', UserLogin, basename='user')
|
||||||
router.register('admin', UserLogin, basename='admin')
|
router.register('admin', UserLogin, basename='admin')
|
||||||
# router.register('google-login', GoogleLoginAPIViewset, basename='admin')
|
router.register('google-login', GoogleLoginViewSet1, basename='admin')
|
||||||
router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp')
|
router.register('send-phone-otp', SendPhoneOtp, basename='send-phone-otp')
|
||||||
router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification')
|
router.register('user-phone-verification', UserPhoneVerification, basename='user-phone-verification')
|
||||||
router.register('user-email-verification', UserEmailVerification, basename='user-email-verification')
|
router.register('user-email-verification', UserEmailVerification, basename='user-email-verification')
|
||||||
|
@ -9,7 +9,7 @@ from account.models import UserProfile, UserPhoneOtp, UserEmailOtp
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer,
|
from .serializers import (SuperUserSerializer, GuardianSerializer, JuniorSerializer, EmailVerificationSerializer,
|
||||||
ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer,
|
ForgotPasswordSerializer, ResetPasswordSerializer, ChangePasswordSerializer,
|
||||||
GoogleSignInSerializer, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer)
|
GoogleLoginSerializer1, UpdateGuardianImageSerializer, UpdateJuniorProfileImageSerializer)
|
||||||
from rest_framework_simplejwt.tokens import RefreshToken
|
from rest_framework_simplejwt.tokens import RefreshToken
|
||||||
from base.messages import ERROR_CODE, SUCCESS_CODE
|
from base.messages import ERROR_CODE, SUCCESS_CODE
|
||||||
from guardian.tasks import generate_otp
|
from guardian.tasks import generate_otp
|
||||||
@ -22,6 +22,73 @@ from rest_framework.response import Response
|
|||||||
from rest_framework.permissions import IsAuthenticated
|
from rest_framework.permissions import IsAuthenticated
|
||||||
from templated_email import send_templated_mail
|
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):
|
# class GoogleLoginAPIViewset(viewsets.ModelViewSet):
|
||||||
# """Google Login"""
|
# """Google Login"""
|
||||||
# serializer_class = GoogleSignInSerializer
|
# serializer_class = GoogleSignInSerializer
|
||||||
|
@ -56,6 +56,7 @@ INSTALLED_APPS = [
|
|||||||
'account',
|
'account',
|
||||||
'junior',
|
'junior',
|
||||||
'guardian',
|
'guardian',
|
||||||
|
# 'social_django'
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
@ -171,6 +172,21 @@ CORS_ALLOW_HEADERS = (
|
|||||||
"""Static files (CSS, JavaScript, Images)
|
"""Static files (CSS, JavaScript, Images)
|
||||||
https://docs.djangoproject.com/en/3.0/howto/static-files/"""
|
https://docs.djangoproject.com/en/3.0/howto/static-files/"""
|
||||||
|
|
||||||
|
AUTHENTICATION_BACKENDS = [
|
||||||
|
'social_core.backends.google.GoogleOAuth2',
|
||||||
|
'django.contrib.auth.backends.ModelBackend',
|
||||||
|
]
|
||||||
|
|
||||||
|
LOGIN_URL = 'login'
|
||||||
|
LOGIN_REDIRECT_URL = 'home'
|
||||||
|
LOGOUT_URL = 'logout'
|
||||||
|
LOGOUT_REDIRECT_URL = 'login'
|
||||||
|
|
||||||
|
# SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
|
||||||
|
# SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
|
||||||
|
|
||||||
|
GOOGLE_CLIENT_ID = "182276566528-hlbjncs19fo502jposod6kft2p9k4grk.apps.googleusercontent.com"
|
||||||
|
GOOGLE_CLIENT_SECRET = "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"
|
||||||
|
|
||||||
# EMAIL_BACKEND = os.getenv('EMAIL_BACKEND')
|
# EMAIL_BACKEND = os.getenv('EMAIL_BACKEND')
|
||||||
# EMAIL_HOST = os.getenv('EMAIL_HOST')
|
# EMAIL_HOST = os.getenv('EMAIL_HOST')
|
||||||
|
Reference in New Issue
Block a user