@vvedantb/native-ui 0.32.0 → 0.33.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.
@@ -1,7 +1,7 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
2
  import { useRef, useState } from "react";
3
3
  import { Text, View } from "react-native";
4
- import { useSignIn, useSignUp } from "@clerk/expo/legacy";
4
+ import { useSignIn, useSignUp } from "@clerk/expo";
5
5
  import { useRouter } from "expo-router";
6
6
  import { Button } from "../Button";
7
7
  import { textStyle } from "../controlStyles";
@@ -11,12 +11,10 @@ import { toast } from "../toastStore";
11
11
  import { useScheme } from "../useScheme";
12
12
  import { AuthScreen, FieldError, OrDivider } from "./AuthScreen";
13
13
  import { authErrorMessage, DEFAULT_STAGE_TIMEOUT_MS, isAuthTimeoutError, logAuthFailure, withAuthTimeout } from "./authErrors";
14
+ import { clerkCall, signInCalls } from "./clerkCore3";
14
15
  import { SecondFactorScreen } from "./SecondFactorScreen";
15
16
  import { pickFirstFactorStrategy, pickSecondFactorStrategy, prepareFirstFactorIfNeeded, submitFirstFactor, supportedFirstFactorStrategies, supportedSecondFactorStrategies, useSecondFactor } from "./totp";
16
17
  import { GoogleSignInButton } from "./GoogleSignInButton";
17
- /** The first factor that emails a password-reset code. */
18
- const RESET_STRATEGY = "reset_password_email_code";
19
-
20
18
  /** The messages the screen can show. Generic: none names a provider. */
21
19
  export const AUTH_MESSAGES = {
22
20
  missingFields: "Enter your email and password",
@@ -137,8 +135,9 @@ export function SignInScreen({
137
135
  onIncomplete,
138
136
  stageTimeoutMs = DEFAULT_STAGE_TIMEOUT_MS
139
137
  }) {
140
- const signInState = useSignIn();
141
- const signUpState = useSignUp();
138
+ // Both are always defined: before Clerk loads, a call waits for it.
139
+ const signIn = signInCalls(useSignIn().signIn);
140
+ const signUpAttempt = useSignUp().signUp;
142
141
  const router = useRouter();
143
142
  const colorScheme = useScheme();
144
143
  const [email, setEmail] = useState("");
@@ -205,10 +204,6 @@ export function SignInScreen({
205
204
  await chooseStrategy(strategy_0, list_0);
206
205
  };
207
206
  const chooseFirstStrategy = async (strategy_1, list_1) => {
208
- const {
209
- signIn
210
- } = signInState;
211
- if (!signIn) return;
212
207
  setFirstError("");
213
208
  setFirstStrategy(strategy_1);
214
209
  setPendingFirstFactor(true);
@@ -232,15 +227,10 @@ export function SignInScreen({
232
227
 
233
228
  // Routes a sign-in attempt. `needs_first_factor` is built in only after `signIn.create`.
234
229
  const routeAttempt = async (attempt_3, afterCreate) => {
235
- const {
236
- setActive
237
- } = signInState;
238
230
  // Clerk's `SignInStatus` omits `needs_client_trust`: widen before comparing.
239
231
  const status = attempt_3.status;
240
- if (status === "complete" && attempt_3.createdSessionId && setActive) {
241
- await withAuthTimeout(setActive({
242
- session: attempt_3.createdSessionId
243
- }), "set-active", stageTimeoutMs);
232
+ if (status === "complete" && attempt_3.createdSessionId) {
233
+ await withAuthTimeout(signIn.setActive(), "set-active", stageTimeoutMs);
244
234
  done();
245
235
  } else if (status === "needs_new_password") {
246
236
  setPendingFirstFactor(false);
@@ -257,15 +247,11 @@ export function SignInScreen({
257
247
  }
258
248
  };
259
249
  const onFirstFactor = async firstCode => {
260
- const {
261
- isLoaded,
262
- signIn: signIn_0
263
- } = signInState;
264
- if (!isLoaded || !signIn_0 || firstLoading) return;
250
+ if (firstLoading) return;
265
251
  setFirstLoading(true);
266
252
  setFirstError("");
267
253
  const result = await submitFirstFactor({
268
- signIn: signIn_0,
254
+ signIn,
269
255
  code: firstCode,
270
256
  strategy: firstStrategy,
271
257
  timeoutMs: stageTimeoutMs
@@ -285,12 +271,7 @@ export function SignInScreen({
285
271
  }
286
272
  };
287
273
  const onNewPassword = async () => {
288
- const {
289
- isLoaded: isLoaded_0,
290
- signIn: signIn_1,
291
- setActive: setActive_0
292
- } = signInState;
293
- if (!isLoaded_0 || !signIn_1 || !setActive_0 || submitting) return;
274
+ if (submitting) return;
294
275
  if (!newPassword) return fail(AUTH_MESSAGES.resetPasswordFailed, AUTH_MESSAGES.missingNewPassword);
295
276
  if (minPasswordLength && newPassword.length < minPasswordLength) {
296
277
  return fail(AUTH_MESSAGES.resetPasswordFailed, `Use at least ${minPasswordLength} characters for the password`);
@@ -301,15 +282,11 @@ export function SignInScreen({
301
282
  setSubmitting(true);
302
283
  setError("");
303
284
  try {
304
- const result_0 = await withAuthTimeout(signIn_1.resetPassword({
305
- password: newPassword
306
- }), "reset-password", stageTimeoutMs);
285
+ const result_0 = await withAuthTimeout(signIn.resetPassword(newPassword), "reset-password", stageTimeoutMs);
307
286
  // Clerk's `SignInStatus` omits `needs_client_trust`: widen before comparing.
308
287
  const afterReset = result_0.status;
309
288
  if (afterReset === "complete" && result_0.createdSessionId) {
310
- await withAuthTimeout(setActive_0({
311
- session: result_0.createdSessionId
312
- }), "set-active", stageTimeoutMs);
289
+ await withAuthTimeout(signIn.setActive(), "set-active", stageTimeoutMs);
313
290
  done();
314
291
  } else if (afterReset === "needs_second_factor" || afterReset === "needs_client_trust") {
315
292
  // A reset on an account with two-step verification: the code step, then the session.
@@ -329,20 +306,12 @@ export function SignInScreen({
329
306
  }
330
307
  };
331
308
  const onSignIn = async () => {
332
- const {
333
- isLoaded: isLoaded_1,
334
- signIn: signIn_2,
335
- setActive: setActive_1
336
- } = signInState;
337
- if (!isLoaded_1 || !signIn_2 || !setActive_1 || submitting) return;
309
+ if (submitting) return;
338
310
  if (!email || !password) return fail(AUTH_MESSAGES.signInFailed, AUTH_MESSAGES.missingFields);
339
311
  setSubmitting(true);
340
312
  setError("");
341
313
  try {
342
- const attempt_4 = await withAuthTimeout(signIn_2.create({
343
- identifier: email,
344
- password
345
- }), "create", stageTimeoutMs);
314
+ const attempt_4 = await withAuthTimeout(signIn.password(email, password), "create", stageTimeoutMs);
346
315
  await routeAttempt(attempt_4, true);
347
316
  } catch (caught_1) {
348
317
  logAuthFailure("create", caught_1);
@@ -356,21 +325,10 @@ export function SignInScreen({
356
325
 
357
326
  // Emails the reset code. Throws what the call throws.
358
327
  const requestResetCode = async () => {
359
- const {
360
- signIn: signIn_3
361
- } = signInState;
362
- if (!signIn_3) return;
363
- await withAuthTimeout(signIn_3.create({
364
- strategy: RESET_STRATEGY,
365
- identifier: email.trim()
366
- }), "create", stageTimeoutMs);
328
+ await withAuthTimeout(signIn.sendResetCode(email.trim()), "create", stageTimeoutMs);
367
329
  };
368
330
  const onResetRequest = async () => {
369
- const {
370
- isLoaded: isLoaded_2,
371
- signIn: signIn_4
372
- } = signInState;
373
- if (!isLoaded_2 || !signIn_4 || submitting) return;
331
+ if (submitting) return;
374
332
  if (!email.trim()) return fail(AUTH_MESSAGES.sendResetFailed, AUTH_MESSAGES.missingEmail);
375
333
  setSubmitting(true);
376
334
  setError("");
@@ -386,11 +344,7 @@ export function SignInScreen({
386
344
  }
387
345
  };
388
346
  const onResetResend = async () => {
389
- const {
390
- isLoaded: isLoaded_3,
391
- signIn: signIn_5
392
- } = signInState;
393
- if (!isLoaded_3 || !signIn_5 || resetLoading) return;
347
+ if (resetLoading) return;
394
348
  setResetLoading(true);
395
349
  setResetCodeError("");
396
350
  try {
@@ -405,18 +359,11 @@ export function SignInScreen({
405
359
 
406
360
  // Checks the emailed code. A good code gives `needs_new_password`, which `routeAttempt` shows.
407
361
  const onResetCode = async resetCode => {
408
- const {
409
- isLoaded: isLoaded_4,
410
- signIn: signIn_6
411
- } = signInState;
412
- if (!isLoaded_4 || !signIn_6 || resetLoading) return;
362
+ if (resetLoading) return;
413
363
  setResetLoading(true);
414
364
  setResetCodeError("");
415
365
  try {
416
- const attempt_5 = await withAuthTimeout(signIn_6.attemptFirstFactor({
417
- strategy: RESET_STRATEGY,
418
- code: resetCode
419
- }), "attempt-first-factor", stageTimeoutMs);
366
+ const attempt_5 = await withAuthTimeout(signIn.verifyResetCode(resetCode), "attempt-first-factor", stageTimeoutMs);
420
367
  const status_0 = attempt_5.status;
421
368
  const known = ["complete", "needs_new_password", "needs_second_factor", "needs_client_trust"];
422
369
  if (status_0 && known.includes(status_0)) {
@@ -453,11 +400,7 @@ export function SignInScreen({
453
400
  secondFactor.clearError();
454
401
  };
455
402
  const onSignUp = async () => {
456
- const {
457
- isLoaded: isLoaded_5,
458
- signUp: attempt_6
459
- } = signUpState;
460
- if (!isLoaded_5 || !attempt_6 || submitting) return;
403
+ if (submitting) return;
461
404
  if (!email || !password) return fail(AUTH_MESSAGES.signUpFailed, AUTH_MESSAGES.missingFields);
462
405
  if (minPasswordLength && password.length < minPasswordLength) {
463
406
  return fail(AUTH_MESSAGES.signUpFailed, `Use at least ${minPasswordLength} characters for the password`);
@@ -471,13 +414,11 @@ export function SignInScreen({
471
414
  setSubmitting(true);
472
415
  setError("");
473
416
  try {
474
- await attempt_6.create({
417
+ await clerkCall(signUpAttempt.password({
475
418
  emailAddress: email,
476
419
  password
477
- });
478
- await attempt_6.prepareEmailAddressVerification({
479
- strategy: "email_code"
480
- });
420
+ }));
421
+ await clerkCall(signUpAttempt.verifications.sendEmailCode());
481
422
  setPendingVerification(true);
482
423
  } catch (caught_5) {
483
424
  fail(AUTH_MESSAGES.signUpFailed, authErrorMessage(caught_5, AUTH_MESSAGES.signUpFailed));
@@ -486,23 +427,16 @@ export function SignInScreen({
486
427
  }
487
428
  };
488
429
  const onVerify = async () => {
489
- const {
490
- isLoaded: isLoaded_6,
491
- signUp: attempt_7,
492
- setActive: setActive_2
493
- } = signUpState;
494
- if (!isLoaded_6 || !attempt_7 || !setActive_2 || submitting) return;
430
+ if (submitting) return;
495
431
  if (!code) return fail(AUTH_MESSAGES.verifyFailed, AUTH_MESSAGES.missingCode);
496
432
  setSubmitting(true);
497
433
  setError("");
498
434
  try {
499
- const result_2 = await attempt_7.attemptEmailAddressVerification({
435
+ await clerkCall(signUpAttempt.verifications.verifyEmailCode({
500
436
  code
501
- });
502
- if (result_2.status === "complete" && result_2.createdSessionId) {
503
- await setActive_2({
504
- session: result_2.createdSessionId
505
- });
437
+ }));
438
+ if (signUpAttempt.status === "complete" && signUpAttempt.createdSessionId) {
439
+ await clerkCall(signUpAttempt.finalize());
506
440
  done();
507
441
  } else {
508
442
  fail(AUTH_MESSAGES.verifyFailed, AUTH_MESSAGES.needsAnotherStep);
@@ -0,0 +1,110 @@
1
+ /** The sign-in resource of the `useSignIn` hook at the root of `@clerk/expo` 4. */
2
+
3
+ /**
4
+ * Awaits a call on Clerk's sign-in or sign-up resource. These calls resolve
5
+ * with `{ error }` where the older ones rejected: this throws that error, so
6
+ * `authErrorMessage` and `logAuthFailure` read it as before.
7
+ */
8
+ export async function clerkCall(call) {
9
+ const {
10
+ error
11
+ } = await call;
12
+ if (error) throw error;
13
+ }
14
+
15
+ /**
16
+ * Wraps the sign-in resource for the helpers in `totp.ts` and for
17
+ * `SignInScreen`. Each method makes the call, throws its error, and returns
18
+ * the sign-in as it stands after the call. The methods are `async`, so an
19
+ * error the resource throws at once is also a rejection.
20
+ */
21
+ export function signInCalls(signIn) {
22
+ const state = () => ({
23
+ status: signIn.status,
24
+ createdSessionId: signIn.createdSessionId,
25
+ supportedFirstFactors: signIn.supportedFirstFactors,
26
+ supportedSecondFactors: signIn.supportedSecondFactors
27
+ });
28
+ return {
29
+ get supportedFirstFactors() {
30
+ return signIn.supportedFirstFactors;
31
+ },
32
+ get supportedSecondFactors() {
33
+ return signIn.supportedSecondFactors;
34
+ },
35
+ /** Starts a new sign-in with email and password. */
36
+ async password(identifier, password) {
37
+ await clerkCall(signIn.password({
38
+ identifier,
39
+ password
40
+ }));
41
+ return state();
42
+ },
43
+ /** Starts a new sign-in for `identifier` and emails its password-reset code. */
44
+ async sendResetCode(identifier) {
45
+ await clerkCall(signIn.create({
46
+ identifier
47
+ }));
48
+ await clerkCall(signIn.resetPasswordEmailCode.sendCode());
49
+ },
50
+ async verifyResetCode(code) {
51
+ await clerkCall(signIn.resetPasswordEmailCode.verifyCode({
52
+ code
53
+ }));
54
+ return state();
55
+ },
56
+ /** Sets the new password. Other sessions stay signed in, as before. */
57
+ async resetPassword(password) {
58
+ await clerkCall(signIn.resetPasswordEmailCode.submitPassword({
59
+ password,
60
+ signOutOfOtherSessions: false
61
+ }));
62
+ return state();
63
+ },
64
+ async prepareFirstFactor(params) {
65
+ if (params.strategy === "phone_code") {
66
+ await clerkCall(signIn.phoneCode.sendCode({
67
+ phoneNumberId: params.phoneNumberId
68
+ }));
69
+ } else {
70
+ await clerkCall(signIn.emailCode.sendCode({
71
+ emailAddressId: params.emailAddressId
72
+ }));
73
+ }
74
+ },
75
+ async attemptFirstFactor(params) {
76
+ const {
77
+ code
78
+ } = params;
79
+ if (params.strategy === "phone_code") await clerkCall(signIn.phoneCode.verifyCode({
80
+ code
81
+ }));else await clerkCall(signIn.emailCode.verifyCode({
82
+ code
83
+ }));
84
+ return state();
85
+ },
86
+ /** Sends the second-factor code. Clerk picks the phone number or email of the sign-in itself. */
87
+ async prepareSecondFactor(params) {
88
+ if (params.strategy === "phone_code") await clerkCall(signIn.mfa.sendPhoneCode());else await clerkCall(signIn.mfa.sendEmailCode());
89
+ },
90
+ async attemptSecondFactor(params) {
91
+ const {
92
+ code
93
+ } = params;
94
+ if (params.strategy === "totp") await clerkCall(signIn.mfa.verifyTOTP({
95
+ code
96
+ }));else if (params.strategy === "backup_code") await clerkCall(signIn.mfa.verifyBackupCode({
97
+ code
98
+ }));else if (params.strategy === "phone_code") await clerkCall(signIn.mfa.verifyPhoneCode({
99
+ code
100
+ }));else await clerkCall(signIn.mfa.verifyEmailCode({
101
+ code
102
+ }));
103
+ return state();
104
+ },
105
+ /** Makes the session of a complete sign-in the active one. */
106
+ async setActive() {
107
+ await clerkCall(signIn.finalize());
108
+ }
109
+ };
110
+ }
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/native-ui",
3
- "version": "0.32.0",
3
+ "version": "0.33.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",
@@ -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
+ }
package/src/auth/totp.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { useState } from "react";
2
- import { useSignIn } from "@clerk/expo/legacy";
2
+ import { useSignIn } from "@clerk/expo";
3
3
  import {
4
4
  authErrorMessage,
5
5
  DEFAULT_STAGE_TIMEOUT_MS,
@@ -7,6 +7,7 @@ import {
7
7
  logAuthFailure,
8
8
  withAuthTimeout,
9
9
  } from "./authErrors";
10
+ import { signInCalls } from "./clerkCore3";
10
11
 
11
12
  /** Logs a timed-out stage. Other failures are not logged here. */
12
13
  const logTimeout = (stage: string, error: unknown) => {
@@ -329,7 +330,8 @@ export type UseSecondFactorResult = {
329
330
  * `{ ok: true }` the session is active: navigate in the caller.
330
331
  */
331
332
  export function useSecondFactor(options: UseSecondFactorOptions = {}): UseSecondFactorResult {
332
- const { isLoaded, signIn, setActive } = useSignIn();
333
+ // Always defined: before Clerk loads, a call waits for it, inside the stage timeout.
334
+ const signIn = signInCalls(useSignIn().signIn);
333
335
  const [chosen, setChosen] = useState<SecondFactorStrategy | null>(null);
334
336
  const strategy = chosen ?? options.strategy ?? "totp";
335
337
  const [loading, setLoading] = useState(false);
@@ -338,13 +340,9 @@ export function useSecondFactor(options: UseSecondFactorOptions = {}): UseSecond
338
340
 
339
341
  const submit = async (code: string): Promise<TotpResult> => {
340
342
  if (loading) return { ok: false, error };
341
- if (!isLoaded || !signIn || !setActive) {
342
- setError(TOTP_MESSAGES.notReady);
343
- return { ok: false, error: TOTP_MESSAGES.notReady };
344
- }
345
343
  setLoading(true);
346
344
  setError("");
347
- const result = await submitSecondFactor({ signIn, setActive, code, strategy, timeoutMs });
345
+ const result = await submitSecondFactor({ signIn, setActive: signIn.setActive, code, strategy, timeoutMs });
348
346
  if (!result.ok) setError(result.error);
349
347
  setLoading(false);
350
348
  return result;
@@ -354,10 +352,6 @@ export function useSecondFactor(options: UseSecondFactorOptions = {}): UseSecond
354
352
  prepareStrategy: SecondFactorStrategy = strategy,
355
353
  factor?: SecondFactor,
356
354
  ): Promise<TotpResult> => {
357
- if (!isLoaded || !signIn) {
358
- setError(TOTP_MESSAGES.notReady);
359
- return { ok: false, error: TOTP_MESSAGES.notReady };
360
- }
361
355
  setLoading(true);
362
356
  setError("");
363
357
  const result = await prepareSecondFactorIfNeeded({ signIn, strategy: prepareStrategy, factor, timeoutMs });