@robelest/convex-auth 0.0.2-preview.2 → 0.0.2
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/dist/bin.cjs +466 -63
- package/dist/client/index.d.ts +127 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js +414 -0
- package/dist/client/index.js.map +1 -1
- package/dist/component/_generated/api.d.ts +56 -1
- package/dist/component/_generated/api.d.ts.map +1 -1
- package/dist/component/_generated/api.js.map +1 -1
- package/dist/component/_generated/component.d.ts +93 -3
- package/dist/component/_generated/component.d.ts.map +1 -1
- package/dist/component/convex.config.d.ts.map +1 -1
- package/dist/component/convex.config.js +2 -0
- package/dist/component/convex.config.js.map +1 -1
- package/dist/component/index.d.ts +5 -3
- package/dist/component/index.d.ts.map +1 -1
- package/dist/component/index.js +5 -3
- package/dist/component/index.js.map +1 -1
- package/dist/component/portalBridge.d.ts +80 -0
- package/dist/component/portalBridge.d.ts.map +1 -0
- package/dist/component/portalBridge.js +102 -0
- package/dist/component/portalBridge.js.map +1 -0
- package/dist/component/public.d.ts +193 -9
- package/dist/component/public.d.ts.map +1 -1
- package/dist/component/public.js +204 -33
- package/dist/component/public.js.map +1 -1
- package/dist/component/schema.d.ts +89 -9
- package/dist/component/schema.d.ts.map +1 -1
- package/dist/component/schema.js +68 -7
- package/dist/component/schema.js.map +1 -1
- package/dist/providers/passkey.d.ts +20 -0
- package/dist/providers/passkey.d.ts.map +1 -0
- package/dist/providers/passkey.js +32 -0
- package/dist/providers/passkey.js.map +1 -0
- package/dist/providers/totp.d.ts +14 -0
- package/dist/providers/totp.d.ts.map +1 -0
- package/dist/providers/totp.js +23 -0
- package/dist/providers/totp.js.map +1 -0
- package/dist/server/convex-auth.d.ts +243 -0
- package/dist/server/convex-auth.d.ts.map +1 -0
- package/dist/server/convex-auth.js +365 -0
- package/dist/server/convex-auth.js.map +1 -0
- package/dist/server/implementation/index.d.ts +80 -7
- package/dist/server/implementation/index.d.ts.map +1 -1
- package/dist/server/implementation/index.js +88 -5
- package/dist/server/implementation/index.js.map +1 -1
- package/dist/server/implementation/passkey.d.ts +33 -0
- package/dist/server/implementation/passkey.d.ts.map +1 -0
- package/dist/server/implementation/passkey.js +450 -0
- package/dist/server/implementation/passkey.js.map +1 -0
- package/dist/server/implementation/redirects.d.ts.map +1 -1
- package/dist/server/implementation/redirects.js +4 -9
- package/dist/server/implementation/redirects.js.map +1 -1
- package/dist/server/implementation/signIn.d.ts +13 -0
- package/dist/server/implementation/signIn.d.ts.map +1 -1
- package/dist/server/implementation/signIn.js +26 -1
- package/dist/server/implementation/signIn.js.map +1 -1
- package/dist/server/implementation/totp.d.ts +40 -0
- package/dist/server/implementation/totp.d.ts.map +1 -0
- package/dist/server/implementation/totp.js +211 -0
- package/dist/server/implementation/totp.js.map +1 -0
- package/dist/server/portal-email.d.ts +19 -0
- package/dist/server/portal-email.d.ts.map +1 -0
- package/dist/server/portal-email.js +89 -0
- package/dist/server/portal-email.js.map +1 -0
- package/dist/server/portal.d.ts +116 -0
- package/dist/server/portal.d.ts.map +1 -0
- package/dist/server/portal.js +294 -0
- package/dist/server/portal.js.map +1 -0
- package/dist/server/provider_utils.d.ts +1 -1
- package/dist/server/provider_utils.d.ts.map +1 -1
- package/dist/server/provider_utils.js +39 -1
- package/dist/server/provider_utils.js.map +1 -1
- package/dist/server/types.d.ts +58 -2
- package/dist/server/types.d.ts.map +1 -1
- package/package.json +5 -2
- package/src/cli/index.ts +48 -6
- package/src/cli/portal-link.ts +112 -0
- package/src/cli/portal-upload.ts +411 -0
- package/src/client/index.ts +477 -0
- package/src/component/_generated/api.ts +72 -1
- package/src/component/_generated/component.ts +180 -4
- package/src/component/convex.config.ts +3 -0
- package/src/component/index.ts +5 -3
- package/src/component/portalBridge.ts +116 -0
- package/src/component/public.ts +231 -37
- package/src/component/schema.ts +70 -7
- package/src/providers/passkey.ts +35 -0
- package/src/providers/totp.ts +26 -0
- package/src/server/convex-auth.ts +470 -0
- package/src/server/implementation/index.ts +109 -8
- package/src/server/implementation/passkey.ts +650 -0
- package/src/server/implementation/redirects.ts +4 -11
- package/src/server/implementation/signIn.ts +39 -1
- package/src/server/implementation/totp.ts +366 -0
- package/src/server/portal-email.ts +95 -0
- package/src/server/portal.ts +375 -0
- package/src/server/provider_utils.ts +42 -1
- package/src/server/types.ts +66 -2
package/dist/client/index.d.ts
CHANGED
|
@@ -20,6 +20,8 @@ export interface Storage {
|
|
|
20
20
|
type SignInResult = {
|
|
21
21
|
signingIn: boolean;
|
|
22
22
|
redirect?: URL;
|
|
23
|
+
totpRequired?: boolean;
|
|
24
|
+
verifier?: string;
|
|
23
25
|
};
|
|
24
26
|
/** Reactive auth state snapshot returned by `auth.state` and `auth.onChange`. */
|
|
25
27
|
export type AuthState = {
|
|
@@ -98,6 +100,131 @@ export declare function client(options: ClientOptions): {
|
|
|
98
100
|
signIn: (provider?: string, args?: FormData | Record<string, Value>) => Promise<SignInResult>;
|
|
99
101
|
signOut: () => Promise<void>;
|
|
100
102
|
onChange: (cb: (state: AuthState) => void) => (() => void);
|
|
103
|
+
/** Passkey (WebAuthn) authentication helpers. */
|
|
104
|
+
passkey: {
|
|
105
|
+
/**
|
|
106
|
+
* Check if WebAuthn passkeys are supported in the current environment.
|
|
107
|
+
*/
|
|
108
|
+
isSupported: () => boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Check if conditional UI (autofill-assisted passkey sign-in) is supported.
|
|
111
|
+
*
|
|
112
|
+
* ```ts
|
|
113
|
+
* if (await auth.passkey.isAutofillSupported()) {
|
|
114
|
+
* auth.passkey.authenticate({ autofill: true });
|
|
115
|
+
* }
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
isAutofillSupported: () => Promise<boolean>;
|
|
119
|
+
/**
|
|
120
|
+
* Register a new passkey for the current or new user.
|
|
121
|
+
*
|
|
122
|
+
* Performs the full two-round-trip WebAuthn registration ceremony:
|
|
123
|
+
* 1. Requests creation options from the server (challenge, RP info)
|
|
124
|
+
* 2. Calls `navigator.credentials.create()` with the options
|
|
125
|
+
* 3. Sends the attestation back to the server for verification
|
|
126
|
+
* 4. Server creates user + account + passkey records and returns tokens
|
|
127
|
+
*
|
|
128
|
+
* Works in both SPA and proxy (SSR) modes.
|
|
129
|
+
*
|
|
130
|
+
* ```ts
|
|
131
|
+
* await auth.passkey.register({ name: "MacBook Touch ID" });
|
|
132
|
+
* ```
|
|
133
|
+
*
|
|
134
|
+
* @param opts.name - Friendly name for this passkey
|
|
135
|
+
* @param opts.email - Email to associate with the new account
|
|
136
|
+
* @param opts.userName - Username for the credential (defaults to email)
|
|
137
|
+
* @param opts.userDisplayName - Display name for the credential
|
|
138
|
+
* @returns `{ signingIn: true }` on success
|
|
139
|
+
*/
|
|
140
|
+
register: (opts?: {
|
|
141
|
+
name?: string;
|
|
142
|
+
email?: string;
|
|
143
|
+
userName?: string;
|
|
144
|
+
userDisplayName?: string;
|
|
145
|
+
}) => Promise<SignInResult>;
|
|
146
|
+
/**
|
|
147
|
+
* Authenticate with an existing passkey.
|
|
148
|
+
*
|
|
149
|
+
* Performs the full two-round-trip WebAuthn authentication ceremony:
|
|
150
|
+
* 1. Requests assertion options from the server (challenge, allowed credentials)
|
|
151
|
+
* 2. Calls `navigator.credentials.get()` with the options
|
|
152
|
+
* 3. Sends the assertion back to the server for signature verification
|
|
153
|
+
* 4. Server verifies signature, updates counter, creates session, returns tokens
|
|
154
|
+
*
|
|
155
|
+
* Works in both SPA and proxy (SSR) modes.
|
|
156
|
+
*
|
|
157
|
+
* ```ts
|
|
158
|
+
* // Discoverable credential (no email needed)
|
|
159
|
+
* await auth.passkey.authenticate();
|
|
160
|
+
*
|
|
161
|
+
* // Scoped to a specific user's credentials
|
|
162
|
+
* await auth.passkey.authenticate({ email: "user@example.com" });
|
|
163
|
+
*
|
|
164
|
+
* // Autofill-assisted (conditional UI)
|
|
165
|
+
* await auth.passkey.authenticate({ autofill: true });
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
168
|
+
* @param opts.email - Scope to credentials for this email's user
|
|
169
|
+
* @param opts.autofill - Use conditional mediation (autofill UI)
|
|
170
|
+
* @returns `{ signingIn: true }` on success
|
|
171
|
+
*/
|
|
172
|
+
authenticate: (opts?: {
|
|
173
|
+
email?: string;
|
|
174
|
+
autofill?: boolean;
|
|
175
|
+
}) => Promise<SignInResult>;
|
|
176
|
+
};
|
|
177
|
+
/** TOTP two-factor authentication helpers. */
|
|
178
|
+
totp: {
|
|
179
|
+
/**
|
|
180
|
+
* Start TOTP enrollment. Must be authenticated.
|
|
181
|
+
*
|
|
182
|
+
* Returns a URI for QR code display and a base32 secret for manual entry.
|
|
183
|
+
*
|
|
184
|
+
* ```ts
|
|
185
|
+
* const setup = await auth.totp.setup();
|
|
186
|
+
* // Display QR code from setup.uri
|
|
187
|
+
* // Or show setup.secret for manual entry
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
setup: (opts?: {
|
|
191
|
+
name?: string;
|
|
192
|
+
accountName?: string;
|
|
193
|
+
}) => Promise<{
|
|
194
|
+
uri: string;
|
|
195
|
+
secret: string;
|
|
196
|
+
verifier: string;
|
|
197
|
+
totpId: string;
|
|
198
|
+
}>;
|
|
199
|
+
/**
|
|
200
|
+
* Complete TOTP enrollment by verifying the first code from the authenticator app.
|
|
201
|
+
*
|
|
202
|
+
* ```ts
|
|
203
|
+
* await auth.totp.confirm({ code: "123456", verifier: setup.verifier, totpId: setup.totpId });
|
|
204
|
+
* ```
|
|
205
|
+
*/
|
|
206
|
+
confirm: (opts: {
|
|
207
|
+
code: string;
|
|
208
|
+
verifier: string;
|
|
209
|
+
totpId: string;
|
|
210
|
+
}) => Promise<void>;
|
|
211
|
+
/**
|
|
212
|
+
* Complete 2FA verification during sign-in.
|
|
213
|
+
*
|
|
214
|
+
* Called after a credentials sign-in returns `totpRequired: true`.
|
|
215
|
+
*
|
|
216
|
+
* ```ts
|
|
217
|
+
* const result = await auth.signIn("password", { email, password });
|
|
218
|
+
* if (result.totpRequired) {
|
|
219
|
+
* await auth.totp.verify({ code: "123456", verifier: result.verifier! });
|
|
220
|
+
* }
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
verify: (opts: {
|
|
224
|
+
code: string;
|
|
225
|
+
verifier: string;
|
|
226
|
+
}) => Promise<void>;
|
|
227
|
+
};
|
|
101
228
|
};
|
|
102
229
|
export {};
|
|
103
230
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/client/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAEtC;;;;GAIG;AACH,UAAU,eAAe;IACvB,MAAM,CAAC,MAAM,EAAE,GAAG,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC;IAC7C,OAAO,CACL,UAAU,EAAE,CAAC,IAAI,EAAE;QACjB,iBAAiB,EAAE,OAAO,CAAC;KAC5B,KAAK,OAAO,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,EACxC,QAAQ,CAAC,EAAE,CAAC,eAAe,EAAE,OAAO,KAAK,IAAI,GAC5C,IAAI,CAAC;IACR,SAAS,IAAI,IAAI,CAAC;CACnB;AAED,gEAAgE;AAChE,MAAM,WAAW,OAAO;IACtB,OAAO,CACL,GAAG,EAAE,MAAM,GACV,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;IAClE,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC1D,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC/C;AAOD,KAAK,YAAY,GAAG;IAClB,SAAS,EAAE,O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OAAO,CAAC,YAAY,CAAC;;IA8OxB,8CAA8C;;QAtI9C;;;;;;;;;;WAUG;uBAEM;YAAE,IAAI,CAAC,EAAE,MAAM,CAAC;YAAC,WAAW,CAAC,EAAE,MAAM,CAAA;SAAE,KAC7C,OAAO,CAAC;YAAE,GAAG,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,MAAM,CAAC;YAAC,QAAQ,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,MAAM,CAAA;SAAE,CAAC;QAoB7E;;;;;;WAMG;wBACmB;YACpB,IAAI,EAAE,MAAM,CAAC;YACb,QAAQ,EAAE,MAAM,CAAC;YACjB,MAAM,EAAE,MAAM,CAAC;SAChB,KAAG,OAAO,CAAC,IAAI,CAAC;QAkCjB;;;;;;;;;;;WAWG;uBACkB;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,QAAQ,EAAE,MAAM,CAAA;SAAE,KAAG,OAAO,CAAC,IAAI,CAAC;;EA+C1E"}
|
package/dist/client/index.js
CHANGED
|
@@ -212,6 +212,9 @@ export function client(options) {
|
|
|
212
212
|
}
|
|
213
213
|
return { signingIn: false, redirect: redirectUrl };
|
|
214
214
|
}
|
|
215
|
+
if (result.totpRequired) {
|
|
216
|
+
return { signingIn: false, totpRequired: true, verifier: result.verifier };
|
|
217
|
+
}
|
|
215
218
|
if (result.tokens !== undefined) {
|
|
216
219
|
// Proxy returns { token, refreshToken: "dummy" }.
|
|
217
220
|
// Store JWT in memory only — real refresh token is in httpOnly cookie.
|
|
@@ -239,6 +242,9 @@ export function client(options) {
|
|
|
239
242
|
}
|
|
240
243
|
return { signingIn: false, redirect: redirectUrl };
|
|
241
244
|
}
|
|
245
|
+
if (result.totpRequired) {
|
|
246
|
+
return { signingIn: false, totpRequired: true, verifier: result.verifier };
|
|
247
|
+
}
|
|
242
248
|
if (result.tokens !== undefined) {
|
|
243
249
|
await setToken({
|
|
244
250
|
shouldStore: true,
|
|
@@ -411,6 +417,410 @@ export function client(options) {
|
|
|
411
417
|
void hydrateFromStorage().then(() => handleCodeFlow());
|
|
412
418
|
}
|
|
413
419
|
}
|
|
420
|
+
// ---------------------------------------------------------------------------
|
|
421
|
+
// Passkey helpers
|
|
422
|
+
// ---------------------------------------------------------------------------
|
|
423
|
+
/**
|
|
424
|
+
* Base64url encode/decode helpers for the WebAuthn credential API.
|
|
425
|
+
* These run client-side only (browser context).
|
|
426
|
+
*/
|
|
427
|
+
const base64urlEncode = (buffer) => {
|
|
428
|
+
const bytes = new Uint8Array(buffer);
|
|
429
|
+
let binary = "";
|
|
430
|
+
for (let i = 0; i < bytes.byteLength; i++) {
|
|
431
|
+
binary += String.fromCharCode(bytes[i]);
|
|
432
|
+
}
|
|
433
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
434
|
+
};
|
|
435
|
+
const base64urlDecode = (str) => {
|
|
436
|
+
const padded = str.replace(/-/g, "+").replace(/_/g, "/");
|
|
437
|
+
const binary = atob(padded);
|
|
438
|
+
const bytes = new Uint8Array(binary.length);
|
|
439
|
+
for (let i = 0; i < binary.length; i++) {
|
|
440
|
+
bytes[i] = binary.charCodeAt(i);
|
|
441
|
+
}
|
|
442
|
+
return bytes;
|
|
443
|
+
};
|
|
444
|
+
const passkey = {
|
|
445
|
+
/**
|
|
446
|
+
* Check if WebAuthn passkeys are supported in the current environment.
|
|
447
|
+
*/
|
|
448
|
+
isSupported: () => {
|
|
449
|
+
return (typeof window !== "undefined" &&
|
|
450
|
+
typeof window.PublicKeyCredential !== "undefined");
|
|
451
|
+
},
|
|
452
|
+
/**
|
|
453
|
+
* Check if conditional UI (autofill-assisted passkey sign-in) is supported.
|
|
454
|
+
*
|
|
455
|
+
* ```ts
|
|
456
|
+
* if (await auth.passkey.isAutofillSupported()) {
|
|
457
|
+
* auth.passkey.authenticate({ autofill: true });
|
|
458
|
+
* }
|
|
459
|
+
* ```
|
|
460
|
+
*/
|
|
461
|
+
isAutofillSupported: async () => {
|
|
462
|
+
if (typeof window === "undefined")
|
|
463
|
+
return false;
|
|
464
|
+
if (typeof window.PublicKeyCredential === "undefined")
|
|
465
|
+
return false;
|
|
466
|
+
if (typeof window.PublicKeyCredential.isConditionalMediationAvailable !== "function") {
|
|
467
|
+
return false;
|
|
468
|
+
}
|
|
469
|
+
return window.PublicKeyCredential.isConditionalMediationAvailable();
|
|
470
|
+
},
|
|
471
|
+
/**
|
|
472
|
+
* Register a new passkey for the current or new user.
|
|
473
|
+
*
|
|
474
|
+
* Performs the full two-round-trip WebAuthn registration ceremony:
|
|
475
|
+
* 1. Requests creation options from the server (challenge, RP info)
|
|
476
|
+
* 2. Calls `navigator.credentials.create()` with the options
|
|
477
|
+
* 3. Sends the attestation back to the server for verification
|
|
478
|
+
* 4. Server creates user + account + passkey records and returns tokens
|
|
479
|
+
*
|
|
480
|
+
* Works in both SPA and proxy (SSR) modes.
|
|
481
|
+
*
|
|
482
|
+
* ```ts
|
|
483
|
+
* await auth.passkey.register({ name: "MacBook Touch ID" });
|
|
484
|
+
* ```
|
|
485
|
+
*
|
|
486
|
+
* @param opts.name - Friendly name for this passkey
|
|
487
|
+
* @param opts.email - Email to associate with the new account
|
|
488
|
+
* @param opts.userName - Username for the credential (defaults to email)
|
|
489
|
+
* @param opts.userDisplayName - Display name for the credential
|
|
490
|
+
* @returns `{ signingIn: true }` on success
|
|
491
|
+
*/
|
|
492
|
+
register: async (opts) => {
|
|
493
|
+
const phase1Params = {
|
|
494
|
+
flow: "register-options",
|
|
495
|
+
email: opts?.email,
|
|
496
|
+
userName: opts?.userName,
|
|
497
|
+
userDisplayName: opts?.userDisplayName,
|
|
498
|
+
};
|
|
499
|
+
// Phase 1: Get registration options from server
|
|
500
|
+
let phase1Result;
|
|
501
|
+
if (proxy) {
|
|
502
|
+
phase1Result = await proxyFetch({
|
|
503
|
+
action: "auth:signIn",
|
|
504
|
+
args: { provider: "passkey", params: phase1Params },
|
|
505
|
+
});
|
|
506
|
+
}
|
|
507
|
+
else {
|
|
508
|
+
phase1Result = await convex.action("auth:signIn", {
|
|
509
|
+
provider: "passkey",
|
|
510
|
+
params: phase1Params,
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
if (!phase1Result.options) {
|
|
514
|
+
throw new Error("Server did not return passkey registration options");
|
|
515
|
+
}
|
|
516
|
+
const options = phase1Result.options;
|
|
517
|
+
// Convert base64url strings to ArrayBuffers for the credential API
|
|
518
|
+
const createOptions = {
|
|
519
|
+
publicKey: {
|
|
520
|
+
rp: options.rp,
|
|
521
|
+
user: {
|
|
522
|
+
id: base64urlDecode(options.user.id).buffer,
|
|
523
|
+
name: options.user.name,
|
|
524
|
+
displayName: options.user.displayName,
|
|
525
|
+
},
|
|
526
|
+
challenge: base64urlDecode(options.challenge).buffer,
|
|
527
|
+
pubKeyCredParams: options.pubKeyCredParams,
|
|
528
|
+
timeout: options.timeout,
|
|
529
|
+
attestation: options.attestation,
|
|
530
|
+
authenticatorSelection: options.authenticatorSelection,
|
|
531
|
+
excludeCredentials: (options.excludeCredentials ?? []).map((cred) => ({
|
|
532
|
+
type: cred.type ?? "public-key",
|
|
533
|
+
id: base64urlDecode(cred.id).buffer,
|
|
534
|
+
transports: cred.transports,
|
|
535
|
+
})),
|
|
536
|
+
},
|
|
537
|
+
};
|
|
538
|
+
// Phase 2: Create credential via browser API
|
|
539
|
+
const credential = (await navigator.credentials.create(createOptions));
|
|
540
|
+
if (!credential) {
|
|
541
|
+
throw new Error("Passkey registration was cancelled");
|
|
542
|
+
}
|
|
543
|
+
const response = credential.response;
|
|
544
|
+
// Extract transports if available
|
|
545
|
+
const transports = typeof response.getTransports === "function"
|
|
546
|
+
? response.getTransports()
|
|
547
|
+
: undefined;
|
|
548
|
+
const phase2Params = {
|
|
549
|
+
flow: "register-verify",
|
|
550
|
+
clientDataJSON: base64urlEncode(response.clientDataJSON),
|
|
551
|
+
attestationObject: base64urlEncode(response.attestationObject),
|
|
552
|
+
transports,
|
|
553
|
+
passkeyName: opts?.name,
|
|
554
|
+
email: opts?.email,
|
|
555
|
+
};
|
|
556
|
+
// Phase 3: Send attestation to server for verification
|
|
557
|
+
let phase2Result;
|
|
558
|
+
if (proxy) {
|
|
559
|
+
// In proxy mode the verifier is stored in an httpOnly cookie by the proxy.
|
|
560
|
+
// We pass it back explicitly so the proxy can forward it to Convex.
|
|
561
|
+
phase2Result = await proxyFetch({
|
|
562
|
+
action: "auth:signIn",
|
|
563
|
+
args: {
|
|
564
|
+
provider: "passkey",
|
|
565
|
+
params: phase2Params,
|
|
566
|
+
verifier: phase1Result.verifier,
|
|
567
|
+
},
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
else {
|
|
571
|
+
phase2Result = await convex.action("auth:signIn", {
|
|
572
|
+
provider: "passkey",
|
|
573
|
+
params: phase2Params,
|
|
574
|
+
verifier: phase1Result.verifier,
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
if (phase2Result.tokens) {
|
|
578
|
+
if (proxy) {
|
|
579
|
+
await setToken({
|
|
580
|
+
shouldStore: false,
|
|
581
|
+
tokens: phase2Result.tokens === null
|
|
582
|
+
? null
|
|
583
|
+
: { token: phase2Result.tokens.token },
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
else {
|
|
587
|
+
await setToken({
|
|
588
|
+
shouldStore: true,
|
|
589
|
+
tokens: phase2Result.tokens,
|
|
590
|
+
});
|
|
591
|
+
}
|
|
592
|
+
return { signingIn: true };
|
|
593
|
+
}
|
|
594
|
+
return { signingIn: false };
|
|
595
|
+
},
|
|
596
|
+
/**
|
|
597
|
+
* Authenticate with an existing passkey.
|
|
598
|
+
*
|
|
599
|
+
* Performs the full two-round-trip WebAuthn authentication ceremony:
|
|
600
|
+
* 1. Requests assertion options from the server (challenge, allowed credentials)
|
|
601
|
+
* 2. Calls `navigator.credentials.get()` with the options
|
|
602
|
+
* 3. Sends the assertion back to the server for signature verification
|
|
603
|
+
* 4. Server verifies signature, updates counter, creates session, returns tokens
|
|
604
|
+
*
|
|
605
|
+
* Works in both SPA and proxy (SSR) modes.
|
|
606
|
+
*
|
|
607
|
+
* ```ts
|
|
608
|
+
* // Discoverable credential (no email needed)
|
|
609
|
+
* await auth.passkey.authenticate();
|
|
610
|
+
*
|
|
611
|
+
* // Scoped to a specific user's credentials
|
|
612
|
+
* await auth.passkey.authenticate({ email: "user@example.com" });
|
|
613
|
+
*
|
|
614
|
+
* // Autofill-assisted (conditional UI)
|
|
615
|
+
* await auth.passkey.authenticate({ autofill: true });
|
|
616
|
+
* ```
|
|
617
|
+
*
|
|
618
|
+
* @param opts.email - Scope to credentials for this email's user
|
|
619
|
+
* @param opts.autofill - Use conditional mediation (autofill UI)
|
|
620
|
+
* @returns `{ signingIn: true }` on success
|
|
621
|
+
*/
|
|
622
|
+
authenticate: async (opts) => {
|
|
623
|
+
const phase1Params = {
|
|
624
|
+
flow: "auth-options",
|
|
625
|
+
email: opts?.email,
|
|
626
|
+
};
|
|
627
|
+
// Phase 1: Get assertion options from server
|
|
628
|
+
let phase1Result;
|
|
629
|
+
if (proxy) {
|
|
630
|
+
phase1Result = await proxyFetch({
|
|
631
|
+
action: "auth:signIn",
|
|
632
|
+
args: { provider: "passkey", params: phase1Params },
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
else {
|
|
636
|
+
phase1Result = await convex.action("auth:signIn", {
|
|
637
|
+
provider: "passkey",
|
|
638
|
+
params: phase1Params,
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
if (!phase1Result.options) {
|
|
642
|
+
throw new Error("Server did not return passkey authentication options");
|
|
643
|
+
}
|
|
644
|
+
const options = phase1Result.options;
|
|
645
|
+
// Convert base64url strings to ArrayBuffers for the credential API
|
|
646
|
+
const getOptions = {
|
|
647
|
+
publicKey: {
|
|
648
|
+
challenge: base64urlDecode(options.challenge).buffer,
|
|
649
|
+
timeout: options.timeout,
|
|
650
|
+
rpId: options.rpId,
|
|
651
|
+
userVerification: options.userVerification,
|
|
652
|
+
allowCredentials: (options.allowCredentials ?? []).map((cred) => ({
|
|
653
|
+
type: cred.type ?? "public-key",
|
|
654
|
+
id: base64urlDecode(cred.id).buffer,
|
|
655
|
+
transports: cred.transports,
|
|
656
|
+
})),
|
|
657
|
+
},
|
|
658
|
+
...(opts?.autofill ? { mediation: "conditional" } : {}),
|
|
659
|
+
};
|
|
660
|
+
// Phase 2: Get credential via browser API
|
|
661
|
+
const credential = (await navigator.credentials.get(getOptions));
|
|
662
|
+
if (!credential) {
|
|
663
|
+
throw new Error("Passkey authentication was cancelled");
|
|
664
|
+
}
|
|
665
|
+
const response = credential.response;
|
|
666
|
+
const phase2Params = {
|
|
667
|
+
flow: "auth-verify",
|
|
668
|
+
credentialId: base64urlEncode(credential.rawId),
|
|
669
|
+
clientDataJSON: base64urlEncode(response.clientDataJSON),
|
|
670
|
+
authenticatorData: base64urlEncode(response.authenticatorData),
|
|
671
|
+
signature: base64urlEncode(response.signature),
|
|
672
|
+
};
|
|
673
|
+
// Phase 3: Send assertion to server for verification
|
|
674
|
+
let phase2Result;
|
|
675
|
+
if (proxy) {
|
|
676
|
+
phase2Result = await proxyFetch({
|
|
677
|
+
action: "auth:signIn",
|
|
678
|
+
args: {
|
|
679
|
+
provider: "passkey",
|
|
680
|
+
params: phase2Params,
|
|
681
|
+
verifier: phase1Result.verifier,
|
|
682
|
+
},
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
else {
|
|
686
|
+
phase2Result = await convex.action("auth:signIn", {
|
|
687
|
+
provider: "passkey",
|
|
688
|
+
params: phase2Params,
|
|
689
|
+
verifier: phase1Result.verifier,
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
if (phase2Result.tokens) {
|
|
693
|
+
if (proxy) {
|
|
694
|
+
await setToken({
|
|
695
|
+
shouldStore: false,
|
|
696
|
+
tokens: phase2Result.tokens === null
|
|
697
|
+
? null
|
|
698
|
+
: { token: phase2Result.tokens.token },
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
else {
|
|
702
|
+
await setToken({
|
|
703
|
+
shouldStore: true,
|
|
704
|
+
tokens: phase2Result.tokens,
|
|
705
|
+
});
|
|
706
|
+
}
|
|
707
|
+
return { signingIn: true };
|
|
708
|
+
}
|
|
709
|
+
return { signingIn: false };
|
|
710
|
+
},
|
|
711
|
+
};
|
|
712
|
+
const totp = {
|
|
713
|
+
/**
|
|
714
|
+
* Start TOTP enrollment. Must be authenticated.
|
|
715
|
+
*
|
|
716
|
+
* Returns a URI for QR code display and a base32 secret for manual entry.
|
|
717
|
+
*
|
|
718
|
+
* ```ts
|
|
719
|
+
* const setup = await auth.totp.setup();
|
|
720
|
+
* // Display QR code from setup.uri
|
|
721
|
+
* // Or show setup.secret for manual entry
|
|
722
|
+
* ```
|
|
723
|
+
*/
|
|
724
|
+
setup: async (opts) => {
|
|
725
|
+
const params = { flow: "setup" };
|
|
726
|
+
if (opts?.name)
|
|
727
|
+
params.name = opts.name;
|
|
728
|
+
if (opts?.accountName)
|
|
729
|
+
params.accountName = opts.accountName;
|
|
730
|
+
if (proxy) {
|
|
731
|
+
const result = await proxyFetch({
|
|
732
|
+
action: "auth:signIn",
|
|
733
|
+
args: { provider: "totp", params },
|
|
734
|
+
});
|
|
735
|
+
return { uri: result.totpSetup.uri, secret: result.totpSetup.secret, verifier: result.verifier, totpId: result.totpSetup.totpId };
|
|
736
|
+
}
|
|
737
|
+
const result = await convex.action("auth:signIn", {
|
|
738
|
+
provider: "totp",
|
|
739
|
+
params,
|
|
740
|
+
});
|
|
741
|
+
return { uri: result.totpSetup.uri, secret: result.totpSetup.secret, verifier: result.verifier, totpId: result.totpSetup.totpId };
|
|
742
|
+
},
|
|
743
|
+
/**
|
|
744
|
+
* Complete TOTP enrollment by verifying the first code from the authenticator app.
|
|
745
|
+
*
|
|
746
|
+
* ```ts
|
|
747
|
+
* await auth.totp.confirm({ code: "123456", verifier: setup.verifier, totpId: setup.totpId });
|
|
748
|
+
* ```
|
|
749
|
+
*/
|
|
750
|
+
confirm: async (opts) => {
|
|
751
|
+
const params = {
|
|
752
|
+
flow: "confirm",
|
|
753
|
+
code: opts.code,
|
|
754
|
+
totpId: opts.totpId,
|
|
755
|
+
};
|
|
756
|
+
if (proxy) {
|
|
757
|
+
const result = await proxyFetch({
|
|
758
|
+
action: "auth:signIn",
|
|
759
|
+
args: { provider: "totp", params, verifier: opts.verifier },
|
|
760
|
+
});
|
|
761
|
+
if (result.tokens) {
|
|
762
|
+
await setToken({
|
|
763
|
+
shouldStore: false,
|
|
764
|
+
tokens: result.tokens === null ? null : { token: result.tokens.token },
|
|
765
|
+
});
|
|
766
|
+
}
|
|
767
|
+
return;
|
|
768
|
+
}
|
|
769
|
+
const result = await convex.action("auth:signIn", {
|
|
770
|
+
provider: "totp",
|
|
771
|
+
params,
|
|
772
|
+
verifier: opts.verifier,
|
|
773
|
+
});
|
|
774
|
+
if (result.tokens) {
|
|
775
|
+
await setToken({
|
|
776
|
+
shouldStore: true,
|
|
777
|
+
tokens: result.tokens ?? null,
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
},
|
|
781
|
+
/**
|
|
782
|
+
* Complete 2FA verification during sign-in.
|
|
783
|
+
*
|
|
784
|
+
* Called after a credentials sign-in returns `totpRequired: true`.
|
|
785
|
+
*
|
|
786
|
+
* ```ts
|
|
787
|
+
* const result = await auth.signIn("password", { email, password });
|
|
788
|
+
* if (result.totpRequired) {
|
|
789
|
+
* await auth.totp.verify({ code: "123456", verifier: result.verifier! });
|
|
790
|
+
* }
|
|
791
|
+
* ```
|
|
792
|
+
*/
|
|
793
|
+
verify: async (opts) => {
|
|
794
|
+
const params = {
|
|
795
|
+
flow: "verify",
|
|
796
|
+
code: opts.code,
|
|
797
|
+
};
|
|
798
|
+
if (proxy) {
|
|
799
|
+
const result = await proxyFetch({
|
|
800
|
+
action: "auth:signIn",
|
|
801
|
+
args: { provider: "totp", params, verifier: opts.verifier },
|
|
802
|
+
});
|
|
803
|
+
if (result.tokens) {
|
|
804
|
+
await setToken({
|
|
805
|
+
shouldStore: false,
|
|
806
|
+
tokens: result.tokens === null ? null : { token: result.tokens.token },
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
return;
|
|
810
|
+
}
|
|
811
|
+
const result = await convex.action("auth:signIn", {
|
|
812
|
+
provider: "totp",
|
|
813
|
+
params,
|
|
814
|
+
verifier: opts.verifier,
|
|
815
|
+
});
|
|
816
|
+
if (result.tokens) {
|
|
817
|
+
await setToken({
|
|
818
|
+
shouldStore: true,
|
|
819
|
+
tokens: result.tokens ?? null,
|
|
820
|
+
});
|
|
821
|
+
}
|
|
822
|
+
},
|
|
823
|
+
};
|
|
414
824
|
return {
|
|
415
825
|
/** Current auth state snapshot. */
|
|
416
826
|
get state() {
|
|
@@ -419,6 +829,10 @@ export function client(options) {
|
|
|
419
829
|
signIn,
|
|
420
830
|
signOut,
|
|
421
831
|
onChange,
|
|
832
|
+
/** Passkey (WebAuthn) authentication helpers. */
|
|
833
|
+
passkey,
|
|
834
|
+
/** TOTP two-factor authentication helpers. */
|
|
835
|
+
totp,
|
|
422
836
|
};
|
|
423
837
|
}
|
|
424
838
|
// ---------------------------------------------------------------------------
|