Skip to content

Commit

Permalink
adding google signup and signin - no test
Browse files Browse the repository at this point in the history
  • Loading branch information
yuvrajrathva committed Sep 22, 2023
1 parent b97759d commit b0911ea
Show file tree
Hide file tree
Showing 5 changed files with 475 additions and 6 deletions.
23 changes: 23 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,27 @@ POSTGRES_DB_PASSWORD=festpassword
POSTGRES_DB_HOST=localhost
POSTGRES_DB_PORT=5432

BASE_FRONTEND_URL=http://localhost:3000
BASE_BACKEND_URL=http://localhost:8000

DEFAULT_PROFILE_IMAGE_URL="https://as1.ftcdn.net/v2/jpg/03/46/83/96/1000_F_346839683_6nAPzbhpSkIpb8pmAwufkC7c5eD7wYws.jpg"

# Razorpay
RAZORPAY_KEY_ID=
RAZORPAY_KEY_SECRET=

# Google Sign-in
DJANGO_GOOGLE_OAUTH2_CLIENT_ID=
DJANGO_GOOGLE_OAUTH2_CLIENT_SECRET=
APP_SECRET=

# Email configs
EMAIL_HOST=""
EMAIL_PORT=""
EMAIL_HOST_USER=""
EMAIL_HOST_PASSWORD=""

PAYTM_MID=
PAYTM_MERCHANT_KEY=

LOG_FILE=
7 changes: 6 additions & 1 deletion accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rest_framework_simplejwt.views import (TokenObtainPairView,
TokenRefreshView)

from .views import MyObtainTokenPairView, UserProfileViewSet, UserDetailAPI, UserProfileDetailsView, RegisterUserAPIView, CookieTokenRefreshView, PreRegistrationAPIView
from .views import MyObtainTokenPairView, UserProfileViewSet, UserDetailAPI, UserProfileDetailsView, RegisterUserAPIView, CookieTokenRefreshView, PreRegistrationAPIView, GoogleRegisterView, GoogleLoginView, GoogleRegisterViewApp, GoogleLoginViewApp, LogoutView

router = routers.DefaultRouter()
router.register(r'pre-register', PreRegistrationAPIView)
Expand All @@ -12,6 +12,11 @@
path('', include(router.urls)),
path('register/', RegisterUserAPIView.as_view(), name="register"),
path('login/', MyObtainTokenPairView.as_view(), name='login'),
path('register/google/', GoogleRegisterView.as_view(), name='google-register'),
path('register/google/app/', GoogleRegisterViewApp.as_view(), name='google-register-app'),
path('login/google/', GoogleLoginView.as_view(), name='google-login'),
path('login/google/app/', GoogleLoginViewApp.as_view(), name='google-login-app'),
path('logout/', LogoutView.as_view(), name='logout'),
path('profile/', UserProfileViewSet.as_view(), name='profile'),
path('refresh/', CookieTokenRefreshView.as_view(), name='refresh'),
path('user-details/', UserDetailAPI.as_view()),
Expand Down
42 changes: 42 additions & 0 deletions accounts/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import requests
from django.conf import settings
from typing import Dict, Any
from django.core.exceptions import ValidationError
from rest_framework_simplejwt.tokens import RefreshToken


GOOGLE_ID_TOKEN_INFO_URL = 'https://www.googleapis.com/oauth2/v3/tokeninfo'
GOOGLE_ACCESS_TOKEN_OBTAIN_URL = 'https://oauth2.googleapis.com/token'
GOOGLE_USER_INFO_URL = 'https://www.googleapis.com/oauth2/v3/userinfo'


def get_tokens_for_user(user):
refresh = RefreshToken.for_user(user)
return {
Expand All @@ -9,5 +18,38 @@ def get_tokens_for_user(user):
}


def google_get_access_token(*, code: str, redirect_uri: str) -> str:
# Reference: https://developers.google.com/identity/protocols/oauth2/web-server#obtainingaccesstokens
data = {
'code': code,
'client_id': settings.GOOGLE_OAUTH2_CLIENT_ID,
'client_secret': settings.GOOGLE_OAUTH2_CLIENT_SECRET,
'redirect_uri': redirect_uri,
'grant_type': 'authorization_code'
}

response = requests.post(GOOGLE_ACCESS_TOKEN_OBTAIN_URL, data=data)

if not response.ok:
raise ValidationError('Failed to obtain access token from Google.')

access_token = response.json()['access_token']

return access_token


def google_get_user_info(*, access_token: str) -> Dict[str, Any]:
# Reference: https://developers.google.com/identity/protocols/oauth2/web-server#callinganapi
response = requests.get(
GOOGLE_USER_INFO_URL,
params={'access_token': access_token}
)

if not response.ok:
raise ValidationError('Failed to obtain user info from Google.')

return response.json()


def generate_registration_code(name, lastRegCode):
return f"{name[:3].upper()}-{lastRegCode+3155}"
Loading

0 comments on commit b0911ea

Please sign in to comment.