You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 837c1f7
Browse filesBrowse the repository at this point in the historyBrowse files
-**Identity**: A long-lived, globally unique identifier for a user.
105
-
-**ConnectionId**: Identifies a specific client connection.
104
+
-**Identity**: A long-lived, globally unique identifier for a user, derived from the token's issuer and subject claims. The same token yields the same identity on every connection.
105
+
-**ConnectionId**: Identifies one client connection. A new connection gets a new connection ID; a disconnect ends the connection, not the identity.
106
106
- Always use `ctx.sender` / `ctx.Sender` / `ctx.sender()` for authorization.
107
107
108
108
SpacetimeDB works with many OIDC providers, including SpacetimeAuth (built-in), Auth0, Clerk, Keycloak, Google, and GitHub.
Connection hooks run once per connection. The same authenticated principal keeps the same identity (`ctx.sender()`) across connections, while each connection has its own connection ID (`ctx.connection_id()`). A disconnect ends one connection; it does not end the identity, which returns unchanged on the next connection with the same token. Use connection IDs for presence and other connection-scoped state.
`ctx.ConnectionId` is `ConnectionId?`, including in connection lifecycle reducers. Check or unwrap it before storing it in a non-nullable column or passing it to an index accessor.
165
+
Connection hooks run once per connection. The same authenticated principal keeps the same identity (`ctx.Sender`) across connections, while each connection has its own connection ID (`ctx.ConnectionId`). A disconnect ends one connection; it does not end the identity, which returns unchanged on the next connection with the same token. Use connection IDs for presence and other connection-scoped state.
166
+
167
+
`ctx.ConnectionId` is typed `ConnectionId?`. It is present inside connection lifecycle reducers and reducers invoked over a connection, and null in `Init` and scheduled reducers. Check or unwrap it before storing it in a non-nullable column or passing it to an index accessor.
Connection hooks run once per connection. The same authenticated principal keeps the same identity (`ctx.sender()`) across connections, while each connection has its own connection ID (`ctx.connection_id()`). A disconnect ends one connection; it does not end the identity, which returns unchanged on the next connection with the same token. Use connection IDs for presence and other connection-scoped state.
154
+
153
155
The current connection ID is available through `ctx.connection_id()` (not a public field) and may be absent outside connection-scoped calls.
`ctx.connectionId` is `ConnectionId | null`, including in lifecycle contexts. Guard it before passing it to a helper or using it as a table key.
188
+
Connection hooks run once per connection. The same authenticated principal keeps the same identity (`ctx.sender`) across connections, while each connection has its own connection ID (`ctx.connectionId`). A disconnect ends one connection; it does not end the identity, which returns unchanged on the next connection with the same token. Use connection IDs for presence and other connection-scoped state.
189
+
190
+
`ctx.connectionId` is typed `ConnectionId | null`. It is present inside connection lifecycle hooks and reducers invoked over a connection, and `null` in `init` and scheduled reducers. Guard it before passing it to a helper or using it as a table key.
182
191
183
192
## Reducer Context API
184
193
194
+
Each reducer call runs in one database transaction. An error that escapes the
195
+
reducer rolls back its database changes. `ctx.sender` identifies the caller;
196
+
application roles and permissions are not inferred from that identity. Table
197
+
visibility and view filters control reads, not authorization to call reducers.
198
+
185
199
`ctx` is the only source of sender identity, time, and randomness; stdlib clocks and RNG are unavailable in modules. Let exported callbacks infer their context type. In helpers, use `ReducerCtx<InferSchema<typeof spacetimedb>>`; do not annotate a context as `any`, because that erases table row types and can make `bigint` expressions infer as `number`.
A client subscribing to a view receives only the rows it returns. Use a per-user view
279
293
(keyed on `ctx.sender`) for per-viewer access control: deleting a row it depends on
280
294
(e.g. a membership row) automatically drops the rows it was exposing from that client.
295
+
Use index accessors in views. Do not scan a whole table with `.iter()` when an
296
+
indexed lookup can select the required rows.
281
297
282
298
`t.row(...)` and `t.object(...)` return schema builders, not TypeScript runtime row types. Let a view callback infer its result, or annotate a separately declared structural type such as `Array<{ sku: bigint; label: string }>`. A named output type must not reuse the generated PascalCase name of its view accessor (for example, reserve `DiscountedProduct` for a `discounted_product` view).
283
299
300
+
A view context is `ViewCtx<S>` (and `AnonymousViewCtx<S>`), both exported from
301
+
`spacetimedb/server`. It carries `sender`, a read-only `db`, and `from`; it is
302
+
not a `ReducerCtx`, so a helper shared between a reducer and a view must accept
0 commit comments