Skip to content

Commit 58bb889

Browse files
Document server-issued token reconnect behavior
1 parent c39141d commit 58bb889

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

docs/docs/00100-intro/00300-tutorials/00300-unity-tutorial/00300-part-2.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,14 @@ public class GameManager : MonoBehaviour
752752
void HandleConnect(DbConnection _conn, Identity identity, string token)
753753
{
754754
Debug.Log("Connected.");
755+
#if UNITY_WEBGL && !UNITY_EDITOR
756+
if (AuthToken.Token == "")
757+
{
758+
AuthToken.SaveToken(token);
759+
}
760+
#else
755761
AuthToken.SaveToken(token);
762+
#endif
756763
LocalIdentity = identity;
757764

758765
OnConnected?.Invoke();
@@ -796,6 +803,8 @@ public class GameManager : MonoBehaviour
796803
}
797804
```
798805

806+
> Unity WebGL needs one extra precaution here. Browser WebSocket APIs cannot set an `Authorization` header, so reconnecting with a saved server-issued token may yield a short-lived WebSocket token in `HandleConnect`. The `#if UNITY_WEBGL` guard keeps the original saved token instead of overwriting it during reconnect.
807+
799808
Here we configure the connection to the database, by passing it some callbacks in addition to providing the `SERVER_URI` and `MODULE_NAME` to the connection. When the client connects, the SpacetimeDB SDK will call the `HandleConnect` method, allowing us to start up the game.
800809

801810
In our `HandleConnect` callback we build a subscription and call `Subscribe`, subscribing to all data in the database. This causes SpacetimeDB to synchronize the state of all your tables with your Unity client's SDK client cache.

docs/docs/00100-intro/00300-tutorials/00300-unity-tutorial/00400-part-3.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,14 @@ Next lets add some callbacks when rows change in the database. Modify the `Handl
13641364
void HandleConnect(DbConnection conn, Identity identity, string token)
13651365
{
13661366
Debug.Log("Connected.");
1367+
#if UNITY_WEBGL && !UNITY_EDITOR
1368+
if (AuthToken.Token == "")
1369+
{
1370+
AuthToken.SaveToken(token);
1371+
}
1372+
#else
13671373
AuthToken.SaveToken(token);
1374+
#endif
13681375
LocalIdentity = identity;
13691376

13701377
conn.Db.Circle.OnInsert += CircleOnInsert;
@@ -1383,6 +1390,8 @@ Next lets add some callbacks when rows change in the database. Modify the `Handl
13831390
}
13841391
```
13851392

1393+
Keep the same WebGL guard from Part 2 here as well. On Unity WebGL, a reconnect can surface a short-lived WebSocket token in `HandleConnect`, so you should not overwrite an already-saved long-lived server-issued token.
1394+
13861395
Next add the following implementations for those callbacks to the `GameManager` class.
13871396

13881397
```cs

docs/docs/00200-core-concepts/00500-authentication.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ needs, or even implement your own.
1111
If you're new to OIDC, check out our [blog post about OIDC](https://spacetimedb.com/blog/who-are-you)
1212
to learn more about how OIDC works and why it's a great choice for authentication.
1313

14+
## Server-issued tokens and reconnects
15+
16+
If a client connects without supplying a token, SpacetimeDB creates a new
17+
identity and returns a server-issued token for that identity. If you want later
18+
connections to keep using that same identity, persist that returned token and
19+
pass it back into the SDK on reconnect.
20+
21+
On platforms that can send an `Authorization` header during the WebSocket
22+
handshake, the SDK can reuse that saved token directly. On platforms that cannot
23+
set custom WebSocket headers, the SDK first exchanges the saved token for a
24+
short-lived WebSocket token through
25+
[`POST /v1/identity/websocket-token`](/docs/http/identity#post-v1identitywebsocket-token),
26+
then sends only that short-lived token in the WebSocket URL.
27+
28+
This matters when you persist tokens on the client:
29+
30+
- Save the long-lived server-issued token that establishes the user's identity.
31+
- Do not overwrite an already-saved long-lived token with a short-lived
32+
WebSocket token received during reconnect.
33+
- Expect this distinction on browser-style transports where WebSocket headers
34+
are unavailable, such as Unity WebGL builds.
35+
1436
## SpacetimeAuth
1537

1638
To make it easier to get started with authentication, SpacetimeDB offers

0 commit comments

Comments
 (0)