@vvedantb/native-ui 0.32.0 → 0.34.0

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/auth/totp.js CHANGED
@@ -1,7 +1,8 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import { useState } from "react";
3
- import { useSignIn } from "@clerk/expo/legacy";
3
+ import { useSignIn } from "@clerk/expo";
4
4
  import { authErrorMessage, DEFAULT_STAGE_TIMEOUT_MS, isAuthTimeoutError, logAuthFailure, withAuthTimeout } from "./authErrors";
5
+ import { signInCalls } from "./clerkCore3";
5
6
 
6
7
  /** Logs a timed-out stage. Other failures are not logged here. */
7
8
  const logTimeout = (stage, error) => {
@@ -250,7 +251,7 @@ export function submitTotpSecondFactor(args) {
250
251
  * `{ ok: true }` the session is active: navigate in the caller.
251
252
  */
252
253
  export function useSecondFactor(t0) {
253
- const $ = _c(22);
254
+ const $ = _c(21);
254
255
  let t1;
255
256
  if ($[0] !== t0) {
256
257
  t1 = t0 === undefined ? {} : t0;
@@ -260,37 +261,35 @@ export function useSecondFactor(t0) {
260
261
  t1 = $[1];
261
262
  }
262
263
  const options = t1;
263
- const {
264
- isLoaded,
265
- signIn,
266
- setActive
267
- } = useSignIn();
264
+ const t2 = useSignIn();
265
+ let t3;
266
+ if ($[2] !== t2.signIn) {
267
+ t3 = signInCalls(t2.signIn);
268
+ $[2] = t2.signIn;
269
+ $[3] = t3;
270
+ } else {
271
+ t3 = $[3];
272
+ }
273
+ const signIn = t3;
268
274
  const [chosen, setChosen] = useState(null);
269
275
  const strategy = chosen ?? options.strategy ?? "totp";
270
276
  const [loading, setLoading] = useState(false);
271
277
  const [error, setError] = useState("");
272
278
  const timeoutMs = options.stageTimeoutMs ?? DEFAULT_STAGE_TIMEOUT_MS;
273
- let t2;
274
- if ($[2] !== error || $[3] !== isLoaded || $[4] !== loading || $[5] !== setActive || $[6] !== signIn || $[7] !== strategy || $[8] !== timeoutMs) {
275
- t2 = async code => {
279
+ let t4;
280
+ if ($[4] !== error || $[5] !== loading || $[6] !== signIn || $[7] !== strategy || $[8] !== timeoutMs) {
281
+ t4 = async code => {
276
282
  if (loading) {
277
283
  return {
278
284
  ok: false,
279
285
  error
280
286
  };
281
287
  }
282
- if (!isLoaded || !signIn || !setActive) {
283
- setError(TOTP_MESSAGES.notReady);
284
- return {
285
- ok: false,
286
- error: TOTP_MESSAGES.notReady
287
- };
288
- }
289
288
  setLoading(true);
290
289
  setError("");
291
290
  const result = await submitSecondFactor({
292
291
  signIn,
293
- setActive,
292
+ setActive: signIn.setActive,
294
293
  code,
295
294
  strategy,
296
295
  timeoutMs
@@ -301,29 +300,20 @@ export function useSecondFactor(t0) {
301
300
  setLoading(false);
302
301
  return result;
303
302
  };
304
- $[2] = error;
305
- $[3] = isLoaded;
306
- $[4] = loading;
307
- $[5] = setActive;
303
+ $[4] = error;
304
+ $[5] = loading;
308
305
  $[6] = signIn;
309
306
  $[7] = strategy;
310
307
  $[8] = timeoutMs;
311
- $[9] = t2;
308
+ $[9] = t4;
312
309
  } else {
313
- t2 = $[9];
310
+ t4 = $[9];
314
311
  }
315
- const submit = t2;
316
- let t3;
317
- if ($[10] !== isLoaded || $[11] !== signIn || $[12] !== strategy || $[13] !== timeoutMs) {
318
- t3 = async (t4, factor) => {
319
- const prepareStrategy = t4 === undefined ? strategy : t4;
320
- if (!isLoaded || !signIn) {
321
- setError(TOTP_MESSAGES.notReady);
322
- return {
323
- ok: false,
324
- error: TOTP_MESSAGES.notReady
325
- };
326
- }
312
+ const submit = t4;
313
+ let t5;
314
+ if ($[10] !== signIn || $[11] !== strategy || $[12] !== timeoutMs) {
315
+ t5 = async (t6, factor) => {
316
+ const prepareStrategy = t6 === undefined ? strategy : t6;
327
317
  setLoading(true);
328
318
  setError("");
329
319
  const result_0 = await prepareSecondFactorIfNeeded({
@@ -338,41 +328,40 @@ export function useSecondFactor(t0) {
338
328
  setLoading(false);
339
329
  return result_0;
340
330
  };
341
- $[10] = isLoaded;
342
- $[11] = signIn;
343
- $[12] = strategy;
344
- $[13] = timeoutMs;
345
- $[14] = t3;
331
+ $[10] = signIn;
332
+ $[11] = strategy;
333
+ $[12] = timeoutMs;
334
+ $[13] = t5;
346
335
  } else {
347
- t3 = $[14];
336
+ t5 = $[13];
348
337
  }
349
- const prepare = t3;
350
- let t4;
351
- if ($[15] === Symbol.for("react.memo_cache_sentinel")) {
352
- t4 = () => setError("");
353
- $[15] = t4;
338
+ const prepare = t5;
339
+ let t6;
340
+ if ($[14] === Symbol.for("react.memo_cache_sentinel")) {
341
+ t6 = () => setError("");
342
+ $[14] = t6;
354
343
  } else {
355
- t4 = $[15];
344
+ t6 = $[14];
356
345
  }
357
- let t5;
358
- if ($[16] !== error || $[17] !== loading || $[18] !== prepare || $[19] !== strategy || $[20] !== submit) {
359
- t5 = {
346
+ let t7;
347
+ if ($[15] !== error || $[16] !== loading || $[17] !== prepare || $[18] !== strategy || $[19] !== submit) {
348
+ t7 = {
360
349
  submit,
361
350
  prepare,
362
351
  strategy,
363
352
  setStrategy: setChosen,
364
353
  loading,
365
354
  error,
366
- clearError: t4
355
+ clearError: t6
367
356
  };
368
- $[16] = error;
369
- $[17] = loading;
370
- $[18] = prepare;
371
- $[19] = strategy;
372
- $[20] = submit;
373
- $[21] = t5;
357
+ $[15] = error;
358
+ $[16] = loading;
359
+ $[17] = prepare;
360
+ $[18] = strategy;
361
+ $[19] = submit;
362
+ $[20] = t7;
374
363
  } else {
375
- t5 = $[21];
364
+ t7 = $[20];
376
365
  }
377
- return t5;
366
+ return t7;
378
367
  }
package/dist/index.js CHANGED
@@ -64,7 +64,7 @@ export { ConfirmDialog } from "./ConfirmDialog";
64
64
  export { BulkActionBar, BULK_BAR_LABEL_MIN_WIDTH } from "./BulkActionBar";
65
65
  export { DropdownMenu, DropdownMenuTrigger, DropdownMenuContent, DropdownMenuLabel, DropdownMenuItem, DropdownMenuRadioItem, DropdownMenuSeparator } from "./DropdownMenu";
66
66
  export { UpdateChecker, UpdatePrompt, V_STORE_UPDATES_URL } from "./UpdateChecker";
67
- export { fetchGitHubUpdate, isNewerVersion, newerGitHubRelease } from "./updateCheck";
67
+ export { fetchGitHubUpdate, fetchVStoreUpdate, isNewerVersion, newerGitHubRelease, V_STORE_SITE_URL } from "./updateCheck";
68
68
  export { Sheet, SheetTrigger, SheetClose, SheetContent, SheetHandle, SheetHeader, SheetTitle, SheetDescription, SheetBody, SheetFooter } from "./Sheet";
69
69
  export { Toaster } from "./Toast";
70
70
  export { toast } from "./toastStore";
@@ -77,3 +77,35 @@ export async function fetchGitHubUpdate(source, currentVersion) {
77
77
  if (!Array.isArray(body) || !body.every(isRelease)) return null;
78
78
  return newerGitHubRelease(body, currentVersion, source.tagPrefix);
79
79
  }
80
+
81
+ /**
82
+ * The HTTP host of V Store, the Project V Android app store. Its
83
+ * `GET /latest-version?package=<Android package>` answers
84
+ * `{ packageName, versionName }` for an app in its catalogue.
85
+ */
86
+ export const V_STORE_SITE_URL = "https://blessed-porcupine-303.eu-west-1.convex.site";
87
+ /**
88
+ * Asks V Store for the app's newest version and returns it when it is newer
89
+ * than `currentVersion`, else null. V Store knows the releases of private
90
+ * repositories too, so this works where `fetchGitHubUpdate` cannot. Null too
91
+ * when the app is not in the catalogue, the request fails, or the answer has
92
+ * another shape. A network error rejects. Without `downloadUrl`, `url` is
93
+ * `fallbackUrl`.
94
+ */
95
+ export async function fetchVStoreUpdate(source, currentVersion, fallbackUrl) {
96
+ const site = source.siteUrl ?? V_STORE_SITE_URL;
97
+ const response = await fetch(`${site}/latest-version?package=${encodeURIComponent(source.packageName)}`, {
98
+ headers: {
99
+ Accept: "application/json"
100
+ }
101
+ });
102
+ if (!response.ok) return null;
103
+ const body = await response.json();
104
+ if (!body || typeof body !== "object") return null;
105
+ const version = body.versionName;
106
+ if (typeof version !== "string" || !isNewerVersion(version, currentVersion)) return null;
107
+ return {
108
+ version,
109
+ url: source.downloadUrl?.(version) ?? fallbackUrl
110
+ };
111
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/native-ui",
3
- "version": "0.32.0",
3
+ "version": "0.34.0",
4
4
  "license": "MIT",
5
5
  "description": "Shared React Native / Expo UI primitives. Floating surfaces, forms, overlays, toasts, settings chrome, tab bar, AI chat parts, the family app marks, a NativeWind preset, the Expo web root and Clerk + Convex auth screens.",
6
6
  "type": "module",
@@ -190,7 +190,7 @@
190
190
  "sideEffects": false,
191
191
  "peerDependencies": {
192
192
  "expo-paste-input": ">=0.2.0",
193
- "@clerk/expo": ">=4.0.0",
193
+ "@clerk/expo": ">=4.16.0",
194
194
  "@expo/vector-icons": "*",
195
195
  "@json-render/core": ">=0.21.0",
196
196
  "@json-render/react-native": ">=0.21.0",
@@ -10,7 +10,13 @@ import {
10
10
  AlertDialogHeader,
11
11
  AlertDialogTitle,
12
12
  } from "./AlertDialog";
13
- import { fetchGitHubUpdate, type AvailableUpdate, type GitHubReleaseSource } from "./updateCheck";
13
+ import {
14
+ fetchGitHubUpdate,
15
+ fetchVStoreUpdate,
16
+ type AvailableUpdate,
17
+ type GitHubReleaseSource,
18
+ type VStoreSource,
19
+ } from "./updateCheck";
14
20
 
15
21
  /** Opens the Updates tab of V Store, the Project V Android app store. */
16
22
  export const V_STORE_UPDATES_URL = "vstore://updates";
@@ -194,9 +200,15 @@ export type UpdateCheckerProps = Omit<
194
200
  * a local or preview build that has no release to compare with.
195
201
  */
196
202
  currentVersion: string | null | undefined;
197
- /** GitHub releases to read: the repository and the tag prefix before the version. */
203
+ /** GitHub releases to read: the repository and the tag prefix before the version. Public repositories only. */
198
204
  source?: GitHubReleaseSource;
199
- /** Your own check, used instead of `source`. Returns the update to offer, or null. */
205
+ /**
206
+ * Ask V Store for the newest version, used instead of `source`. It works
207
+ * for a private repository too. Without `vStore.downloadUrl`, Download is
208
+ * hidden unless `showDownload` says otherwise.
209
+ */
210
+ vStore?: VStoreSource;
211
+ /** Your own check, used instead of `source` and `vStore`. Returns the update to offer, or null. */
200
212
  check?: (currentVersion: string) => Promise<AvailableUpdate | null>;
201
213
  /** `false` skips the check. Default `true`. */
202
214
  enabled?: boolean;
@@ -216,7 +228,8 @@ export type UpdateCheckerProps = Omit<
216
228
  * Looks for a newer version once when it mounts and offers it in an
217
229
  * `UpdatePrompt`. Mount it once, near the app root.
218
230
  *
219
- * - The check reads the GitHub releases of `source`, or calls `check`.
231
+ * - The check reads the GitHub releases of `source`, asks V Store (`vStore`),
232
+ * or calls `check`.
220
233
  * Offline, rate limited, a failed check or no `currentVersion`: nothing is
221
234
  * offered, and nothing is shown.
222
235
  * - Later (and Android back) hides that version until a newer one is out,
@@ -227,6 +240,7 @@ export type UpdateCheckerProps = Omit<
227
240
  export function UpdateChecker({
228
241
  currentVersion,
229
242
  source,
243
+ vStore,
230
244
  check,
231
245
  enabled = true,
232
246
  storage,
@@ -238,22 +252,28 @@ export function UpdateChecker({
238
252
  }: UpdateCheckerProps) {
239
253
  const [update, setUpdate] = useState<AvailableUpdate | null>(null);
240
254
  // The latest callbacks, so a new function each render does not check again.
241
- const latest = useRef({ check, storage, storageKey });
255
+ const latest = useRef({ check, vStore, storage, storageKey });
242
256
  useEffect(() => {
243
- latest.current = { check, storage, storageKey };
257
+ latest.current = { check, vStore, storage, storageKey };
244
258
  });
245
259
 
246
260
  const repo = source?.repo;
247
261
  const tagPrefix = source?.tagPrefix;
248
262
  const hasCheck = check !== undefined;
263
+ const vStorePackage = vStore?.packageName;
264
+ const vStoreSite = vStore?.siteUrl;
265
+ // Without a download link, the update's link is the one that opens V Store.
266
+ const vStoreUrl = prompt.vStoreUrl ?? V_STORE_UPDATES_URL;
249
267
  useEffect(() => {
250
268
  if (!enabled || !currentVersion) return;
251
269
  let cancelled = false;
252
270
  const run = async () => {
253
- const { check: custom, storage: store, storageKey: key } = latest.current;
271
+ const { check: custom, vStore: vStoreSource, storage: store, storageKey: key } = latest.current;
254
272
  const found = custom
255
273
  ? await custom(currentVersion)
256
- : repo !== undefined && tagPrefix !== undefined
274
+ : vStoreSource
275
+ ? await fetchVStoreUpdate(vStoreSource, currentVersion, vStoreUrl)
276
+ : repo !== undefined && tagPrefix !== undefined
257
277
  ? await fetchGitHubUpdate({ repo, tagPrefix }, currentVersion)
258
278
  : null;
259
279
  if (!found) return;
@@ -265,11 +285,12 @@ export function UpdateChecker({
265
285
  return () => {
266
286
  cancelled = true;
267
287
  };
268
- }, [enabled, currentVersion, repo, tagPrefix, hasCheck]);
288
+ }, [enabled, currentVersion, repo, tagPrefix, hasCheck, vStorePackage, vStoreSite, vStoreUrl]);
269
289
 
270
290
  return (
271
291
  <UpdatePrompt
272
292
  {...prompt}
293
+ showDownload={prompt.showDownload ?? !(vStore !== undefined && vStore.downloadUrl === undefined)}
273
294
  update={update}
274
295
  onLater={(current) => {
275
296
  setUpdate(null);
@@ -1,6 +1,6 @@
1
1
  import { useRef, useState, type ReactNode } from "react";
2
2
  import { Text, View, type TextInput } from "react-native";
3
- import { useSignIn, useSignUp } from "@clerk/expo/legacy";
3
+ import { useSignIn, useSignUp } from "@clerk/expo";
4
4
  import { useRouter, type Href } from "expo-router";
5
5
  import { Button } from "../Button";
6
6
  import { textStyle } from "../controlStyles";
@@ -16,6 +16,7 @@ import {
16
16
  logAuthFailure,
17
17
  withAuthTimeout,
18
18
  } from "./authErrors";
19
+ import { clerkCall, signInCalls } from "./clerkCore3";
19
20
  import { SecondFactorScreen } from "./SecondFactorScreen";
20
21
  import {
21
22
  pickFirstFactorStrategy,
@@ -34,9 +35,6 @@ import { GoogleSignInButton, type AuthNavigationOptions } from "./GoogleSignInBu
34
35
 
35
36
  export type SignInMode = "sign-in" | "sign-up" | "reset-password";
36
37
 
37
- /** The first factor that emails a password-reset code. */
38
- const RESET_STRATEGY = "reset_password_email_code";
39
-
40
38
  /** The messages the screen can show. Generic: none names a provider. */
41
39
  export const AUTH_MESSAGES = {
42
40
  missingFields: "Enter your email and password",
@@ -210,8 +208,9 @@ export function SignInScreen({
210
208
  onIncomplete,
211
209
  stageTimeoutMs = DEFAULT_STAGE_TIMEOUT_MS,
212
210
  }: SignInScreenProps) {
213
- const signInState = useSignIn();
214
- const signUpState = useSignUp();
211
+ // Both are always defined: before Clerk loads, a call waits for it.
212
+ const signIn = signInCalls(useSignIn().signIn);
213
+ const signUpAttempt = useSignUp().signUp;
215
214
  const router = useRouter();
216
215
  const colorScheme = useScheme();
217
216
  const [email, setEmail] = useState("");
@@ -290,8 +289,6 @@ export function SignInScreen({
290
289
  };
291
290
 
292
291
  const chooseFirstStrategy = async (strategy: FirstFactorStrategy, list: FirstFactor[]) => {
293
- const { signIn } = signInState;
294
- if (!signIn) return;
295
292
  setFirstError("");
296
293
  setFirstStrategy(strategy);
297
294
  setPendingFirstFactor(true);
@@ -316,11 +313,10 @@ export function SignInScreen({
316
313
 
317
314
  // Routes a sign-in attempt. `needs_first_factor` is built in only after `signIn.create`.
318
315
  const routeAttempt = async (attempt: IncompleteSignIn, afterCreate: boolean) => {
319
- const { setActive } = signInState;
320
316
  // Clerk's `SignInStatus` omits `needs_client_trust`: widen before comparing.
321
317
  const status: string | null = attempt.status;
322
- if (status === "complete" && attempt.createdSessionId && setActive) {
323
- await withAuthTimeout(setActive({ session: attempt.createdSessionId }), "set-active", stageTimeoutMs);
318
+ if (status === "complete" && attempt.createdSessionId) {
319
+ await withAuthTimeout(signIn.setActive(), "set-active", stageTimeoutMs);
324
320
  done();
325
321
  } else if (status === "needs_new_password") {
326
322
  setPendingFirstFactor(false);
@@ -338,8 +334,7 @@ export function SignInScreen({
338
334
  };
339
335
 
340
336
  const onFirstFactor = async (firstCode: string) => {
341
- const { isLoaded, signIn } = signInState;
342
- if (!isLoaded || !signIn || firstLoading) return;
337
+ if (firstLoading) return;
343
338
  setFirstLoading(true);
344
339
  setFirstError("");
345
340
  const result = await submitFirstFactor({
@@ -364,8 +359,7 @@ export function SignInScreen({
364
359
  };
365
360
 
366
361
  const onNewPassword = async () => {
367
- const { isLoaded, signIn, setActive } = signInState;
368
- if (!isLoaded || !signIn || !setActive || submitting) return;
362
+ if (submitting) return;
369
363
  if (!newPassword) return fail(AUTH_MESSAGES.resetPasswordFailed, AUTH_MESSAGES.missingNewPassword);
370
364
  if (minPasswordLength && newPassword.length < minPasswordLength) {
371
365
  return fail(AUTH_MESSAGES.resetPasswordFailed, `Use at least ${minPasswordLength} characters for the password`);
@@ -376,15 +370,11 @@ export function SignInScreen({
376
370
  setSubmitting(true);
377
371
  setError("");
378
372
  try {
379
- const result = await withAuthTimeout(
380
- signIn.resetPassword({ password: newPassword }),
381
- "reset-password",
382
- stageTimeoutMs,
383
- );
373
+ const result = await withAuthTimeout(signIn.resetPassword(newPassword), "reset-password", stageTimeoutMs);
384
374
  // Clerk's `SignInStatus` omits `needs_client_trust`: widen before comparing.
385
375
  const afterReset: string | null = result.status;
386
376
  if (afterReset === "complete" && result.createdSessionId) {
387
- await withAuthTimeout(setActive({ session: result.createdSessionId }), "set-active", stageTimeoutMs);
377
+ await withAuthTimeout(signIn.setActive(), "set-active", stageTimeoutMs);
388
378
  done();
389
379
  } else if (afterReset === "needs_second_factor" || afterReset === "needs_client_trust") {
390
380
  // A reset on an account with two-step verification: the code step, then the session.
@@ -405,17 +395,12 @@ export function SignInScreen({
405
395
  };
406
396
 
407
397
  const onSignIn = async () => {
408
- const { isLoaded, signIn, setActive } = signInState;
409
- if (!isLoaded || !signIn || !setActive || submitting) return;
398
+ if (submitting) return;
410
399
  if (!email || !password) return fail(AUTH_MESSAGES.signInFailed, AUTH_MESSAGES.missingFields);
411
400
  setSubmitting(true);
412
401
  setError("");
413
402
  try {
414
- const attempt = await withAuthTimeout(
415
- signIn.create({ identifier: email, password }),
416
- "create",
417
- stageTimeoutMs,
418
- );
403
+ const attempt = await withAuthTimeout(signIn.password(email, password), "create", stageTimeoutMs);
419
404
  await routeAttempt(attempt, true);
420
405
  } catch (caught) {
421
406
  logAuthFailure("create", caught);
@@ -430,18 +415,11 @@ export function SignInScreen({
430
415
 
431
416
  // Emails the reset code. Throws what the call throws.
432
417
  const requestResetCode = async () => {
433
- const { signIn } = signInState;
434
- if (!signIn) return;
435
- await withAuthTimeout(
436
- signIn.create({ strategy: RESET_STRATEGY, identifier: email.trim() }),
437
- "create",
438
- stageTimeoutMs,
439
- );
418
+ await withAuthTimeout(signIn.sendResetCode(email.trim()), "create", stageTimeoutMs);
440
419
  };
441
420
 
442
421
  const onResetRequest = async () => {
443
- const { isLoaded, signIn } = signInState;
444
- if (!isLoaded || !signIn || submitting) return;
422
+ if (submitting) return;
445
423
  if (!email.trim()) return fail(AUTH_MESSAGES.sendResetFailed, AUTH_MESSAGES.missingEmail);
446
424
  setSubmitting(true);
447
425
  setError("");
@@ -458,8 +436,7 @@ export function SignInScreen({
458
436
  };
459
437
 
460
438
  const onResetResend = async () => {
461
- const { isLoaded, signIn } = signInState;
462
- if (!isLoaded || !signIn || resetLoading) return;
439
+ if (resetLoading) return;
463
440
  setResetLoading(true);
464
441
  setResetCodeError("");
465
442
  try {
@@ -474,16 +451,11 @@ export function SignInScreen({
474
451
 
475
452
  // Checks the emailed code. A good code gives `needs_new_password`, which `routeAttempt` shows.
476
453
  const onResetCode = async (resetCode: string) => {
477
- const { isLoaded, signIn } = signInState;
478
- if (!isLoaded || !signIn || resetLoading) return;
454
+ if (resetLoading) return;
479
455
  setResetLoading(true);
480
456
  setResetCodeError("");
481
457
  try {
482
- const attempt: IncompleteSignIn = await withAuthTimeout(
483
- signIn.attemptFirstFactor({ strategy: RESET_STRATEGY, code: resetCode }),
484
- "attempt-first-factor",
485
- stageTimeoutMs,
486
- );
458
+ const attempt = await withAuthTimeout(signIn.verifyResetCode(resetCode), "attempt-first-factor", stageTimeoutMs);
487
459
  const status: string | null = attempt.status;
488
460
  const known = ["complete", "needs_new_password", "needs_second_factor", "needs_client_trust"];
489
461
  if (status && known.includes(status)) {
@@ -523,8 +495,7 @@ export function SignInScreen({
523
495
  };
524
496
 
525
497
  const onSignUp = async () => {
526
- const { isLoaded, signUp: attempt } = signUpState;
527
- if (!isLoaded || !attempt || submitting) return;
498
+ if (submitting) return;
528
499
  if (!email || !password) return fail(AUTH_MESSAGES.signUpFailed, AUTH_MESSAGES.missingFields);
529
500
  if (minPasswordLength && password.length < minPasswordLength) {
530
501
  return fail(AUTH_MESSAGES.signUpFailed, `Use at least ${minPasswordLength} characters for the password`);
@@ -538,8 +509,8 @@ export function SignInScreen({
538
509
  setSubmitting(true);
539
510
  setError("");
540
511
  try {
541
- await attempt.create({ emailAddress: email, password });
542
- await attempt.prepareEmailAddressVerification({ strategy: "email_code" });
512
+ await clerkCall(signUpAttempt.password({ emailAddress: email, password }));
513
+ await clerkCall(signUpAttempt.verifications.sendEmailCode());
543
514
  setPendingVerification(true);
544
515
  } catch (caught) {
545
516
  fail(AUTH_MESSAGES.signUpFailed, authErrorMessage(caught, AUTH_MESSAGES.signUpFailed));
@@ -549,15 +520,14 @@ export function SignInScreen({
549
520
  };
550
521
 
551
522
  const onVerify = async () => {
552
- const { isLoaded, signUp: attempt, setActive } = signUpState;
553
- if (!isLoaded || !attempt || !setActive || submitting) return;
523
+ if (submitting) return;
554
524
  if (!code) return fail(AUTH_MESSAGES.verifyFailed, AUTH_MESSAGES.missingCode);
555
525
  setSubmitting(true);
556
526
  setError("");
557
527
  try {
558
- const result = await attempt.attemptEmailAddressVerification({ code });
559
- if (result.status === "complete" && result.createdSessionId) {
560
- await setActive({ session: result.createdSessionId });
528
+ await clerkCall(signUpAttempt.verifications.verifyEmailCode({ code }));
529
+ if (signUpAttempt.status === "complete" && signUpAttempt.createdSessionId) {
530
+ await clerkCall(signUpAttempt.finalize());
561
531
  done();
562
532
  } else {
563
533
  fail(AUTH_MESSAGES.verifyFailed, AUTH_MESSAGES.needsAnotherStep);
@@ -0,0 +1,88 @@
1
+ import type { useSignIn } from "@clerk/expo";
2
+ import type { IncompleteSignIn } from "./SignInScreen";
3
+ import type { PrepareFirstFactorParams, PrepareSecondFactorParams, SecondFactorStrategy } from "./totp";
4
+
5
+ /** The sign-in resource of the `useSignIn` hook at the root of `@clerk/expo` 4. */
6
+ type CoreSignIn = ReturnType<typeof useSignIn>["signIn"];
7
+
8
+ /**
9
+ * Awaits a call on Clerk's sign-in or sign-up resource. These calls resolve
10
+ * with `{ error }` where the older ones rejected: this throws that error, so
11
+ * `authErrorMessage` and `logAuthFailure` read it as before.
12
+ */
13
+ export async function clerkCall(call: Promise<{ error: unknown }>): Promise<void> {
14
+ const { error } = await call;
15
+ if (error) throw error;
16
+ }
17
+
18
+ /**
19
+ * Wraps the sign-in resource for the helpers in `totp.ts` and for
20
+ * `SignInScreen`. Each method makes the call, throws its error, and returns
21
+ * the sign-in as it stands after the call. The methods are `async`, so an
22
+ * error the resource throws at once is also a rejection.
23
+ */
24
+ export function signInCalls(signIn: CoreSignIn) {
25
+ const state = (): IncompleteSignIn => ({
26
+ status: signIn.status,
27
+ createdSessionId: signIn.createdSessionId,
28
+ supportedFirstFactors: signIn.supportedFirstFactors,
29
+ supportedSecondFactors: signIn.supportedSecondFactors,
30
+ });
31
+ return {
32
+ get supportedFirstFactors() {
33
+ return signIn.supportedFirstFactors;
34
+ },
35
+ get supportedSecondFactors() {
36
+ return signIn.supportedSecondFactors;
37
+ },
38
+ /** Starts a new sign-in with email and password. */
39
+ async password(identifier: string, password: string) {
40
+ await clerkCall(signIn.password({ identifier, password }));
41
+ return state();
42
+ },
43
+ /** Starts a new sign-in for `identifier` and emails its password-reset code. */
44
+ async sendResetCode(identifier: string) {
45
+ await clerkCall(signIn.create({ identifier }));
46
+ await clerkCall(signIn.resetPasswordEmailCode.sendCode());
47
+ },
48
+ async verifyResetCode(code: string) {
49
+ await clerkCall(signIn.resetPasswordEmailCode.verifyCode({ code }));
50
+ return state();
51
+ },
52
+ /** Sets the new password. Other sessions stay signed in, as before. */
53
+ async resetPassword(password: string) {
54
+ await clerkCall(signIn.resetPasswordEmailCode.submitPassword({ password, signOutOfOtherSessions: false }));
55
+ return state();
56
+ },
57
+ async prepareFirstFactor(params: PrepareFirstFactorParams) {
58
+ if (params.strategy === "phone_code") {
59
+ await clerkCall(signIn.phoneCode.sendCode({ phoneNumberId: params.phoneNumberId }));
60
+ } else {
61
+ await clerkCall(signIn.emailCode.sendCode({ emailAddressId: params.emailAddressId }));
62
+ }
63
+ },
64
+ async attemptFirstFactor(params: { strategy: "email_code" | "phone_code"; code: string }) {
65
+ const { code } = params;
66
+ if (params.strategy === "phone_code") await clerkCall(signIn.phoneCode.verifyCode({ code }));
67
+ else await clerkCall(signIn.emailCode.verifyCode({ code }));
68
+ return state();
69
+ },
70
+ /** Sends the second-factor code. Clerk picks the phone number or email of the sign-in itself. */
71
+ async prepareSecondFactor(params: PrepareSecondFactorParams) {
72
+ if (params.strategy === "phone_code") await clerkCall(signIn.mfa.sendPhoneCode());
73
+ else await clerkCall(signIn.mfa.sendEmailCode());
74
+ },
75
+ async attemptSecondFactor(params: { strategy: SecondFactorStrategy; code: string }) {
76
+ const { code } = params;
77
+ if (params.strategy === "totp") await clerkCall(signIn.mfa.verifyTOTP({ code }));
78
+ else if (params.strategy === "backup_code") await clerkCall(signIn.mfa.verifyBackupCode({ code }));
79
+ else if (params.strategy === "phone_code") await clerkCall(signIn.mfa.verifyPhoneCode({ code }));
80
+ else await clerkCall(signIn.mfa.verifyEmailCode({ code }));
81
+ return state();
82
+ },
83
+ /** Makes the session of a complete sign-in the active one. */
84
+ async setActive() {
85
+ await clerkCall(signIn.finalize());
86
+ },
87
+ };
88
+ }