Introducing a new way to review your PRs.

Read the announcement

appwrite / sdk-for-react-native

[READ ONLY] Official Appwrite React Native SDK 💙 ⚛︎

View on GitHub

24 documents · Updated Nov 18, 2026

docs / oauth-analytics-and-push

OAuth, analytics, and push integration

This part of the SDK connects an application to Appwrite's identity and delivery features. It supports OAuth 2 and OpenID Connect consent and token flows, sends product events to Analytics, and links signed-in users to push targets or live push topics.

OAuth 2 authorization

OAuth 2 is the integration path for an external application that needs user-approved access to Appwrite resources. The Oauth2 service starts the request, carries the request through user consent, and exchanges the resulting proof for access and refresh tokens.

Authorization request

The authorization request describes the client, return address, response type, requested scopes, and optional OpenID Connect or resource details. The service supports both GET and POST forms, and it returns a redirect result that the application sends to the end user.

The request can use PKCE for public clients. The state value returns to the client so it can match the response to the original request. A nonce protects an OpenID Connect ID token from replay when the response includes an ID token.

The request may also carry rich authorization_details, resource indicators, or a short-lived request_uri created by PAR. These values let the client ask for a specific resource set without changing the main consent flow.

References: src/…/oauth-2.ts• L127-392 src/…/oauth-2.ts• L394-660

Pushed authorization request

Pushed Authorization Request (PAR) stores the authorization request before the browser flow starts. The caller receives a short-lived request_uri, then passes that handle to authorization instead of sending the full request through the browser.

PAR keeps the request data on the authorization service and preserves the same client, redirect, response, scope, PKCE, and resource rules as a direct authorization request.

References: src/…/oauth-2.ts• L1355-1624

A grant is the pending record that connects a client request to the signed-in user's consent decision. The consent screen reads the grant to show the client, scopes, resources, and redirect information before it chooses an outcome.

The flow follows this sequence:

sequenceDiagram
    participant App as Client application
    participant Auth as OAuth service
    participant User as User and consent screen
    App->>Auth: Start authorization
    Auth-->>App: Redirect with grant context
    App->>Auth: Read grant
    Auth-->>App: Requested access details
    User->>App: Approve or reject
    App->>Auth: Complete grant decision
    Auth-->>App: Success or access_denied redirect

Approval can keep the requested scopes or reduce them to a subset. It can also replace the requested authorization details with the concrete resources the user selected. Rejection ends the request with an access_denied result.

References: src/…/oauth-2.ts• L16-125 src/…/oauth-2.ts• L803-968 src/…/oauth-2.ts• L1739-1813

Device authorization

Device authorization supports clients that cannot open a normal browser callback. The client first receives a device code, user code, verification address, expiry, and polling interval. The user completes consent on another device.

After the user enters the user code, the authenticated consent side creates a grant. The device then polls the token endpoint with its device code until the grant is approved or the request fails.

References: src/…/oauth-2.ts• L662-800 src/…/oauth-2.ts• L803-888 src/…/oauth-2.ts• L1929-2147

Token exchange

The token endpoint turns an approved authorization result into access and refresh tokens. It supports three grant types:

  • authorization_code for the browser authorization flow.
  • refresh_token for obtaining a new token set.
  • urn:ietf:params:oauth:grant-type:device_code for device authorization.

Public clients provide the PKCE verifier when they exchange an authorization code. Confidential clients can provide their client secret. Resource indicators can stay attached to the token request so the issued access token targets the intended resource.

References: src/…/oauth-2.ts• L1815-1927 src/…/oauth-2.ts• L1929-2147

Resource lists

OAuth access can include project or organization resources. The service can list the concrete projects or organizations allowed by the token, which lets a consent or resource-selection screen turn a broad permission into a user-visible set of choices.

References: src/…/oauth-2.ts• L1244-1353 src/…/oauth-2.ts• L1626-1737

Token revocation

Revocation ends the validity of an OAuth access token or refresh token. The caller supplies the token and may provide its token type, client identity, and secret so the authorization service can apply the correct client rules.

OpenID Connect logout provides a related user-facing flow. A valid ID token hint ends the user session and revokes the tokens issued to the identified application before an approved post-logout redirect.

References: src/…/oauth-2.ts• L971-1242 src/…/oauth-2.ts• L1815-1927

Analytics event creation

Analytics receives one event at a time for a configured property. The Analytics service accepts a required event name and page or screen URL, then adds context that helps describe the visit or interaction.

An event can include the following context:

ContextPurpose
Domain and referrerDescribe where the event occurred and how the visitor arrived.
Screen widthRecord the display size for the visit.
Session hashConnect events from the same SDK session.
Scroll depth and engagement timeDescribe how far and how long the visitor used the page or screen.
Custom propertiesAdd application-specific string values.
User, IP, and user agent overridesSet server-side identity or request context when API-key analytics write access allows it.
Interactive statusMark whether the event is a visitor interaction that ends a bounce.

pageview and screen_view are counted as views. Other event names record custom product activity. The interactive setting separates visitor actions from background telemetry, while views ignore that setting.

References: src/…/analytics.ts• L10-45 src/…/analytics.ts• L175-273

Push delivery

Push delivery has two related roles. Account push targets register a user's device with a messaging provider. The Push service connects an authenticated application to live topic messages and can turn selected messages into local notifications.

Push target registration

The Account service binds a push target to the current session. Registration supplies a target ID and device identifier, with optional provider, app bundle, and APNs environment data. Re-registering for the same provider updates the existing target instead of creating a second delivery record.

Target management follows a small lifecycle:

  1. Register the device with createPushTarget.
  2. Replace a rotated token or routing detail with updatePushTarget.
  3. Stop delivery for that device with deletePushTarget.

The target must belong to the current user for update and delete operations. The related examples are create push target, update push target, and delete push target.

References: src/…/account.ts• L5495-5645 src/…/account.ts• L5647-5783 src/…/account.ts• L5785-5844

Topic subscriptions

The Push service connects an authenticated client to Appwrite's push broker and routes matching messages to callbacks. A caller can subscribe to explicit topic strings or use the signed-in user's own topic. Topic.path builds topic names one level at a time and prevents MQTT wildcard characters from entering a single topic level.

Each subscription returns a handle that can change delivery options or unsubscribe. The callback is allowed to finish before a retried message is acknowledged, which gives at-least-once delivery. A subscription can disable retry for at-most-once delivery. The subscription promise completes after the broker confirms each topic, so a publish that follows an awaited subscribe does not race the subscription setup.

The user-topic form defaults to background delivery and retry. Explicit topics default to foreground delivery. Background delivery can post local notifications while the app is not visible, and an option can also show them while the app is in the foreground. Notification taps are available as an initial event at startup or as later open events while the app runs.

Push can also connect to an analytics tracking service. It records push_received for visible background notifications and push_opened when the user opens one, while suppressing duplicate reports for the same delivery event.

References: src/…/push.ts• L74-118 src/…/push.ts• L348-452 src/…/push.ts• L525-711 src/…/push.ts• L1627-1667 src/topic.ts• L44-65

Background delivery

Background delivery keeps selected subscriptions active after the application leaves the foreground. Android can use its native host or a foreground service. Other platforms use the available background task and notification dependencies.

The service reports Android background limits and supports exact-alarm and battery-optimization requests. The app should read the initial notification at startup and listen for later notification taps while it is running.

Background delivery uses the client's authenticated session or JWT. Closing the push service removes live subscriptions; on Android it also stops saved background delivery when it is the last active owner, so sign-out must close the service. A single native host serves one credential, so background subscriptions using another credential can move back to foreground delivery.

References: src/…/push.ts• L299-345 src/…/push.ts• L887-1153 src/…/push.ts• L1154-1185 src/…/push.ts• L1232-1313 src/…/push.ts• L1371-1610

Service relationships

These services share the configured Appwrite client but serve different downstream systems. OAuth produces delegated credentials, Analytics records usage signals, and Push connects a user or topic to message delivery.

flowchart LR
    Client[Application client]
    Client --> OAuth[OAuth 2 service]
    OAuth --> Consent[User consent]
    Consent --> Tokens[Access and refresh tokens]
    Client --> Analytics[Analytics service]
    Analytics --> Events[Property events]
    Client --> Account[Account service]
    Account --> Target[Registered push target]
    Client --> Push[Push service]
    Push --> Topics[Live topic messages and notifications]

For the wider service map, see Application and integration services. Application credentials and installations are described in Application management and installations. Detailed event submission is described in Analytics tracking. Current-user sessions and security controls are described in Account authentication and security.

References: src/index.ts• L1-28 src/…/oauth-2.ts• L11-15 src/…/analytics.ts• L5-8 src/…/account.ts• L5495-5506 src/…/push.ts• L126-149

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the OAuth request fields aligned between direct authorization, POST authorization, PAR, device authorization, and token exchange. A new resource or consent field must remain usable across the full flow.
  • Keep grant outcomes aligned with the redirect contract. Approval, rejection, and logout must continue to return only registered redirect destinations and the expected state values.
  • Keep OAuth resource selection aligned with token authorization details. Project and organization lists must still reflect the resources that the token can access.
  • Keep push target ownership and provider routing aligned when changing target registration. Token rotation must still update the existing provider target rather than create duplicate delivery.
  • Keep push topic, authentication, client ID, and background-host behavior aligned. Changing one can alter broker session replay or move subscriptions between foreground and native delivery.
  • Keep notification-open and push-tracking events aligned with message identity. Initial opens, later opens, and repeated matching subscriptions must not create duplicate analytics events.
  • Keep analytics override fields protected by the required API-key analytics write scope.
  • Keep analytics event classification aligned with the interactive flag. View events must remain view events, while background telemetry must not count as a visitor interaction.

Important failure modes

TriggerConsequenceGuard
A public OAuth client omits the PKCE verifier or sends a mismatched redirect URI.The token exchange or redirect cannot complete safely.Keep the original redirect URI and code verifier with the authorization request.
Consent approves scopes outside the requested set.The grant violates the consent contract.Approve only the requested scope set or a subset.
A device polls with an expired device code or before the user completes consent.No token is issued.Use the returned expiry and polling interval, then stop when the grant expires or is denied.
A push subscription starts without a session or JWT.The broker cannot identify the user or authorize the connection.Set a JWT or session on the client before subscribing.
Background delivery is requested without the native Android module or required peer services.The subscription cannot remain active after the app leaves the foreground.Check platform support and install the required native or notification support.
A push callback fails before a retryable message is acknowledged.The broker can deliver the message again.Make handlers safe to run more than once and report callback errors.
A rotated device token is registered as a new provider target.The user can receive duplicate notifications or retain a stale target.Update the existing target for the provider.
A notification open is counted once for each matching subscription.Push analytics can overstate notification opens.Use the message identity and event type to suppress duplicate tracking.
A background event is marked interactive.Telemetry can count as a visitor interaction and affect bounce reporting.Set interactive to false for heartbeats, syncs, and other non-user activity.

Key files

FileWhy to read it
src/…/oauth-2.tsDefines the authorization, consent, device, PAR, token, logout, resource, and revocation contracts.
src/…/analytics.tsDefines the event shape and the analytics property event boundary.
src/…/account.tsDefines push target registration, update, and deletion for the current user.
src/…/push.tsDefines live topic subscriptions, retry behavior, background delivery, notification open handling, and push analytics hooks.
src/topic.tsDefines safe construction of push topic names.
src/client.tsDefines push broker endpoint and stable client ID configuration.
src/index.tsShows which OAuth, Analytics, Account, and Push APIs are public SDK exports.