@useauthio/react 0.2.4 → 0.3.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/CHANGELOG.md +79 -0
- package/README.md +88 -19
- package/dist/index.cjs +496 -63
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +358 -27
- package/dist/index.d.ts +358 -27
- package/dist/index.js +485 -65
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/PasskeyManager.tsx +4 -3
- package/src/activity.ts +95 -0
- package/src/device-signals.ts +86 -0
- package/src/errors.ts +239 -17
- package/src/fetch.ts +23 -12
- package/src/index.ts +37 -3
- package/src/lobby-context.ts +16 -0
- package/src/locate.ts +42 -0
- package/src/passkeys.ts +18 -8
- package/src/provider.tsx +111 -15
- package/src/refresh.ts +41 -0
- package/src/sign-in.ts +263 -29
- package/src/telemetry.ts +11 -0
- package/src/types.ts +71 -0
- package/src/usePasskeys.ts +13 -4
- package/src/version.ts +1 -1
package/src/sign-in.ts
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
1
|
import { AuthioError } from "@useauthio/node";
|
|
2
2
|
import { authioFetch } from "./fetch";
|
|
3
|
+
import {
|
|
4
|
+
AUTHIO_MESSAGES,
|
|
5
|
+
riskDecisionError,
|
|
6
|
+
type RiskDecisionBody,
|
|
7
|
+
} from "./errors";
|
|
8
|
+
import {
|
|
9
|
+
deviceSignalsExtraHeaders,
|
|
10
|
+
type DeviceSignalsCapture,
|
|
11
|
+
} from "./device-signals";
|
|
3
12
|
import { isBrowser } from "./ssr";
|
|
4
|
-
import type {
|
|
13
|
+
import type { ClientLocationCapture } from "./locate";
|
|
14
|
+
import {
|
|
15
|
+
coerceSessionPolicy,
|
|
16
|
+
type AuthioUser,
|
|
17
|
+
type RawSessionPolicy,
|
|
18
|
+
type SessionPolicy,
|
|
19
|
+
} from "./types";
|
|
5
20
|
|
|
6
21
|
export interface SignInWithMagicLinkOptions {
|
|
7
22
|
/** Authio auth-core base URL. */
|
|
@@ -16,6 +31,8 @@ export interface SignInWithMagicLinkOptions {
|
|
|
16
31
|
* after the user clicks the link in their inbox.
|
|
17
32
|
*/
|
|
18
33
|
redirectUri: string;
|
|
34
|
+
/** Opaque, consent-gated proof minted by @useauthio/xray. */
|
|
35
|
+
xrayVisitorProof?: string;
|
|
19
36
|
signal?: AbortSignal;
|
|
20
37
|
fetch?: typeof fetch;
|
|
21
38
|
}
|
|
@@ -59,6 +76,9 @@ export async function signInWithMagicLink(
|
|
|
59
76
|
// (email or E.164), NOT `email`. See magiclink.go magicLinkSendReq.
|
|
60
77
|
destination: opts.email,
|
|
61
78
|
redirect_uri: opts.redirectUri,
|
|
79
|
+
...(opts.xrayVisitorProof
|
|
80
|
+
? { xray_visitor_proof: opts.xrayVisitorProof }
|
|
81
|
+
: {}),
|
|
62
82
|
},
|
|
63
83
|
signal: opts.signal,
|
|
64
84
|
fetchImpl: opts.fetch,
|
|
@@ -73,6 +93,16 @@ export interface SignInWithPasskeyOptions {
|
|
|
73
93
|
* the user picks their key from the browser's native chooser.
|
|
74
94
|
*/
|
|
75
95
|
email?: string;
|
|
96
|
+
/**
|
|
97
|
+
* Post-sign-in path on the app (`next`) forwarded to auth-core on
|
|
98
|
+
* the verify call. Older auth-core deployments ignore it; token
|
|
99
|
+
* handling on the client is unchanged either way.
|
|
100
|
+
*/
|
|
101
|
+
next?: string;
|
|
102
|
+
/** Organization scope (`org_…`) for org-policy enforcement. */
|
|
103
|
+
organizationId?: string;
|
|
104
|
+
/** Opaque, consent-gated proof minted by @useauthio/xray. */
|
|
105
|
+
xrayVisitorProof?: string;
|
|
76
106
|
signal?: AbortSignal;
|
|
77
107
|
fetch?: typeof fetch;
|
|
78
108
|
}
|
|
@@ -80,6 +110,8 @@ export interface SignInWithPasskeyOptions {
|
|
|
80
110
|
export interface SignInWithPasskeyResult {
|
|
81
111
|
accessToken: string;
|
|
82
112
|
refreshToken?: string;
|
|
113
|
+
/** Effective session policy from the envelope, `null` on older auth-core. */
|
|
114
|
+
sessionPolicy: SessionPolicy | null;
|
|
83
115
|
user: AuthioUser;
|
|
84
116
|
}
|
|
85
117
|
|
|
@@ -95,17 +127,74 @@ interface PasskeyLoginOptionsResponse {
|
|
|
95
127
|
}>;
|
|
96
128
|
}
|
|
97
129
|
|
|
98
|
-
|
|
99
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Wire shape of a sign-in verify response. Every field is optional on
|
|
132
|
+
* purpose: the same endpoint returns either a session envelope or a
|
|
133
|
+
* risk-engine `step_up` body (HTTP 200, no tokens, no user), so nothing
|
|
134
|
+
* may be assumed present. See `sessionFromResponse`.
|
|
135
|
+
*/
|
|
136
|
+
interface SignInVerifyResponse extends RiskDecisionBody {
|
|
137
|
+
access_token?: string;
|
|
100
138
|
refresh_token?: string;
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
139
|
+
session_policy?: RawSessionPolicy;
|
|
140
|
+
user?: {
|
|
141
|
+
id?: string;
|
|
142
|
+
email?: string;
|
|
104
143
|
email_verified?: boolean;
|
|
105
144
|
emailVerified?: boolean;
|
|
106
145
|
name?: string;
|
|
107
146
|
avatar_url?: string;
|
|
108
147
|
avatarUrl?: string;
|
|
148
|
+
} | null;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
interface SignInSession {
|
|
152
|
+
accessToken: string;
|
|
153
|
+
refreshToken?: string;
|
|
154
|
+
sessionPolicy: SessionPolicy | null;
|
|
155
|
+
user: AuthioUser;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Map a 2xx sign-in verify body to a session, never assuming a field
|
|
160
|
+
* is present:
|
|
161
|
+
*
|
|
162
|
+
* - `decision: "step_up"` → `AuthioStepUpRequiredError`
|
|
163
|
+
* (`step_up_required`, carries `stepUpUrl` / `challengeId` /
|
|
164
|
+
* `requiredMethod`).
|
|
165
|
+
* - `decision: "block" | "deny"` → `AuthioSignInDeniedError`
|
|
166
|
+
* (`sign_in_denied`).
|
|
167
|
+
* - no access token or no user id → `AuthioError` `invalid_response`.
|
|
168
|
+
*/
|
|
169
|
+
export function sessionFromResponse(
|
|
170
|
+
res: SignInVerifyResponse | null | undefined,
|
|
171
|
+
status = 200,
|
|
172
|
+
): SignInSession {
|
|
173
|
+
const risk = riskDecisionError(res, status);
|
|
174
|
+
if (risk) throw risk;
|
|
175
|
+
const accessToken =
|
|
176
|
+
res && typeof res.access_token === "string" ? res.access_token : "";
|
|
177
|
+
const user = res && res.user && typeof res.user === "object" ? res.user : null;
|
|
178
|
+
const userId = user && typeof user.id === "string" ? user.id : "";
|
|
179
|
+
if (!res || !accessToken || !user || !userId) {
|
|
180
|
+
throw new AuthioError({
|
|
181
|
+
code: "invalid_response",
|
|
182
|
+
message: AUTHIO_MESSAGES.invalid_response,
|
|
183
|
+
status,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
return {
|
|
187
|
+
accessToken,
|
|
188
|
+
refreshToken:
|
|
189
|
+
typeof res.refresh_token === "string" ? res.refresh_token : undefined,
|
|
190
|
+
sessionPolicy: coerceSessionPolicy(res.session_policy),
|
|
191
|
+
user: {
|
|
192
|
+
id: userId,
|
|
193
|
+
email: typeof user.email === "string" ? user.email : "",
|
|
194
|
+
emailVerified: Boolean(user.emailVerified ?? user.email_verified),
|
|
195
|
+
name: user.name,
|
|
196
|
+
avatarUrl: user.avatarUrl ?? user.avatar_url,
|
|
197
|
+
},
|
|
109
198
|
};
|
|
110
199
|
}
|
|
111
200
|
|
|
@@ -127,7 +216,13 @@ interface PasskeyLoginVerifyResponse {
|
|
|
127
216
|
* - `webauthn_unsupported` if `navigator.credentials.get` is
|
|
128
217
|
* missing (legacy browser).
|
|
129
218
|
* - `webauthn_cancelled` if the user dismisses the chooser.
|
|
130
|
-
* -
|
|
219
|
+
* - `step_up_required` (`AuthioStepUpRequiredError`) when the risk
|
|
220
|
+
* engine wants one more check. Redirect to `err.stepUpUrl`.
|
|
221
|
+
* - `sign_in_denied` (`AuthioSignInDeniedError`) when the risk engine
|
|
222
|
+
* or a project policy refuses the sign-in. `err.message` is
|
|
223
|
+
* auth-core's readable reason.
|
|
224
|
+
* - `invalid_response` if auth-core answered without a session.
|
|
225
|
+
* - Whatever else auth-core returns on the options or verify call.
|
|
131
226
|
*
|
|
132
227
|
* The returned `refreshToken` is included for completeness, but the
|
|
133
228
|
* SDK never persists it — auth-core's `/v1/auth/passkey/login/verify`
|
|
@@ -141,14 +236,14 @@ export async function signInWithPasskey(
|
|
|
141
236
|
throw new AuthioError({
|
|
142
237
|
code: "not_in_browser",
|
|
143
238
|
message:
|
|
144
|
-
"
|
|
239
|
+
"Passkey sign-in only works in a web browser. Call signInWithPasskey from client-side code.",
|
|
145
240
|
status: 0,
|
|
146
241
|
});
|
|
147
242
|
}
|
|
148
243
|
if (!navigator.credentials || !navigator.credentials.get) {
|
|
149
244
|
throw new AuthioError({
|
|
150
245
|
code: "webauthn_unsupported",
|
|
151
|
-
message:
|
|
246
|
+
message: AUTHIO_MESSAGES.webauthn_unsupported,
|
|
152
247
|
status: 0,
|
|
153
248
|
});
|
|
154
249
|
}
|
|
@@ -163,6 +258,14 @@ export async function signInWithPasskey(
|
|
|
163
258
|
fetchImpl: opts.fetch,
|
|
164
259
|
});
|
|
165
260
|
|
|
261
|
+
if (!options || typeof options.challenge !== "string" || !options.challenge) {
|
|
262
|
+
throw new AuthioError({
|
|
263
|
+
code: "invalid_response",
|
|
264
|
+
message: AUTHIO_MESSAGES.invalid_response,
|
|
265
|
+
status: 200,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
166
269
|
const publicKey: PublicKeyCredentialRequestOptions = {
|
|
167
270
|
challenge: base64UrlToBuffer(options.challenge),
|
|
168
271
|
rpId: options.rpId,
|
|
@@ -182,19 +285,20 @@ export async function signInWithPasskey(
|
|
|
182
285
|
signal: opts.signal,
|
|
183
286
|
});
|
|
184
287
|
} catch (err) {
|
|
185
|
-
|
|
288
|
+
// Browser text ("The operation either timed out or was not
|
|
289
|
+
// allowed…") is not user-friendly; keep it on `cause` only.
|
|
290
|
+
const e = new AuthioError({
|
|
186
291
|
code: "webauthn_cancelled",
|
|
187
|
-
message:
|
|
188
|
-
err instanceof Error
|
|
189
|
-
? err.message
|
|
190
|
-
: "Passkey ceremony cancelled by user.",
|
|
292
|
+
message: AUTHIO_MESSAGES.webauthn_cancelled,
|
|
191
293
|
status: 0,
|
|
192
294
|
});
|
|
295
|
+
Object.defineProperty(e, "cause", { value: err, enumerable: false });
|
|
296
|
+
throw e;
|
|
193
297
|
}
|
|
194
298
|
if (!credential) {
|
|
195
299
|
throw new AuthioError({
|
|
196
300
|
code: "webauthn_cancelled",
|
|
197
|
-
message:
|
|
301
|
+
message: AUTHIO_MESSAGES.webauthn_cancelled,
|
|
198
302
|
status: 0,
|
|
199
303
|
});
|
|
200
304
|
}
|
|
@@ -223,30 +327,160 @@ export async function signInWithPasskey(
|
|
|
223
327
|
},
|
|
224
328
|
};
|
|
225
329
|
|
|
226
|
-
const verifyRes = await authioFetch<
|
|
330
|
+
const verifyRes = await authioFetch<SignInVerifyResponse>({
|
|
227
331
|
apiUrl: opts.apiUrl,
|
|
228
332
|
projectId: opts.projectId,
|
|
229
333
|
path: "/v1/auth/passkey/login/verify",
|
|
230
334
|
method: "POST",
|
|
231
|
-
body: {
|
|
335
|
+
body: {
|
|
336
|
+
credential: webauthnCredential,
|
|
337
|
+
...(opts.next ? { next: opts.next } : {}),
|
|
338
|
+
...(opts.organizationId ? { organization_id: opts.organizationId } : {}),
|
|
339
|
+
...(opts.xrayVisitorProof
|
|
340
|
+
? { xray_visitor_proof: opts.xrayVisitorProof }
|
|
341
|
+
: {}),
|
|
342
|
+
},
|
|
343
|
+
extraHeaders: deviceSignalsExtraHeaders(),
|
|
232
344
|
credentials: "include",
|
|
233
345
|
signal: opts.signal,
|
|
234
346
|
fetchImpl: opts.fetch,
|
|
235
347
|
});
|
|
236
348
|
|
|
237
|
-
return
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
349
|
+
return sessionFromResponse(verifyRes);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// ============================================================
|
|
353
|
+
// Email OTP (6-digit code) sign-in — M6
|
|
354
|
+
// ============================================================
|
|
355
|
+
|
|
356
|
+
export interface SendEmailOtpOptions {
|
|
357
|
+
/** Authio auth-core base URL. */
|
|
358
|
+
apiUrl: string;
|
|
359
|
+
/** Project ID (`proj_…`). */
|
|
360
|
+
projectId: string;
|
|
361
|
+
/** Email address to send the 6-digit code to. */
|
|
362
|
+
email: string;
|
|
363
|
+
/**
|
|
364
|
+
* Where auth-core should consider the sign-in to be headed. Optional —
|
|
365
|
+
* when omitted, auth-core derives it from the request Origin.
|
|
366
|
+
*/
|
|
367
|
+
redirectUri?: string;
|
|
368
|
+
/** Post-sign-in path on the app, carried through to the session redirect. */
|
|
369
|
+
next?: string;
|
|
370
|
+
/** Organization scope (`org_…`) for org-policy enforcement. */
|
|
371
|
+
organizationId?: string;
|
|
372
|
+
/** Opaque, consent-gated proof minted by @useauthio/xray. */
|
|
373
|
+
xrayVisitorProof?: string;
|
|
374
|
+
signal?: AbortSignal;
|
|
375
|
+
fetch?: typeof fetch;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* POSTs `/v1/auth/email-otp/send` — the same endpoint/body shape the
|
|
380
|
+
* hosted Lobby's "Email me a code" button uses. Returns `void` on
|
|
381
|
+
* success; the code is now in the user's inbox. Throws `AuthioError`
|
|
382
|
+
* on any failure (rate-limit, invalid email, org policy, network).
|
|
383
|
+
*
|
|
384
|
+
* Note: unlike `verifyEmailOtp`, the send endpoint does not accept
|
|
385
|
+
* `client_location` — capture location once and pass it to verify.
|
|
386
|
+
*/
|
|
387
|
+
export async function sendEmailOtp(opts: SendEmailOtpOptions): Promise<void> {
|
|
388
|
+
await authioFetch<{ expires_in?: number }>({
|
|
389
|
+
apiUrl: opts.apiUrl,
|
|
390
|
+
projectId: opts.projectId,
|
|
391
|
+
path: "/v1/auth/email-otp/send",
|
|
392
|
+
method: "POST",
|
|
393
|
+
body: {
|
|
394
|
+
email: opts.email,
|
|
395
|
+
...(opts.redirectUri ? { redirect_uri: opts.redirectUri } : {}),
|
|
396
|
+
...(opts.next ? { next: opts.next } : {}),
|
|
397
|
+
...(opts.organizationId ? { organization_id: opts.organizationId } : {}),
|
|
398
|
+
...(opts.xrayVisitorProof
|
|
399
|
+
? { xray_visitor_proof: opts.xrayVisitorProof }
|
|
400
|
+
: {}),
|
|
248
401
|
},
|
|
249
|
-
|
|
402
|
+
signal: opts.signal,
|
|
403
|
+
fetchImpl: opts.fetch,
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
export interface VerifyEmailOtpOptions {
|
|
408
|
+
apiUrl: string;
|
|
409
|
+
projectId: string;
|
|
410
|
+
/** Email address the code was sent to. */
|
|
411
|
+
email: string;
|
|
412
|
+
/** The 6-digit code from the user's inbox. */
|
|
413
|
+
code: string;
|
|
414
|
+
/** Fallback redirect target when the send call didn't record one. */
|
|
415
|
+
redirectUri?: string;
|
|
416
|
+
/** Post-sign-in path on the app. */
|
|
417
|
+
next?: string;
|
|
418
|
+
/** Organization scope (`org_…`) for org-policy enforcement. */
|
|
419
|
+
organizationId?: string;
|
|
420
|
+
/** Optional browser geolocation for Authio Locate attestation. */
|
|
421
|
+
clientLocation?: ClientLocationCapture;
|
|
422
|
+
/** Optional coarse device signals for risk / device recognition. */
|
|
423
|
+
deviceSignals?: DeviceSignalsCapture;
|
|
424
|
+
/** Opaque, consent-gated proof minted by @useauthio/xray. */
|
|
425
|
+
xrayVisitorProof?: string;
|
|
426
|
+
signal?: AbortSignal;
|
|
427
|
+
fetch?: typeof fetch;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export interface VerifyEmailOtpResult {
|
|
431
|
+
accessToken: string;
|
|
432
|
+
refreshToken?: string;
|
|
433
|
+
/** Effective session policy from the envelope, `null` on older auth-core. */
|
|
434
|
+
sessionPolicy: SessionPolicy | null;
|
|
435
|
+
user: AuthioUser;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* POSTs `/v1/auth/email-otp/verify` with the code the user typed —
|
|
440
|
+
* mirrors the hosted Lobby's verify body shape. On success returns
|
|
441
|
+
* `{ accessToken, refreshToken, user }`; auth-core also sets the
|
|
442
|
+
* session cookie via `Set-Cookie` (the request runs with
|
|
443
|
+
* `credentials: "include"`), which is the canonical storage.
|
|
444
|
+
*
|
|
445
|
+
* Throws `AuthioError`:
|
|
446
|
+
*
|
|
447
|
+
* - `invalid_code` for a wrong/expired code (auth-core's code).
|
|
448
|
+
* - `step_up_required` (`AuthioStepUpRequiredError`) when the risk
|
|
449
|
+
* engine withholds the session pending a second factor. Redirect to
|
|
450
|
+
* `err.stepUpUrl` to finish the sign-in on the hosted UI.
|
|
451
|
+
* - `sign_in_denied` (`AuthioSignInDeniedError`) when the sign-in is
|
|
452
|
+
* refused by risk or project policy.
|
|
453
|
+
* - `invalid_response` if auth-core answered without a session.
|
|
454
|
+
* - Whatever else auth-core returns (rate-limit, policy, etc.).
|
|
455
|
+
*/
|
|
456
|
+
export async function verifyEmailOtp(
|
|
457
|
+
opts: VerifyEmailOtpOptions,
|
|
458
|
+
): Promise<VerifyEmailOtpResult> {
|
|
459
|
+
const verifyRes = await authioFetch<SignInVerifyResponse>({
|
|
460
|
+
apiUrl: opts.apiUrl,
|
|
461
|
+
projectId: opts.projectId,
|
|
462
|
+
path: "/v1/auth/email-otp/verify",
|
|
463
|
+
method: "POST",
|
|
464
|
+
body: {
|
|
465
|
+
email: opts.email,
|
|
466
|
+
code: opts.code,
|
|
467
|
+
...(opts.redirectUri ? { redirect_uri: opts.redirectUri } : {}),
|
|
468
|
+
...(opts.next ? { next: opts.next } : {}),
|
|
469
|
+
...(opts.organizationId ? { organization_id: opts.organizationId } : {}),
|
|
470
|
+
...(opts.clientLocation ? { client_location: opts.clientLocation } : {}),
|
|
471
|
+
...(opts.deviceSignals ? { device_signals: opts.deviceSignals } : {}),
|
|
472
|
+
...(opts.xrayVisitorProof
|
|
473
|
+
? { xray_visitor_proof: opts.xrayVisitorProof }
|
|
474
|
+
: {}),
|
|
475
|
+
},
|
|
476
|
+
credentials: "include",
|
|
477
|
+
signal: opts.signal,
|
|
478
|
+
fetchImpl: opts.fetch,
|
|
479
|
+
});
|
|
480
|
+
|
|
481
|
+
// The risk engine may withhold the session (decision: "step_up") —
|
|
482
|
+
// sessionFromResponse turns that into a typed error with stepUpUrl.
|
|
483
|
+
return sessionFromResponse(verifyRes);
|
|
250
484
|
}
|
|
251
485
|
|
|
252
486
|
function base64UrlToBuffer(b64u: string): ArrayBuffer {
|
package/src/telemetry.ts
CHANGED
|
@@ -20,6 +20,10 @@
|
|
|
20
20
|
* - `refresh_scheduled`: a future silent refresh has been timer-
|
|
21
21
|
* scheduled. `runAt` is the unix millisecond at which the refresh
|
|
22
22
|
* will fire (subject to the visibilitychange deferral).
|
|
23
|
+
* - `refresh_deferred`: a scheduled refresh was held back — `hidden`
|
|
24
|
+
* when the tab is in the background, `idle` when the user has not
|
|
25
|
+
* interacted since the last refresh and the session policy has an
|
|
26
|
+
* inactivity timeout. `refresh_resumed` follows when it re-arms.
|
|
23
27
|
* - `sign_in_started` / `sign_in_completed` / `sign_in_failed`: the
|
|
24
28
|
* three terminal states of a sign-in attempt. `method` says which
|
|
25
29
|
* surface initiated it.
|
|
@@ -42,6 +46,13 @@ export type AuthioTelemetryEvent =
|
|
|
42
46
|
attempt: number;
|
|
43
47
|
}
|
|
44
48
|
| { kind: "refresh_scheduled"; timestamp: number; runAt: number }
|
|
49
|
+
| {
|
|
50
|
+
kind: "refresh_deferred";
|
|
51
|
+
timestamp: number;
|
|
52
|
+
/** `hidden`: background tab. `idle`: no user activity under an inactivity policy. */
|
|
53
|
+
reason: "hidden" | "idle";
|
|
54
|
+
}
|
|
55
|
+
| { kind: "refresh_resumed"; timestamp: number }
|
|
45
56
|
| {
|
|
46
57
|
kind: "sign_in_started";
|
|
47
58
|
timestamp: number;
|
package/src/types.ts
CHANGED
|
@@ -25,6 +25,61 @@ export interface AuthioUser {
|
|
|
25
25
|
*/
|
|
26
26
|
export type AuthioStatus = "loading" | "authenticated" | "unauthenticated";
|
|
27
27
|
|
|
28
|
+
/**
|
|
29
|
+
* Effective session-lifecycle policy for the current session, as reported
|
|
30
|
+
* by auth-core in the `session_policy` field of every session envelope
|
|
31
|
+
* (sign-in, refresh, switch-org). Values are minutes; `0` means "no limit
|
|
32
|
+
* at this layer". `null` on the context when auth-core predates the field.
|
|
33
|
+
*
|
|
34
|
+
* The SDK uses `idleTimeoutMin` to decide whether a timer-driven silent
|
|
35
|
+
* refresh should be held back while the user is inactive — see the
|
|
36
|
+
* `idleRefresh` provider prop.
|
|
37
|
+
*/
|
|
38
|
+
export interface SessionPolicy {
|
|
39
|
+
idleTimeoutMin: number;
|
|
40
|
+
absoluteMaxMin: number;
|
|
41
|
+
accessTokenTtlMin: number;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Wire shape of `session_policy` on auth-core envelopes. */
|
|
45
|
+
export interface RawSessionPolicy {
|
|
46
|
+
idle_timeout_min?: number;
|
|
47
|
+
absolute_max_min?: number;
|
|
48
|
+
access_token_ttl_min?: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Normalise a raw `session_policy` object. Returns `null` when the field
|
|
53
|
+
* is absent or malformed (older auth-core), so callers can keep the
|
|
54
|
+
* legacy always-refresh behaviour.
|
|
55
|
+
*/
|
|
56
|
+
export function coerceSessionPolicy(
|
|
57
|
+
raw: RawSessionPolicy | null | undefined,
|
|
58
|
+
): SessionPolicy | null {
|
|
59
|
+
if (!raw || typeof raw !== "object") return null;
|
|
60
|
+
const n = (v: unknown): number =>
|
|
61
|
+
typeof v === "number" && Number.isFinite(v) && v > 0 ? Math.floor(v) : 0;
|
|
62
|
+
return {
|
|
63
|
+
idleTimeoutMin: n(raw.idle_timeout_min),
|
|
64
|
+
absoluteMaxMin: n(raw.absolute_max_min),
|
|
65
|
+
accessTokenTtlMin: n(raw.access_token_ttl_min),
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* How the silent-refresh timer behaves while the user is inactive.
|
|
71
|
+
*
|
|
72
|
+
* - `"defer"` (default): when the effective policy has an inactivity
|
|
73
|
+
* timeout (`sessionPolicy.idleTimeoutMin > 0`) and there has been no
|
|
74
|
+
* user interaction since the last refresh, the scheduled refresh is
|
|
75
|
+
* held until the next interaction. Without an inactivity policy the
|
|
76
|
+
* refresh always runs — identical to previous SDK versions.
|
|
77
|
+
* - `"always"`: never hold a refresh for inactivity (tab-hidden
|
|
78
|
+
* deferral still applies). Escape hatch for apps whose background
|
|
79
|
+
* work needs a warm token even when nobody is at the keyboard.
|
|
80
|
+
*/
|
|
81
|
+
export type AuthioIdleRefreshMode = "defer" | "always";
|
|
82
|
+
|
|
28
83
|
export type AuthioStorageMode =
|
|
29
84
|
| "memory"
|
|
30
85
|
| "localStorage"
|
|
@@ -35,6 +90,12 @@ export interface AuthioContextValue {
|
|
|
35
90
|
user: AuthioUser | null;
|
|
36
91
|
status: AuthioStatus;
|
|
37
92
|
accessToken: string | null;
|
|
93
|
+
/**
|
|
94
|
+
* Effective session-lifecycle policy reported by auth-core on the last
|
|
95
|
+
* envelope, or `null` when unknown (no session yet, or an auth-core
|
|
96
|
+
* that predates `session_policy`).
|
|
97
|
+
*/
|
|
98
|
+
sessionPolicy: SessionPolicy | null;
|
|
38
99
|
/** Same `apiUrl` passed to `<AuthioProvider>`. */
|
|
39
100
|
apiUrl: string;
|
|
40
101
|
/** Same `projectId` passed to `<AuthioProvider>`. */
|
|
@@ -94,6 +155,8 @@ export interface AuthioContextValue {
|
|
|
94
155
|
accessToken: string;
|
|
95
156
|
refreshToken?: string | null;
|
|
96
157
|
user?: AuthioUser | null;
|
|
158
|
+
/** Policy from the sign-in envelope, when the helper surfaced one. */
|
|
159
|
+
sessionPolicy?: SessionPolicy | null;
|
|
97
160
|
}) => Promise<void>;
|
|
98
161
|
}
|
|
99
162
|
|
|
@@ -129,6 +192,14 @@ export interface AuthioProviderProps {
|
|
|
129
192
|
* a background tab.
|
|
130
193
|
*/
|
|
131
194
|
refreshLeadSeconds?: number;
|
|
195
|
+
/**
|
|
196
|
+
* Silent-refresh behaviour while the user is inactive. Defaults to
|
|
197
|
+
* `"defer"`: under a project/org inactivity policy the timer-driven
|
|
198
|
+
* refresh waits for the next user interaction instead of keeping the
|
|
199
|
+
* session warm on an idle tab. Projects without an inactivity policy
|
|
200
|
+
* are unaffected. See {@link AuthioIdleRefreshMode}.
|
|
201
|
+
*/
|
|
202
|
+
idleRefresh?: AuthioIdleRefreshMode;
|
|
132
203
|
/**
|
|
133
204
|
* Optional sink for SDK telemetry (refresh outcomes, sign-in
|
|
134
205
|
* starts/completes, token verification results). Useful for
|
package/src/usePasskeys.ts
CHANGED
|
@@ -7,6 +7,15 @@ import {
|
|
|
7
7
|
revokePasskey,
|
|
8
8
|
type AuthioPasskey,
|
|
9
9
|
} from "./passkeys";
|
|
10
|
+
import { AUTHIO_MESSAGES, AuthioError } from "./errors";
|
|
11
|
+
|
|
12
|
+
function notSignedIn(): AuthioError {
|
|
13
|
+
return new AuthioError({
|
|
14
|
+
code: "not_signed_in",
|
|
15
|
+
message: AUTHIO_MESSAGES.not_signed_in,
|
|
16
|
+
status: 401,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
10
19
|
|
|
11
20
|
export interface UsePasskeysResult {
|
|
12
21
|
passkeys: AuthioPasskey[];
|
|
@@ -50,7 +59,7 @@ export function usePasskeys(): UsePasskeysResult {
|
|
|
50
59
|
});
|
|
51
60
|
setPasskeys(rows);
|
|
52
61
|
} catch (err) {
|
|
53
|
-
setError(err instanceof
|
|
62
|
+
setError(err instanceof AuthioError ? err.message : "We couldn't load your passkeys. Please refresh the page and try again.");
|
|
54
63
|
setPasskeys([]);
|
|
55
64
|
} finally {
|
|
56
65
|
setLoading(false);
|
|
@@ -70,7 +79,7 @@ export function usePasskeys(): UsePasskeysResult {
|
|
|
70
79
|
const rename = useCallback(
|
|
71
80
|
async (credentialId: string, nickname: string) => {
|
|
72
81
|
const token = await ctx.getAccessToken();
|
|
73
|
-
if (!token) throw
|
|
82
|
+
if (!token) throw notSignedIn();
|
|
74
83
|
await renamePasskey(
|
|
75
84
|
{
|
|
76
85
|
apiUrl: ctx.apiUrl,
|
|
@@ -89,7 +98,7 @@ export function usePasskeys(): UsePasskeysResult {
|
|
|
89
98
|
const revoke = useCallback(
|
|
90
99
|
async (credentialId: string) => {
|
|
91
100
|
const token = await ctx.getAccessToken();
|
|
92
|
-
if (!token) throw
|
|
101
|
+
if (!token) throw notSignedIn();
|
|
93
102
|
await revokePasskey(
|
|
94
103
|
{
|
|
95
104
|
apiUrl: ctx.apiUrl,
|
|
@@ -108,7 +117,7 @@ export function usePasskeys(): UsePasskeysResult {
|
|
|
108
117
|
async (opts?: { returnUrl?: string; next?: string }) => {
|
|
109
118
|
const token = await ctx.getAccessToken();
|
|
110
119
|
const email = ctx.user?.email;
|
|
111
|
-
if (!token || !email) throw
|
|
120
|
+
if (!token || !email) throw notSignedIn();
|
|
112
121
|
await enrollPasskey({
|
|
113
122
|
apiUrl: ctx.apiUrl,
|
|
114
123
|
projectId: ctx.projectId,
|
package/src/version.ts
CHANGED