Skip to content

Commit

Permalink
feat: adding back google auth
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0elliot committed Jul 24, 2023
1 parent 58c09a1 commit 7c81519
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 10,401 deletions.
8 changes: 8 additions & 0 deletions backend/authentication/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,26 @@
MeAPIView,
SetWalletAddressAPIView,
github_login,
google_login,
GoogleLoginCallbackView
)

urlpatterns = [
path("login", LoginAPIView.as_view(), name="login"),
path("logout", LogoutAPIView.as_view(), name="logout"),
path("token/refresh", TokenRefreshView.as_view(), name="token_refresh"),
path("github", github_login, name="oauth_github"),
path("google", google_login, name="oauth_google"),
path(
"github-callback",
GithubLoginCallbackView.as_view(),
name="oauth_github_callback",
),
path(
"google-callback",
GoogleLoginCallbackView.as_view(),
name="oauth_google_callback",
),
path(
"me",
MeAPIView.as_view(),
Expand Down
75 changes: 75 additions & 0 deletions backend/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,32 @@ def get(self, request):

return Response({"message": "success"}, status=status.HTTP_200_OK)

def google_login(request):
# for development
REPLACEMENT_DOMAIN = "localhost:3000"

if settings.DEMO_INSTANCE:
REPLACEMENT_DOMAIN = settings.PROTOTYPE_DOMAIN

REPLACEMENT_DOMAIN += "/api"

current_domain = request.get_host()

redirect_uri = request.build_absolute_uri(reverse("oauth_google_callback")).replace(
current_domain, REPLACEMENT_DOMAIN
)

if settings.DEMO_INSTANCE:
redirect_uri = redirect_uri.replace("http://", "https://")

try:
return oauth.google.authorize_redirect(request, redirect_uri)
except AttributeError as error:
if "No such client: " in str(error):
raise AuthenticationFailed("Google OAuth is not configured.")
raise error


def github_login(request):
# for development
REPLACEMENT_DOMAIN = "localhost:3000"
Expand Down Expand Up @@ -206,3 +232,52 @@ def post(self, request):
f"{settings.FRONTEND_URL}auth/social?access"
f"={access_token}&refresh={refresh_token}&username={user.username}"
)

class GoogleLoginCallbackView(APIView):
@staticmethod
def validate_and_return_user(request):
try:
token = oauth.google.authorize_access_token(request)
except (
OAuthError,
OAuth2Error,
):
# Not giving out the actual error as we risk exposing the client secret
raise AuthenticationFailed("OAuth authentication error.")

user = token.get("userinfo")
user_email = user.get("email")
user_name = user.get("name")
# image = user.get("image").get("url")

try:
return User.objects.get(email=user_email)
except User.DoesNotExist:
logging.info("[Google Oauth] User does not exist. Creating new one.")
return User.objects.create_user(
email=user_email,
username=user_name,
password=None,
auth_provider="google",
# avatar=image,
)

def get(self, request):
return self.post(request)

def post(self, request):
user = self.validate_and_return_user(request)
print(user)

tokens = user.tokens()
access_token = tokens.get("access")
refresh_token = tokens.get("refresh")

# Uncomment this for local testing
return redirect(
f"{settings.FRONTEND_URL}/auth/social?access"
f"={access_token}&refresh={refresh_token}&username={user.username}"
)
# return redirect(self.request.build_absolute_uri(f"/login?token={token}"))


Loading

0 comments on commit 7c81519

Please sign in to comment.