<UrProvider> is the heart of the React SDK. It initializes the urBackend client, manages global authentication state, and synchronizes the user’s session seamlessly across your application.
You must wrap your entire application (or at least the parts that require authentication and database access) inside this provider.
Usage
Props
How it works
When<UrProvider> mounts, it automatically:
- Initializes a singleton
@urbackend/sdkinstance. - Checks
localStoragefor an existing active session. The access token is read fromub_auth_tokenand the refresh token fromub_refresh_token. - Attempts to refresh the session in the background by calling
auth.refreshToken(), which sends the stored refresh token in thex-refresh-tokenheader (and falls back to any HTTP-only cookie if no stored token is found). This makes cross-domain auth work without shared cookies. - Exposes the
AuthUserstate and SDK client to all child components via React Context (accessed through hooks likeuseUseranduseDb).
Access tokens live in memory during the session and are mirrored to
localStorage.ub_auth_token. Refresh tokens are persisted to localStorage.ub_refresh_token. Call client.auth.logout() (or setToken(undefined, undefined)) to clear both.The provider sets up global state. You only ever need one
<UrProvider> at the root of your React tree.