@relai-team/access-sdk 0.8.0 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -0
- package/dist/index.cjs +12 -0
- package/dist/index.d.cts +12 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +12 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -46,6 +46,44 @@ const result = await relai.unlock({
|
|
|
46
46
|
console.log(result.accepted, result.command); // true "OPEN 3:0"
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
+
## Environments: one app, two keys
|
|
50
|
+
|
|
51
|
+
Your app holds a **sandbox** key pair from the moment it is created, and gains a
|
|
52
|
+
**production** pair alongside it once a Relai admin approves it. You never make a
|
|
53
|
+
second app to test, and going live moves nothing: same app id, same webhook
|
|
54
|
+
secret, same simulated Exchange Zones, same subscription.
|
|
55
|
+
|
|
56
|
+
Switching environments is switching the key. There is no mode flag:
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
const test = new RelaiClient({ publishableKey: "pk_sandbox_..." }); // simulated EZs, Stripe test
|
|
60
|
+
const live = new RelaiClient({ publishableKey: "pk_live_..." }); // real hardware, real money
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
A sandbox key sees only your simulated Exchange Zones; a production key sees only
|
|
64
|
+
real ones you have access to. Asking either for the other's Exchange Zone returns
|
|
65
|
+
`not_found`.
|
|
66
|
+
|
|
67
|
+
**Sessions belong to one environment.** A token issued under a sandbox key is
|
|
68
|
+
rejected when sent with a production key, and refreshing will not move a session
|
|
69
|
+
across. Prefer one `RelaiClient` per environment. If a stored session does end up
|
|
70
|
+
on the wrong side (a persisted token store surviving a key swap), the SDK drops
|
|
71
|
+
it on the first `invalid_token` so `auth.isSignedIn` goes false and your normal
|
|
72
|
+
signed-out path takes over; sign in again to get a session for the new
|
|
73
|
+
environment.
|
|
74
|
+
|
|
75
|
+
To sign in while building, create **test end-users** on your app's page in the
|
|
76
|
+
portal. Each gets a fixed sign-in code shown next to it, so your app runs the
|
|
77
|
+
normal OTP flow with no environment branch in your code:
|
|
78
|
+
|
|
79
|
+
```ts
|
|
80
|
+
await relai.auth.startOtp("dana.the.courier.abc12345@test.relai.us");
|
|
81
|
+
await relai.auth.verifyOtp("dana.the.courier.abc12345@test.relai.us", "455606");
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Test end-users receive no email, never appear among your real customers, and are
|
|
85
|
+
refused by production keys.
|
|
86
|
+
|
|
49
87
|
## How an open works
|
|
50
88
|
|
|
51
89
|
`relai.unlock(...)` runs the full server-owns-trust sequence:
|
package/dist/index.cjs
CHANGED
|
@@ -137,6 +137,9 @@ var HttpClient = class {
|
|
|
137
137
|
const refreshed = await this.cfg.onTokenExpired();
|
|
138
138
|
if (refreshed) return this.attempt(opts, idem, true);
|
|
139
139
|
}
|
|
140
|
+
if (err instanceof exports.RelaiApiError && err.code === "invalid_token" && this.cfg.onTokenInvalid && !opts.noBearer) {
|
|
141
|
+
this.cfg.onTokenInvalid();
|
|
142
|
+
}
|
|
140
143
|
throw err;
|
|
141
144
|
}
|
|
142
145
|
}
|
|
@@ -295,6 +298,14 @@ var AuthModule = class {
|
|
|
295
298
|
return false;
|
|
296
299
|
}
|
|
297
300
|
}
|
|
301
|
+
/**
|
|
302
|
+
* Drop the local session without calling the server. For a session the server
|
|
303
|
+
* has already rejected as permanently invalid, where `signOut()`'s revoke call
|
|
304
|
+
* would just fail on a token that is already dead.
|
|
305
|
+
*/
|
|
306
|
+
clearSession() {
|
|
307
|
+
this.storage.clear();
|
|
308
|
+
}
|
|
298
309
|
/** Revoke the refresh token and clear local state. */
|
|
299
310
|
async signOut() {
|
|
300
311
|
const current = this.storage.get();
|
|
@@ -587,6 +598,7 @@ var RelaiClient = class {
|
|
|
587
598
|
baseUrl: options.baseUrl ?? DEFAULT_BASE_URL,
|
|
588
599
|
getBearer: () => auth?.accessToken,
|
|
589
600
|
onTokenExpired: () => auth?.refresh() ?? Promise.resolve(false),
|
|
601
|
+
onTokenInvalid: () => auth?.clearSession(),
|
|
590
602
|
...options.publishableKey !== void 0 ? { publishableKey: options.publishableKey } : {},
|
|
591
603
|
...options.secretKey !== void 0 ? { secretKey: options.secretKey } : {},
|
|
592
604
|
...options.fetchImpl !== void 0 ? { fetchImpl: options.fetchImpl } : {},
|
package/dist/index.d.cts
CHANGED
|
@@ -6,6 +6,12 @@ interface HttpConfig {
|
|
|
6
6
|
getBearer?: () => string | undefined;
|
|
7
7
|
/** Invoked once on `token_expired`; returns true if a refresh succeeded. */
|
|
8
8
|
onTokenExpired?: () => Promise<boolean>;
|
|
9
|
+
/**
|
|
10
|
+
* Invoked on `invalid_token`: the stored session is permanently dead and no
|
|
11
|
+
* refresh can save it (wrong environment, revoked epoch, wrong app). The
|
|
12
|
+
* client drops it so `auth.isSignedIn` goes false.
|
|
13
|
+
*/
|
|
14
|
+
onTokenInvalid?: () => void;
|
|
9
15
|
fetchImpl?: typeof fetch;
|
|
10
16
|
maxRetries?: number;
|
|
11
17
|
}
|
|
@@ -272,6 +278,12 @@ declare class AuthModule {
|
|
|
272
278
|
/** Rotate the token pair. Single-flight: concurrent callers share one refresh. */
|
|
273
279
|
refresh(): Promise<boolean>;
|
|
274
280
|
private doRefresh;
|
|
281
|
+
/**
|
|
282
|
+
* Drop the local session without calling the server. For a session the server
|
|
283
|
+
* has already rejected as permanently invalid, where `signOut()`'s revoke call
|
|
284
|
+
* would just fail on a token that is already dead.
|
|
285
|
+
*/
|
|
286
|
+
clearSession(): void;
|
|
275
287
|
/** Revoke the refresh token and clear local state. */
|
|
276
288
|
signOut(): Promise<void>;
|
|
277
289
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -6,6 +6,12 @@ interface HttpConfig {
|
|
|
6
6
|
getBearer?: () => string | undefined;
|
|
7
7
|
/** Invoked once on `token_expired`; returns true if a refresh succeeded. */
|
|
8
8
|
onTokenExpired?: () => Promise<boolean>;
|
|
9
|
+
/**
|
|
10
|
+
* Invoked on `invalid_token`: the stored session is permanently dead and no
|
|
11
|
+
* refresh can save it (wrong environment, revoked epoch, wrong app). The
|
|
12
|
+
* client drops it so `auth.isSignedIn` goes false.
|
|
13
|
+
*/
|
|
14
|
+
onTokenInvalid?: () => void;
|
|
9
15
|
fetchImpl?: typeof fetch;
|
|
10
16
|
maxRetries?: number;
|
|
11
17
|
}
|
|
@@ -272,6 +278,12 @@ declare class AuthModule {
|
|
|
272
278
|
/** Rotate the token pair. Single-flight: concurrent callers share one refresh. */
|
|
273
279
|
refresh(): Promise<boolean>;
|
|
274
280
|
private doRefresh;
|
|
281
|
+
/**
|
|
282
|
+
* Drop the local session without calling the server. For a session the server
|
|
283
|
+
* has already rejected as permanently invalid, where `signOut()`'s revoke call
|
|
284
|
+
* would just fail on a token that is already dead.
|
|
285
|
+
*/
|
|
286
|
+
clearSession(): void;
|
|
275
287
|
/** Revoke the refresh token and clear local state. */
|
|
276
288
|
signOut(): Promise<void>;
|
|
277
289
|
}
|
package/dist/index.js
CHANGED
|
@@ -27,6 +27,9 @@ var HttpClient = class {
|
|
|
27
27
|
const refreshed = await this.cfg.onTokenExpired();
|
|
28
28
|
if (refreshed) return this.attempt(opts, idem, true);
|
|
29
29
|
}
|
|
30
|
+
if (err instanceof RelaiApiError && err.code === "invalid_token" && this.cfg.onTokenInvalid && !opts.noBearer) {
|
|
31
|
+
this.cfg.onTokenInvalid();
|
|
32
|
+
}
|
|
30
33
|
throw err;
|
|
31
34
|
}
|
|
32
35
|
}
|
|
@@ -184,6 +187,14 @@ var AuthModule = class {
|
|
|
184
187
|
return false;
|
|
185
188
|
}
|
|
186
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* Drop the local session without calling the server. For a session the server
|
|
192
|
+
* has already rejected as permanently invalid, where `signOut()`'s revoke call
|
|
193
|
+
* would just fail on a token that is already dead.
|
|
194
|
+
*/
|
|
195
|
+
clearSession() {
|
|
196
|
+
this.storage.clear();
|
|
197
|
+
}
|
|
187
198
|
/** Revoke the refresh token and clear local state. */
|
|
188
199
|
async signOut() {
|
|
189
200
|
const current = this.storage.get();
|
|
@@ -469,6 +480,7 @@ var RelaiClient = class {
|
|
|
469
480
|
baseUrl: options.baseUrl ?? DEFAULT_BASE_URL,
|
|
470
481
|
getBearer: () => auth?.accessToken,
|
|
471
482
|
onTokenExpired: () => auth?.refresh() ?? Promise.resolve(false),
|
|
483
|
+
onTokenInvalid: () => auth?.clearSession(),
|
|
472
484
|
...options.publishableKey !== void 0 ? { publishableKey: options.publishableKey } : {},
|
|
473
485
|
...options.secretKey !== void 0 ? { secretKey: options.secretKey } : {},
|
|
474
486
|
...options.fetchImpl !== void 0 ? { fetchImpl: options.fetchImpl } : {},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@relai-team/access-sdk",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "Official Relai Access SDK, open Relai Exchange Zone nodes over BLE, backed by the Relai Access platform (server-owns-trust).",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"type": "module",
|