# Sessions and tokens

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

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

Access tokens live for one hour; refresh tokens for 30 days and rotate on every refresh. Reusing a replaced refresh token revokes the whole family.

## The token pair

| Token | Format | Default lifetime | Use |
| --- | --- | --- | --- |
| `accessToken` | HS256 JWT | 1 hour | Bearer token on Data API and user endpoints |
| `refreshToken` | Opaque, stored hashed | 30 days | Exchange for a new pair; never sent to the Data API |

The access token’s claims include `sub` (the user id), `email`, `session_id`, `amr` (how the session authenticated: `pwd`, `mfa`, `oauth`, or empty for anonymous), and the expiry. Both lifetimes are configurable per project.

## The session lifecycle

**Session lifecycle, illustration.**

1. Sign in (accessToken · 1 h). Email and password return an access JWT and a refresh token.
2. Refresh (refreshToken · rotates). A new pair is issued and the old refresh token is retired.
3. Reuse detected (family revoked). A retired token presented again revokes the whole family.
4. Revoke (session_revoked). Logout or a Console revoke fails the session on the next request.

Illustration with fictional tokens. It does not query CubiBase.

## Refreshing

**Refresh · request**

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

{
  "refreshToken": "<REFRESH_TOKEN>"
}
```

- A refresh issues a new access token and a new refresh token, and retires the old one.
- The session id stays the same across refreshes, so the Console shows one session, not dozens.
- Store the new pair atomically: the old refresh token is dead the moment the response arrives.

> **warning**: If a replaced refresh token is presented again — a replayed copy, a stolen backup — CubiBase revokes the entire token family. The user signs in again; the attacker’s copy stops working. Treat `401 invalid_refresh_token` as “sign in again”, not “retry”.

## Revocation is immediate

Every Data API request checks that the session behind the token is still live and the user is not banned. Revoking a session (logout, or the Console’s Sessions screen) or banning a user takes effect on the next request — not when the access token expires.

**Logout · request**

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

{
  "refreshToken": "<REFRESH_TOKEN>"
}
```

| Situation | Next request returns |
| --- | --- |
| Session revoked | `401 session_revoked` |
| User banned | `403 user_banned` |
| Project requires MFA, session lacks it | `401 mfa_required` |
| Access token expired | `401 invalid_token` |

## Additional resources

- [Email and password](https://cubibase.com/docs/auth/email-password) Where the first pair comes from.
- [Multi-factor (TOTP)](https://cubibase.com/docs/auth/mfa) Sessions that carry an MFA verification.
