# Anonymous users

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

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

Let visitors use your app before they create an account. An anonymous user is a real user with a real id — and converting them later keeps every row they wrote, with a password or with Google (no password needed).

## Enable anonymous sign-ins

1. **Open the provider settings** — Console → your project → Auth → Providers → Anonymous users.
2. **Switch on Allow anonymous sign-ins** — The setting is off by default. Turning it off later blocks new anonymous sessions but does not touch existing ones.

> **note**: Anonymous sign-ins follow the project signup switch and the signup rate limit, and anonymous users count toward the plan’s end-user quota.

## Sign a visitor in

`POST /auth/v1/signup/anonymous` needs no email or password. CubiBase creates a real user in `auth.users` with `is_anonymous` set and returns the same session shape as a normal signup.

**Anonymous sign-in · request**

```http
POST https://example-project.api.cubibase.com/auth/v1/signup/anonymous
apikey: cb_pub_EXAMPLE_ONLY
Content-Type: application/json

{
  "data": { "cart": ["item-1"] }
}
```

**201 · response (trimmed)**

```json
{
  "user": {
    "id": "3f2b8c1e-9a47-4d02-b6e5-1c8f0a3d7e29",
    "email": null,
    "isAnonymous": true,
    "providers": [],
    "mfaEnabled": false
  },
  "accessToken": "<ACCESS_JWT>",
  "refreshToken": "<REFRESH_TOKEN>",
  "tokenType": "Bearer",
  "expiresIn": 3600
}
```

The access token works like any other end-user JWT: its `sub` is the user id, so rows written with a user column already belong to that visitor.

## Convert to a real account

An anonymous visitor becomes a real account in place — same user id, same rows. There are two ways to do it, and Google never asks for a password:

| Path | What the visitor does | What you send |
| --- | --- | --- |
| Email + password | Types an email and chooses a password | `PATCH /auth/v1/user` with the anonymous bearer token, `email`, and `password` |
| Google | Clicks “Continue with Google” — Google proves who they are | The anonymous bearer token on `/auth/v1/authorize` or `grant_type=id_token` |

> **important**: With Google, the Google account is the credential: CubiBase sets the email from the verified Google profile and clears `is_anonymous` — no password is created or required. The visitor signs in with Google from then on.

- The user id does not change on either path — the rows they created stay theirs.
- Existing sessions keep working; no re-login is forced.
- Metadata is merged with the Google profile (name, picture).

## Path 1 · email and password

When the visitor is ready to register with an email, convert the account with `PATCH /auth/v1/user`, sending the anonymous bearer token plus an email and a password.

**Convert · same user id**

```http
PATCH https://example-project.api.cubibase.com/auth/v1/user
apikey: cb_pub_EXAMPLE_ONLY
Authorization: Bearer <ANONYMOUS_ACCESS_JWT>
Content-Type: application/json

{
  "email": "mina@example.com",
  "password": "<USER_PASSWORD>"
}
```

> **important**: On this endpoint conversion requires both email and password in one request (`400 conversion_requires_credentials` otherwise). A duplicate email returns `409 user_exists`. If the visitor prefers Google, use Path 2 instead — no password needed.

## Path 2 · Google (no password)

The trick is one header: send the anonymous user’s access token as `Authorization: Bearer …` on the Google sign-in request. CubiBase then links the Google identity to that user instead of creating a new one. Both Google flows support it.

### Browser redirect flow

1. **Start the flow with the anonymous bearer token** — Call GET /auth/v1/authorize?provider=google&redirect_to=… with the apikey header and the anonymous access token as the bearer. CubiBase remembers the user id inside the single-use state.
2. **The visitor consents at Google** — Open the returned url in the browser. Google shows its consent screen — no CubiBase password is involved at any point.
3. **The callback converts the account** — Google redirects back to CubiBase, which links the identity to the remembered user: the email comes from the Google profile, is_anonymous is cleared, and the user id stays the same.
4. **Store the new session** — The browser lands on your redirect_to with a fresh token pair in the URL fragment (amr: ["oauth"]). Replace the anonymous pair with it; the old anonymous session keeps working until it expires or is revoked.

**Authorize · with the anonymous bearer token**

```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
Authorization: Bearer <ANONYMOUS_ACCESS_JWT>
```

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

If your app already holds a Google ID token from Google Sign-In, post it to the id_token grant with the same anonymous bearer token. The response is a normal session for the converted user.

**Link a Google ID token · same user id**

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

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

> **warning**: Without the anonymous bearer token, Google sign-in creates a brand-new user — the visitor’s anonymous rows stay behind on the old id. Always send the header while the visitor is still anonymous.

> **note**: Because an anonymous user has no email yet, any Google account can be linked. A Google account already linked to another user returns `409 identity_exists`. The email-match rule (`409 email_mismatch`) only applies to users who already have an email.

## Managing anonymous users

In the Console user directory, anonymous users carry an Anonymous badge and can be filtered separately. From the SQL editor you can inspect them directly:

**SQL · count anonymous users**

```sql
SELECT count(*) AS anonymous_users
FROM auth.users
WHERE is_anonymous;
```

## Additional resources

- [Google sign-in](https://cubibase.com/docs/auth/google) Both Google flows in depth — the bearer-token header is what converts an anonymous user.
- [Row-level security](https://cubibase.com/docs/database/row-level-security) Why anonymous rows are already protected.
- [Quotas](https://cubibase.com/docs/platform/quotas) Anonymous users count toward the end-user limit.
