Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for OAuth 2.0 authorization code flow without requiring a custom button. #78

Open
wants to merge 99 commits into
base: master
Choose a base branch
from

Conversation

astarrh
Copy link

@astarrh astarrh commented Jan 24, 2025

This PR introduces an authMode prop to enable support for the OAuth 2.0 authorization code flow in the GoogleLogin component in addition to the existing credential-based flow. This is accomplished by using the GIS button style and replacing the click listener with logic from google.accounts.oauth2.initCodeClient.:

if (button) {
  window.google.accounts.id.renderButton(button, buttonConfig);
  const newButton = button.cloneNode(true);
  button.parentNode!.replaceChild(newButton, button);
  newButton.addEventListener("click", () => {
    oauthClient.requestCode(); // Custom behavior
  });
}

Default behavior remains unchanged, but developers may toggle the new behavior with the authMode prop:

<script setup>
const callback = (response) => {
  // This callback will be triggered when the user logs in
  // and receives an authorization code
  console.log("Authorization code:", response.code);
};
</script>

<template>
  <GoogleLogin :callback="callback" :authMode="'code'" />
</template>

Default behavior may be set explicitly with authMode="'credential'". This module doesn't contain a testing package, but on my system it is working as expected and is backwards compatible.

Key Changes in the PR:
1. Added authMode: "code" to support google.accounts.oauth2.initCodeClient.
2. Updated the GoogleLogin component to dynamically render buttons based on the selected flow.
3. Ensured backward compatibility with existing credential behavior.
4. Added documentation and examples for the new authMode prop.

Closes #71

devbaji and others added 30 commits August 13, 2024 22:37
Modified utils.ts to allow dynamically passing custom scopes via options.
Updated types.ts to reflect changes in the type definitions.
- Introduced the `authMode` prop to allow switching between "credential" and "code" flows.
- Render GIS-styled buttons for consistent styling in both flows.
- Added logic to override GIS default behavior when `authMode` is set to "code".
- Ensured compatibility with OAuth 2.0 authorization code flow by using `google.accounts.oauth2.initCodeClient`.
- Improved flexibility for developers needing custom authentication/authorization logic.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Is it possible to use an Auth Code flow but no custom button?
8 participants