Google OAuth 2.0

Implement "Login with Google"

With oidc-spa, you would typically use an OIDC Provider like Keycloak or Auth0 to centralize authentication and configure Google as an identity provider within Keycloak. This allows users to select "Google" as a login option.

That being said, if you really want to, you can configure oidc-spa directly with Google, as demonstrated in the following video:

Google Cloud Console Configuration

To set up authentication via Google, follow these steps in the Google Cloud Console:

  1. Navigate to Google Cloud Platform Console.

  2. Go to API & ServicesCredentials.

  3. Click Create CredentialsOAuth Client ID.

  4. Choose Application Type: Web Application.

  5. Set the Authorized Redirect URIs:

    • https://my-app.com/ and http://localhost:5173/ (Ensure the trailing slash is included).

    • If your app is hosted under a subpath (e.g., /dashboard), set:

      • https://my-app.com/dashboard/

      • http://localhost:5173/dashboard/

    • 5173 is Vite's default development server port—adjust as needed.

  6. Set the Authorized JavaScript Origins to match the origins of your redirect URIs.

Subtituing the Access Token by the ID Token

Here’s how to configure oidc-spa to work with Google:

import { createOidc } from "oidc-spa";

export const prOidc = createOidc({
    issuerUri: "https://accounts.google.com",
    clientId: "928024164279-ifjvmsffi64slkk81h3gmoh7p03ev68k.apps.googleusercontent.com",
    homeUrl: import.meta.env.BASE_URL,
    scope: ["profile", "email",
    /*Obtionally more scopes to get more infos in the id token like "https://www.googleapis.com/auth/youtube.readonly", ...*/
    ],
    __unsafe_clientSecret: "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",
    __unsafe_useIdTokenAsAccessToken: true
});

Testing

npx degit https://github.com/keycloakify/oidc-spa/examples/tanstack-router-file-based oidc-spa-tanstack-router
cd oidc-spa-tanstack-router
cp .env.local.sample .env.local

# Edit .env.local, uncomment the Google section and comment the Keycloak section
# replace the values by your own.

yarn
yarn dev

Last updated

Was this helpful?