Skip to content

Instantly share code, notes, and snippets.

@HereOrCode
Last active February 1, 2025 10:46
Show Gist options
  • Save HereOrCode/4a1516ff7fabd575c079c6b251b8e149 to your computer and use it in GitHub Desktop.
Save HereOrCode/4a1516ff7fabd575c079c6b251b8e149 to your computer and use it in GitHub Desktop.
Chrome Extension: Authenticate users with Google

Code

export async function loginToGoogle() {
  const { code } = getAuthParams(
    (await chrome.identity.launchWebAuthFlow({
      interactive: true,
      url: `https://accounts.google.com/o/oauth2/v2/auth?${new URLSearchParams({
        redirect_uri: chrome.identity.getRedirectURL(import.meta.env.VITE_CHROME_EXT_ID),
        client_id: import.meta.env.VITE_GOOGLE_CLIENT_ID,
        access_type: "offline",
        response_type: "code",
        prompt: "consent",
        scope: [
          "https://www.googleapis.com/auth/userinfo.profile",
          "https://www.googleapis.com/auth/userinfo.email",
          "https://www.googleapis.com/auth/user.birthday.read",
          "https://www.googleapis.com/auth/user.gender.read"
        ].join(" ")
      })}`
    })) as string
  );
  const authData = await getGoogleAuthData({ code });
  return getGoogleProfile(authData);
}

Document

OAuth 2.0: authenticate users with Google  |  Chrome Extensions  |  Chrome for Developers

https://developer.chrome.com/docs/extensions/how-to/integrate/oauth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment