# Google sign-in

Source: https://cubibase.com/docs/auth/google

Also available: [Markdown version](https://cubibase.com/docs/auth/google.md) · [llms.txt](https://cubibase.com/llms.txt) · [full site content](https://cubibase.com/llms-full.txt)

Create an OAuth client in the Google Cloud Console, paste its credentials into CubiBase, and register the callback URL. Two flows: browser redirect, or a Google ID token your app already holds.

## Configure it step by step

Everything happens in two places: console.cloud.google.com for the Google side, and Console → project → Auth → Providers → Google for the CubiBase side.

1. **Google: select or create a project** — Open console.cloud.google.com and pick the Google Cloud project for your app.
2. **Google: configure the OAuth consent screen** — APIs & Services → OAuth consent screen. Choose External, fill in the app name and the support and developer contact emails, and add the scopes openid, email, and profile. While the app is in Testing, only listed test users can sign in; publish to production when you are ready for everyone.
3. **Google: create an OAuth client** — APIs & Services → Credentials → Create credentials → OAuth client ID → Web application.
4. **Google: register the redirect URI** — Copy the Authorized redirect URI that the CubiBase Google section displays — it looks like https://{slug}.api.cubibase.com/auth/v1/callback?provider=google&project={projectId}. Paste it exactly, query parameters included.
5. **CubiBase: enable the provider** — In Auth → Providers → Google, tick Enable Google sign-in, paste the Client ID and Client secret, and save. The secret is stored encrypted and never shown again; the section shows a Secret stored badge instead.
6. **CubiBase: allowlist your app’s URLs** — In the same form, fill the App redirect allowlist with the URLs in your app that should receive the session — one per line; a trailing * allows prefix matches. Loopback HTTP redirect URLs are always allowed for native and local development.

> **warning**: The redirect URI must match character for character, including the `provider` and `project` query parameters. A mismatch is the most common cause of Google’s “redirect_uri_mismatch” error.

## The browser redirect flow

**Google sign-in redirect flow, illustration.** Your app asks for a consent url → GET /auth/v1/authorize?provider=google → CubiBase Auth (single-use state) → browser opens Google consent → Google (user consents) → redirect to /auth/v1/callback → CubiBase callback (verifies code + state) → 302 · your redirect_to with #access_token=… refresh_token=… expires_in=3600.

Illustration. The session rides in the URL fragment, which browsers never send to servers.

Your app starts the flow with `GET /auth/v1/authorize?provider=google&redirect_to={your callback URL}`, sending the publishable key. The response contains a `url`: send the browser there.

**1 · Start the flow**

```http
GET https://example-project.api.cubibase.com/auth/v1/authorize?provider=google&redirect_to=https%3A%2F%2Fapp.example.com%2Fauth%2Fcallback
apikey: cb_pub_EXAMPLE_ONLY
```

**2 · Response: open this url**

```json
{
  "url": "https://accounts.google.com/o/oauth2/v2/auth?client_id=…&redirect_uri=…&response_type=code&scope=openid%20email%20profile&state=…",
  "state": "<SINGLE_USE_STATE>",
  "expiresAt": "2026-11-20T10:10:00.000Z"
}
```

Google shows its consent screen and redirects back to the CubiBase callback, which verifies the response and redirects the browser to your `redirect_to` with the session in the URL fragment (the part after `#`).

**3 · Your callback page reads the fragment**

```text
https://app.example.com/auth/callback#access_token=<ACCESS_JWT>&refresh_token=<REFRESH_TOKEN>&token_type=Bearer&expires_in=3600&expires_at=2026-11-20T11%3A00%3A00.000Z
```

> **tip**: Read the tokens from `location.hash` on your callback page and store them like any CubiBase session. Fragments are never sent to servers or access logs. If the user denies consent or something fails, the same redirect carries an `error` query parameter instead.

## The ID-token flow (mobile and web SDKs)

If your app already obtains a Google ID token — Google Sign-In on Android, iOS, or the web — skip the redirect flow and POST it to `/auth/v1/token?grant_type=id_token` with the publishable key.

**Verify a Google ID token**

```http
POST https://example-project.api.cubibase.com/auth/v1/token?grant_type=id_token
apikey: cb_pub_EXAMPLE_ONLY
Content-Type: application/json

{
  "provider": "google",
  "idToken": "<GOOGLE_ID_TOKEN>"
}
```

CubiBase verifies the token’s RS256 signature against Google’s published keys, the issuer, the audience (your Client ID), and the expiry, then returns a normal session. Pass a `nonce` to bind the token to your sign-in attempt.

## Identity rules (both flows)

| Situation | What happens |
| --- | --- |
| Google account has signed in before | Signs straight in |
| Request carries an end-user bearer token | The Google identity links to that user — the anonymous upgrade path |
| Verified Google email matches an existing user | The identity attaches to that account |
| New Google account | Signs up a new user (the signup switch applies) |
| User has a verified factor, project requires MFA | `403 mfa_required` — Google alone does not satisfy MFA |

> **tip**: Converting an anonymous visitor? Send their access token as `Authorization: Bearer …` on the authorize request (or on the id_token grant). The Google identity then links to that same user — no password involved. Step by step in Anonymous users → Path 2.

> **note**: Linking checks the email: a Google account whose email differs from the signed-in user’s email is refused with `409 email_mismatch`. Anonymous users have no email yet, so any Google account links and converts them. A Google account already linked to another user returns `409 identity_exists`.

## Additional resources

- [Anonymous users](https://cubibase.com/docs/auth/anonymous) The conversion path Google linking completes.
- [Sessions and tokens](https://cubibase.com/docs/auth/sessions) The session both flows return.
- [Multi-factor (TOTP)](https://cubibase.com/docs/auth/mfa) Why Google sign-in still asks for a code sometimes.
