Skip to content

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

Open
astarrh wants to merge 99 commits into
devbaji:masterfrom
astarrh:master
Open

Add support for OAuth 2.0 authorization code flow without requiring a custom button.#78
astarrh wants to merge 99 commits into
devbaji:masterfrom
astarrh:master

Conversation

@astarrh

@astarrh astarrh commented Jan 24, 2025

Copy link
Copy Markdown

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 29 commits August 13, 2024 22:41
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.

@devbaji devbaji left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you look into the changes? I can see 43 file changes. Is this correct?

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