On this page

Authentication

Google

Google provider setup and usage.

Before you create a client, set up the consent screen in Google Cloud Console → Google Auth Platform. Check that you are in the right project, and for a project under an organization, the right organization.

  1. Branding: the app name, a support email, the home page, privacy policy and terms of service URLs, and the authorized domains. Verify each authorized domain in Google Search Console. The privacy policy URL here must be the same one your sign-in page links to (see Sign-in button).
  2. Audience:
    • Choose External for an app the public signs in to. Internal (offered for projects under an organization) limits sign-in to that organization's accounts.
    • Publish the app so its status reads In production. Google documents that while the status is Testing, only the test users you list can sign in; check the Audience page for the current limits before relying on a Testing setup.
  3. Data access: openid, email (.../auth/userinfo.email) and profile (.../auth/userinfo.profile). These scopes are non-sensitive, so sign-in that requests only them doesn't need Google's review for sensitive scopes.
Note

The gcloud CLI can't create this consent screen or a Web OAuth client: gcloud iam oauth-clients creates Workforce Identity Federation clients, which Google's sign-in page doesn't accept. Use the Console.

Get your Google credentials

To use Google as a social provider, you need to get your Google credentials. You can get them by creating a new project in the Google Cloud Console.

In the Google Cloud Console > Credentials > Authorized redirect URIs, make sure to set the redirect URL to http://localhost:3000/api/auth/callback/google for local development. For production, make sure to set the redirect URL as your application domain, e.g. https://example.com/api/auth/callback/google. If you change the base path of the auth routes, you should update the redirect URL accordingly.

Note

Creating Your Google OAuth Credentials

If you haven't created OAuth credentials yet, follow these step-by-step instructions:

  1. Open Google Cloud Console → Google Auth Platform → Clients (also reachable from APIs & Services → Credentials)
  2. Click Create client (or Create Credentials → OAuth client ID)
  3. Choose Web application
  4. Add your redirect URIs:
    • http://localhost:3000/api/auth/callback/google (for local development)
    • https://your-domain.com/api/auth/callback/google (for production)
  5. Copy the Client ID and Client Secret into your environment variables. You can download the client's JSON file instead of copying the secret by hand; keep it outside your repository.

These steps avoid common issues such as redirect_uri_mismatch.

The redirect URI must match exactly what the server sends, including the scheme and port. If your dev server runs on http://localhost:5173, register http://localhost:5173/api/auth/callback/google, not port 3000. Add the same origin under Authorized JavaScript origins. A newly added URI can take a few minutes before Google accepts it.

Configure the provider

To configure the provider, you need to pass the clientId and clientSecret to socialProviders.google in your auth configuration.

auth.ts
import { entroAuth } from "@entro314labs/entro-auth"

export const auth = entroAuth({
    baseURL: process.env.ENTRO_AUTH_URL,
    socialProviders: {
        google: { 
            clientId: process.env.GOOGLE_CLIENT_ID as string,
            clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
        },
    },
})
Warning

Important: Set Your Base URL

You must configure the baseURL to avoid redirect_uri_mismatch errors. Entro Auth uses this to construct the OAuth callback URL sent to Google.

Option 1: Environment Variable (Recommended)

Add to your .env file:

ENTRO_AUTH_URL=https://your-domain.com

Option 2: Explicit Configuration

Pass baseURL directly in the auth config as shown above. Without this, the callback URL may default to localhost, causing Google OAuth to fail in production.

Usage

Sign In with Google

To sign in with Google, you can use the signIn.social function provided by the client. The signIn function takes an object with the following properties:

  • provider: The provider to use. It should be set to google.
auth-client.ts
import { createAuthClient } from "@entro314labs/entro-auth/client";
const authClient = createAuthClient();

const signIn = async () => {
  const data = await authClient.signIn.social({
    provider: "google",
  });
};

Sign In with Google With ID Token

To sign in with Google using the ID Token, you can use the signIn.social function to pass the ID Token.

This is useful when you have the ID Token from Google on the client-side and want to use it to sign in on the server.

Note

If ID token is provided no redirection will happen, and the user will be signed in directly.

auth-client.ts
const data = await authClient.signIn.social({
    provider: "google",
    idToken: {
        token: // Google ID Token,
        accessToken: // Google Access Token
    }
})
Note

If you want to use google one tap, you can use the One Tap Plugin guide.

Cross-Platform Sign In (Web, iOS, Android)

Google issues a separate Client ID per platform in the same Google Cloud project. Pass an array to clientId to accept ID tokens from any of them. See clientId for the shared provider-option semantics.

auth.ts
socialProviders: {
    google: {
        clientId: [ 
            process.env.GOOGLE_WEB_CLIENT_ID as string,
            process.env.GOOGLE_IOS_CLIENT_ID as string,
            process.env.GOOGLE_ANDROID_CLIENT_ID as string,
        ],
        clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    },
}

Your mobile app signs in with the native Google SDK and forwards the ID token:

Mobile app
const { idToken, accessToken } = await GoogleSignin.signIn();

await authClient.signIn.social({
    provider: "google",
    idToken: { token: idToken, accessToken },
});

Each native platform needs its own OAuth client in the same Google Cloud project as the web client:

  • Android: one Android client per signing certificate that ships builds, each with your package name (the Gradle applicationId) and the certificate's SHA-1 fingerprint:
    • the Play App Signing key, for builds installed from Google Play. Its SHA-1 is shown in Play Console → your app → Test and release → App integrity → App signing;
    • the upload key, for builds you install directly (keytool -list -v -keystore <upload.keystore> -alias <alias> prints it);
    • the debug key, for local development (keytool -list -v -keystore ~/.android/debug.keystore -alias androiddebugkey -storepass android).
  • iOS: an iOS client with your bundle ID. Add its reversed client ID as a URL scheme in the app.

Which client ID ends up in the token's aud depends on how the native SDK is configured (on Android it is usually the web client ID passed as the server client ID). This is not confirmed for every setup: decode one real token and check its aud before narrowing the clientId list.

Note

The array only expands ID token audience verification. The authorization code flow still uses the first entry paired with the single clientSecret and redirectURI, so those cannot vary per platform within one provider block.

Restrict Sign-In to Google Workspace

Set hd on the Google provider to require a verified Google Workspace hosted-domain claim. Google also receives this value as an account-selection hint, but Entro Auth enforces the returned ID token/profile claim after Google signs the response.

auth.ts
socialProviders: {
    google: {
        clientId: process.env.GOOGLE_CLIENT_ID as string,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
        hd: "company.com",
    },
}

Set hd: "*" to allow any Google Workspace hosted domain. Tokens with no hd claim are rejected whenever hd is configured.

Warning

A custom getUserInfo callback replaces Google's built-in callback-path hd check. Validate the claim from a trusted provider response and return null when it is missing or does not match. Direct ID-token sign-in follows the provider's ID-token verification path, while Google One Tap enforces the configured hd separately.

Always ask to select an account

If you want to always ask the user to select an account, you pass the prompt parameter to the provider, setting it to select_account.

socialProviders: {
    google: {
        prompt: "select_account",
        clientId: process.env.GOOGLE_CLIENT_ID as string,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
    },
}

Requesting Additional Google Scopes

If your application needs additional Google scopes after the user has already signed up (e.g., for Google Drive, Gmail, or other Google services), you can request them using the linkSocial method with the same Google provider.

auth-client.ts
const requestGoogleDriveAccess = async () => {
  await authClient.linkSocial({
    provider: "google",
    scopes: ["https://www.googleapis.com/auth/drive.file"],
  });
};

// Example usage in a React component
return (
  <button onClick={requestGoogleDriveAccess}>
    Add Google Drive Permissions
  </button>
);

This will trigger a new OAuth flow that requests the additional scopes. After completion, your account will have the new scope in the database, and the access token will give you access to the requested Google APIs.

Note

Ensure you're using Entro Auth version 1.2.7 or later to avoid "Social account already linked" errors when requesting additional scopes from the same provider.

Disable incremental authorization

By default, Entro Auth sends include_granted_scopes=true to Google's authorization endpoint so newly issued access tokens cover scopes from prior grants in addition to the ones requested for the current flow. Set includeGrantedScopes: false if each OAuth flow should request only its own scopes.

socialProviders: {
    google: {
        clientId: process.env.GOOGLE_CLIENT_ID as string,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
        includeGrantedScopes: false,
    },
}

Call-time additionalParams.include_granted_scopes still follows the general OAuth parameter precedence rules and can override this provider default for a single flow.

See Google's Incremental authorization docs for the underlying parameter behavior.

Always get refresh token

Google only issues a refresh token the first time a user consents to your app. If the user has already authorized your app, subsequent OAuth flows will only return an access token, not a refresh token.

To always get a refresh token, you can set the accessType to offline, and prompt to select_account consent in the provider options.

socialProviders: {
    google: {
        clientId: process.env.GOOGLE_CLIENT_ID as string,
        clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
        accessType: "offline",
        prompt: "select_account consent",
    },
}
Note

Revoking Access: If you want to get a new refresh token for a user who has already authorized your app, you must have them revoke your app's access in their Google account settings, then re-authorize.

Restricting to a Google Workspace domain

To limit sign-in to accounts in a specific Google Workspace domain, forward the hd parameter on the call. This mirrors Google's hd authorization-URL parameter and is resolved per request:

await authClient.signIn.social({
  provider: "google",
  additionalParams: { hd: "example.com" },
});

You can also set hd at the provider config level if the restriction is static for your application.

Verify your setup

Run these checks in order. Each one proves only its own part, so don't treat the setup as working until the last one passes.

  1. The provider is configured. POST /api/auth/sign-in/social with the body {"provider": "google", "disableRedirect": true} returns a url, not a 404 PROVIDER_NOT_FOUND. If your UI decides at build time which buttons to show, redeploy after changing the provider's environment variables.
  2. The authorization URL. The url has your client_id and redirect_uri=<baseURL>/api/auth/callback/google. This proves what the server sends, not that Google accepts it.
  3. Google's page. Open the url in a browser. Google's sign-in page appears, not redirect_uri_mismatch. This proves the client ID and the redirect URI are registered. A mismatch shows up here, at the authorize step: check baseURL, the port, and whether the URI was added only minutes ago.
  4. A real sign-in. Sign in with a Google account and land on your callbackURL with a session. Only this proves the client secret, which is used only in the code exchange after the user signs in.
  5. The publishing status. Check the Audience page. A sign-in that works while the app is in Testing may work only for listed test users.

Sign-in button

Follow Google's Sign in with Google branding guidelines, adapted to your design system:

  • Use the official standard-colour "G" logo from Google's assets, unmodified: don't change its size, colour or padding, and don't use a monochrome or redrawn "G".
  • Use an approved title: "Sign in with Google", "Sign up with Google" or "Continue with Google".
  • Use one of Google's themes: light, dark or neutral. You may scale the button or style it to match your app, as long as the "G" keeps its size, padding and colour.
  • Make the Google button at least as prominent as the other third-party options, with about the same size and visual weight.

Link your privacy policy and terms of service next to the button. The privacy policy URL must match the one on the consent screen's Branding page.

Google's guidelines change; re-read them before shipping a new button.