@vvedantb/native-ui 0.30.0 → 0.32.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/README.md +44 -7
- package/dist/UpdateChecker.jsx +424 -0
- package/dist/auth/AccountScreens.jsx +450 -0
- package/dist/auth/AuthProviders.jsx +2 -2
- package/dist/auth/GoogleSignInButton.jsx +1 -1
- package/dist/auth/SignInScreen.jsx +1 -1
- package/dist/auth/authErrors.js +1 -1
- package/dist/auth/index.js +2 -1
- package/dist/auth/totp.js +1 -1
- package/dist/entries/auth.js +1 -1
- package/dist/entries/primitives.js +2 -0
- package/dist/index.js +2 -0
- package/dist/updateCheck.js +79 -0
- package/package.json +4 -4
- package/src/UpdateChecker.tsx +295 -0
- package/src/auth/AccountScreens.tsx +294 -0
- package/src/auth/AuthProviders.tsx +8 -5
- package/src/auth/GoogleSignInButton.tsx +1 -1
- package/src/auth/SignInScreen.tsx +1 -1
- package/src/auth/authErrors.ts +1 -1
- package/src/auth/index.ts +3 -1
- package/src/auth/totp.ts +1 -1
- package/src/entries/auth.ts +1 -1
- package/src/entries/primitives.ts +4 -0
- package/src/index.ts +4 -0
- package/src/updateCheck.ts +103 -0
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
import { useEffect, useRef, useState, type ReactNode } from "react";
|
|
2
|
+
import { Linking } from "react-native";
|
|
3
|
+
import {
|
|
4
|
+
AlertDialog,
|
|
5
|
+
AlertDialogAction,
|
|
6
|
+
AlertDialogCancel,
|
|
7
|
+
AlertDialogContent,
|
|
8
|
+
AlertDialogDescription,
|
|
9
|
+
AlertDialogFooter,
|
|
10
|
+
AlertDialogHeader,
|
|
11
|
+
AlertDialogTitle,
|
|
12
|
+
} from "./AlertDialog";
|
|
13
|
+
import { fetchGitHubUpdate, type AvailableUpdate, type GitHubReleaseSource } from "./updateCheck";
|
|
14
|
+
|
|
15
|
+
/** Opens the Updates tab of V Store, the Project V Android app store. */
|
|
16
|
+
export const V_STORE_UPDATES_URL = "vstore://updates";
|
|
17
|
+
|
|
18
|
+
type UpdateText = ReactNode | ((update: AvailableUpdate) => ReactNode);
|
|
19
|
+
|
|
20
|
+
export type UpdatePromptProps = {
|
|
21
|
+
/** The update to offer. The prompt is open while this is set. */
|
|
22
|
+
update: AvailableUpdate | null;
|
|
23
|
+
/** The app's display name, used in the default description. */
|
|
24
|
+
appName: string;
|
|
25
|
+
/** Default "Update available". */
|
|
26
|
+
title?: ReactNode;
|
|
27
|
+
/** Default "`<appName> <version>` is ready to install.". A function receives the update. */
|
|
28
|
+
description?: UpdateText;
|
|
29
|
+
/** Default "Later". */
|
|
30
|
+
laterLabel?: ReactNode;
|
|
31
|
+
/** Default "Download". */
|
|
32
|
+
downloadLabel?: ReactNode;
|
|
33
|
+
/** Default "Install from V Store". */
|
|
34
|
+
vStoreLabel?: ReactNode;
|
|
35
|
+
/** Shown once V Store could not be opened. Default "V Store is not installed on this device.". */
|
|
36
|
+
vStoreMissingText?: ReactNode;
|
|
37
|
+
/** Show Download, which opens `update.url` (the APK or its release page). Default `true`. */
|
|
38
|
+
showDownload?: boolean;
|
|
39
|
+
/** Show "Install from V Store", which only opens V Store. Default `true`. */
|
|
40
|
+
showVStore?: boolean;
|
|
41
|
+
/**
|
|
42
|
+
* The link that opens V Store. Default `V_STORE_UPDATES_URL`, its Updates
|
|
43
|
+
* tab. Pass the app's own page there when the app knows its link.
|
|
44
|
+
*/
|
|
45
|
+
vStoreUrl?: string;
|
|
46
|
+
/** Later, Android back and the VoiceOver escape. */
|
|
47
|
+
onLater?: (update: AvailableUpdate) => void;
|
|
48
|
+
/** After Download opened `update.url`. Close the prompt here. */
|
|
49
|
+
onDownload?: (update: AvailableUpdate) => void;
|
|
50
|
+
/** After V Store opened. Close the prompt here. */
|
|
51
|
+
onOpenVStore?: (update: AvailableUpdate) => void;
|
|
52
|
+
/** V Store could not be opened (not installed). The prompt stays open, without the V Store button. */
|
|
53
|
+
onVStoreMissing?: (update: AvailableUpdate) => void;
|
|
54
|
+
/** Opens a link. Default `Linking.openURL`, which rejects when no app handles it. */
|
|
55
|
+
openUrl?: (url: string) => Promise<unknown>;
|
|
56
|
+
/** The buttons get `<testID>-later`, `-download` and `-vstore`; the notice `-vstore-missing`. */
|
|
57
|
+
testID?: string;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* "Update available" with up to three actions: Later, Install from V Store
|
|
62
|
+
* and Download. Download opens the update's link in the browser, which
|
|
63
|
+
* downloads the APK for install. "Install from V Store" only opens V Store
|
|
64
|
+
* (`vStoreUrl`); it downloads and installs nothing itself.
|
|
65
|
+
*
|
|
66
|
+
* V Store not installed: the link fails to open, so the V Store button goes,
|
|
67
|
+
* a notice says so, and the prompt stays open with Download (when shown) and
|
|
68
|
+
* Later. Nothing is downloaded unless the user presses Download. The button is
|
|
69
|
+
* not hidden up front: Android 11+ only reports another app's link as
|
|
70
|
+
* openable to an app that declares it in its manifest `<queries>`.
|
|
71
|
+
*
|
|
72
|
+
* Controlled: it is open while `update` is set. `UpdateChecker` runs the
|
|
73
|
+
* check and holds that state. It adds no motion of its own; the dialog's
|
|
74
|
+
* follows Reduce Motion.
|
|
75
|
+
*/
|
|
76
|
+
export function UpdatePrompt({
|
|
77
|
+
update,
|
|
78
|
+
appName,
|
|
79
|
+
title = "Update available",
|
|
80
|
+
description,
|
|
81
|
+
laterLabel = "Later",
|
|
82
|
+
downloadLabel = "Download",
|
|
83
|
+
vStoreLabel = "Install from V Store",
|
|
84
|
+
vStoreMissingText = "V Store is not installed on this device.",
|
|
85
|
+
showDownload = true,
|
|
86
|
+
showVStore = true,
|
|
87
|
+
vStoreUrl = V_STORE_UPDATES_URL,
|
|
88
|
+
onLater,
|
|
89
|
+
onDownload,
|
|
90
|
+
onOpenVStore,
|
|
91
|
+
onVStoreMissing,
|
|
92
|
+
openUrl = Linking.openURL,
|
|
93
|
+
testID,
|
|
94
|
+
}: UpdatePromptProps) {
|
|
95
|
+
// The version V Store failed to open for, so the next version asks afresh.
|
|
96
|
+
const [missingFor, setMissingFor] = useState<string | null>(null);
|
|
97
|
+
// The last update, so the text stays while the dialog closes.
|
|
98
|
+
const [shown, setShown] = useState(update);
|
|
99
|
+
if (update && update !== shown) setShown(update);
|
|
100
|
+
|
|
101
|
+
const vStoreMissing = shown !== null && missingFor === shown.version;
|
|
102
|
+
const body =
|
|
103
|
+
shown === null
|
|
104
|
+
? null
|
|
105
|
+
: typeof description === "function"
|
|
106
|
+
? description(shown)
|
|
107
|
+
: (description ?? `${appName} ${shown.version} is ready to install.`);
|
|
108
|
+
|
|
109
|
+
return (
|
|
110
|
+
<AlertDialog
|
|
111
|
+
open={update !== null}
|
|
112
|
+
onOpenChange={(next) => {
|
|
113
|
+
if (!next && update) onLater?.(update);
|
|
114
|
+
}}
|
|
115
|
+
>
|
|
116
|
+
<AlertDialogContent testID={testID}>
|
|
117
|
+
<AlertDialogHeader>
|
|
118
|
+
<AlertDialogTitle>{title}</AlertDialogTitle>
|
|
119
|
+
{body != null ? <AlertDialogDescription>{body}</AlertDialogDescription> : null}
|
|
120
|
+
{showVStore && vStoreMissing ? (
|
|
121
|
+
<AlertDialogDescription
|
|
122
|
+
testID={testID ? `${testID}-vstore-missing` : undefined}
|
|
123
|
+
accessibilityLiveRegion="polite"
|
|
124
|
+
>
|
|
125
|
+
{vStoreMissingText}
|
|
126
|
+
</AlertDialogDescription>
|
|
127
|
+
) : null}
|
|
128
|
+
</AlertDialogHeader>
|
|
129
|
+
<AlertDialogFooter>
|
|
130
|
+
{/* Closes the dialog, and so calls `onLater`. */}
|
|
131
|
+
<AlertDialogCancel size="lg" testID={testID ? `${testID}-later` : undefined}>
|
|
132
|
+
{laterLabel}
|
|
133
|
+
</AlertDialogCancel>
|
|
134
|
+
{showVStore && !vStoreMissing ? (
|
|
135
|
+
<AlertDialogAction
|
|
136
|
+
size="lg"
|
|
137
|
+
variant={showDownload ? "secondary" : "default"}
|
|
138
|
+
testID={testID ? `${testID}-vstore` : undefined}
|
|
139
|
+
onPress={() => {
|
|
140
|
+
if (!update) return;
|
|
141
|
+
openUrl(vStoreUrl).then(
|
|
142
|
+
() => onOpenVStore?.(update),
|
|
143
|
+
() => {
|
|
144
|
+
setMissingFor(update.version);
|
|
145
|
+
onVStoreMissing?.(update);
|
|
146
|
+
},
|
|
147
|
+
);
|
|
148
|
+
}}
|
|
149
|
+
>
|
|
150
|
+
{vStoreLabel}
|
|
151
|
+
</AlertDialogAction>
|
|
152
|
+
) : null}
|
|
153
|
+
{showDownload ? (
|
|
154
|
+
<AlertDialogAction
|
|
155
|
+
size="lg"
|
|
156
|
+
testID={testID ? `${testID}-download` : undefined}
|
|
157
|
+
onPress={() => {
|
|
158
|
+
if (!update) return;
|
|
159
|
+
// A link that does not open leaves nothing to do here.
|
|
160
|
+
openUrl(update.url).catch(() => undefined);
|
|
161
|
+
onDownload?.(update);
|
|
162
|
+
}}
|
|
163
|
+
>
|
|
164
|
+
{downloadLabel}
|
|
165
|
+
</AlertDialogAction>
|
|
166
|
+
) : null}
|
|
167
|
+
</AlertDialogFooter>
|
|
168
|
+
</AlertDialogContent>
|
|
169
|
+
</AlertDialog>
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Where "Later" is remembered, for example `expo-secure-store` or AsyncStorage. */
|
|
174
|
+
export type UpdateDismissStorage = {
|
|
175
|
+
getItem: (key: string) => Promise<string | null> | string | null;
|
|
176
|
+
setItem: (key: string, value: string) => Promise<unknown> | unknown;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/** The version Later hid. An unreadable store means nothing was dismissed. */
|
|
180
|
+
async function readDismissed(storage: UpdateDismissStorage, key: string): Promise<string | null> {
|
|
181
|
+
try {
|
|
182
|
+
return await storage.getItem(key);
|
|
183
|
+
} catch {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
export type UpdateCheckerProps = Omit<
|
|
189
|
+
UpdatePromptProps,
|
|
190
|
+
"update" | "onLater" | "onDownload" | "onOpenVStore"
|
|
191
|
+
> & {
|
|
192
|
+
/**
|
|
193
|
+
* The installed version, "1.2.3". `null` or `undefined` skips the check, for
|
|
194
|
+
* a local or preview build that has no release to compare with.
|
|
195
|
+
*/
|
|
196
|
+
currentVersion: string | null | undefined;
|
|
197
|
+
/** GitHub releases to read: the repository and the tag prefix before the version. */
|
|
198
|
+
source?: GitHubReleaseSource;
|
|
199
|
+
/** Your own check, used instead of `source`. Returns the update to offer, or null. */
|
|
200
|
+
check?: (currentVersion: string) => Promise<AvailableUpdate | null>;
|
|
201
|
+
/** `false` skips the check. Default `true`. */
|
|
202
|
+
enabled?: boolean;
|
|
203
|
+
/**
|
|
204
|
+
* Remembers the version "Later" hid, so it is not offered again until a
|
|
205
|
+
* newer one is out. Without it, Later hides the prompt until the next start.
|
|
206
|
+
*/
|
|
207
|
+
storage?: UpdateDismissStorage;
|
|
208
|
+
/** Key in `storage`. Default `"updateDismissed"`. */
|
|
209
|
+
storageKey?: string;
|
|
210
|
+
onLater?: (update: AvailableUpdate) => void;
|
|
211
|
+
onDownload?: (update: AvailableUpdate) => void;
|
|
212
|
+
onOpenVStore?: (update: AvailableUpdate) => void;
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Looks for a newer version once when it mounts and offers it in an
|
|
217
|
+
* `UpdatePrompt`. Mount it once, near the app root.
|
|
218
|
+
*
|
|
219
|
+
* - The check reads the GitHub releases of `source`, or calls `check`.
|
|
220
|
+
* Offline, rate limited, a failed check or no `currentVersion`: nothing is
|
|
221
|
+
* offered, and nothing is shown.
|
|
222
|
+
* - Later (and Android back) hides that version until a newer one is out,
|
|
223
|
+
* when `storage` is given.
|
|
224
|
+
* - Download and "Install from V Store" close the prompt for this run only,
|
|
225
|
+
* so it asks again at the next start if the update was not installed.
|
|
226
|
+
*/
|
|
227
|
+
export function UpdateChecker({
|
|
228
|
+
currentVersion,
|
|
229
|
+
source,
|
|
230
|
+
check,
|
|
231
|
+
enabled = true,
|
|
232
|
+
storage,
|
|
233
|
+
storageKey = "updateDismissed",
|
|
234
|
+
onLater,
|
|
235
|
+
onDownload,
|
|
236
|
+
onOpenVStore,
|
|
237
|
+
...prompt
|
|
238
|
+
}: UpdateCheckerProps) {
|
|
239
|
+
const [update, setUpdate] = useState<AvailableUpdate | null>(null);
|
|
240
|
+
// The latest callbacks, so a new function each render does not check again.
|
|
241
|
+
const latest = useRef({ check, storage, storageKey });
|
|
242
|
+
useEffect(() => {
|
|
243
|
+
latest.current = { check, storage, storageKey };
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
const repo = source?.repo;
|
|
247
|
+
const tagPrefix = source?.tagPrefix;
|
|
248
|
+
const hasCheck = check !== undefined;
|
|
249
|
+
useEffect(() => {
|
|
250
|
+
if (!enabled || !currentVersion) return;
|
|
251
|
+
let cancelled = false;
|
|
252
|
+
const run = async () => {
|
|
253
|
+
const { check: custom, storage: store, storageKey: key } = latest.current;
|
|
254
|
+
const found = custom
|
|
255
|
+
? await custom(currentVersion)
|
|
256
|
+
: repo !== undefined && tagPrefix !== undefined
|
|
257
|
+
? await fetchGitHubUpdate({ repo, tagPrefix }, currentVersion)
|
|
258
|
+
: null;
|
|
259
|
+
if (!found) return;
|
|
260
|
+
const dismissed = store ? await readDismissed(store, key) : null;
|
|
261
|
+
if (!cancelled && dismissed !== found.version) setUpdate(found);
|
|
262
|
+
};
|
|
263
|
+
// A failed check must never get in the way of the app.
|
|
264
|
+
run().catch(() => undefined);
|
|
265
|
+
return () => {
|
|
266
|
+
cancelled = true;
|
|
267
|
+
};
|
|
268
|
+
}, [enabled, currentVersion, repo, tagPrefix, hasCheck]);
|
|
269
|
+
|
|
270
|
+
return (
|
|
271
|
+
<UpdatePrompt
|
|
272
|
+
{...prompt}
|
|
273
|
+
update={update}
|
|
274
|
+
onLater={(current) => {
|
|
275
|
+
setUpdate(null);
|
|
276
|
+
const { storage: store, storageKey: key } = latest.current;
|
|
277
|
+
if (store) {
|
|
278
|
+
// A failed write only means the prompt returns at the next start.
|
|
279
|
+
Promise.resolve()
|
|
280
|
+
.then(() => store.setItem(key, current.version))
|
|
281
|
+
.catch(() => undefined);
|
|
282
|
+
}
|
|
283
|
+
onLater?.(current);
|
|
284
|
+
}}
|
|
285
|
+
onDownload={(current) => {
|
|
286
|
+
setUpdate(null);
|
|
287
|
+
onDownload?.(current);
|
|
288
|
+
}}
|
|
289
|
+
onOpenVStore={(current) => {
|
|
290
|
+
setUpdate(null);
|
|
291
|
+
onOpenVStore?.(current);
|
|
292
|
+
}}
|
|
293
|
+
/>
|
|
294
|
+
);
|
|
295
|
+
}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { useRef, useState, type ReactNode } from "react";
|
|
2
|
+
import type { TextInput } from "react-native";
|
|
3
|
+
import { useUser } from "@clerk/expo";
|
|
4
|
+
import { useRouter, type Href } from "expo-router";
|
|
5
|
+
import { Button } from "../Button";
|
|
6
|
+
import { Input } from "../Input";
|
|
7
|
+
import { PasswordInput } from "../PasswordInput";
|
|
8
|
+
import { AuthScreen, FieldError } from "./AuthScreen";
|
|
9
|
+
import {
|
|
10
|
+
authErrorMessage,
|
|
11
|
+
DEFAULT_STAGE_TIMEOUT_MS,
|
|
12
|
+
logAuthFailure,
|
|
13
|
+
withAuthTimeout,
|
|
14
|
+
} from "./authErrors";
|
|
15
|
+
|
|
16
|
+
/** The messages the account screens can show. Generic: none names a provider. */
|
|
17
|
+
export const ACCOUNT_MESSAGES = {
|
|
18
|
+
changePasswordTitle: "Change password",
|
|
19
|
+
changePasswordSubtitle: "Enter your current password, then a new one.",
|
|
20
|
+
setPasswordTitle: "Set a password",
|
|
21
|
+
setPasswordSubtitle: "Your account has no password yet. Choose one.",
|
|
22
|
+
currentPasswordLabel: "Current password",
|
|
23
|
+
newPasswordLabel: "New password",
|
|
24
|
+
confirmPasswordLabel: "Confirm new password",
|
|
25
|
+
missingCurrentPassword: "Enter your current password",
|
|
26
|
+
missingNewPassword: "Enter a new password",
|
|
27
|
+
passwordMismatch: "The passwords do not match",
|
|
28
|
+
changePasswordFailed: "Could not change the password",
|
|
29
|
+
changePassword: "Change password",
|
|
30
|
+
setPassword: "Set password",
|
|
31
|
+
deleteTitle: "Delete account",
|
|
32
|
+
deleteSubtitle: "This deletes your account and its data. It cannot be undone.",
|
|
33
|
+
deleteConfirmMismatch: "Type the word exactly to confirm",
|
|
34
|
+
deleteFailed: "Could not delete the account",
|
|
35
|
+
deleteAccount: "Delete account",
|
|
36
|
+
cancel: "Cancel",
|
|
37
|
+
notReady: "Your account is not ready yet. Try again in a moment.",
|
|
38
|
+
} as const;
|
|
39
|
+
|
|
40
|
+
type AccountScreenBase = {
|
|
41
|
+
title?: string;
|
|
42
|
+
subtitle?: string;
|
|
43
|
+
/** Above the title. App brand chrome goes here. */
|
|
44
|
+
header?: ReactNode;
|
|
45
|
+
/** Below the form. */
|
|
46
|
+
footer?: ReactNode;
|
|
47
|
+
/** Shows a "Cancel" button that calls it. No button without it. */
|
|
48
|
+
onCancel?: () => void;
|
|
49
|
+
/** Time for the call, in milliseconds. A longer call fails with code `auth_timeout`. Default 20000. */
|
|
50
|
+
stageTimeoutMs?: number;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export type ChangePasswordScreenProps = AccountScreenBase & {
|
|
54
|
+
/** Rejects a shorter new password before sending it. Default: no check. */
|
|
55
|
+
minPasswordLength?: number;
|
|
56
|
+
/** Ends the user's other sessions after the change. Default `true`. */
|
|
57
|
+
signOutOfOtherSessions?: boolean;
|
|
58
|
+
/** Called after the password changed. Default: `router.back()`. */
|
|
59
|
+
onSuccess?: () => void;
|
|
60
|
+
/** Prefix for every `testID`. Default `"change-password"`. */
|
|
61
|
+
testIDPrefix?: string;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Changes the signed-in user's password with `user.updatePassword`: the
|
|
66
|
+
* current password, then the new one typed twice. An account with no password
|
|
67
|
+
* (Google only) gets no current-password field and the "Set a password" copy.
|
|
68
|
+
* Errors show under the fields. Render it under `ClerkProvider`, signed in.
|
|
69
|
+
*/
|
|
70
|
+
export function ChangePasswordScreen({
|
|
71
|
+
title,
|
|
72
|
+
subtitle,
|
|
73
|
+
header,
|
|
74
|
+
footer,
|
|
75
|
+
onCancel,
|
|
76
|
+
stageTimeoutMs = DEFAULT_STAGE_TIMEOUT_MS,
|
|
77
|
+
minPasswordLength,
|
|
78
|
+
signOutOfOtherSessions = true,
|
|
79
|
+
onSuccess,
|
|
80
|
+
testIDPrefix = "change-password",
|
|
81
|
+
}: ChangePasswordScreenProps) {
|
|
82
|
+
const { isLoaded, user } = useUser();
|
|
83
|
+
const router = useRouter();
|
|
84
|
+
const [current, setCurrent] = useState("");
|
|
85
|
+
const [next, setNext] = useState("");
|
|
86
|
+
const [confirm, setConfirm] = useState("");
|
|
87
|
+
const [error, setError] = useState("");
|
|
88
|
+
const [submitting, setSubmitting] = useState(false);
|
|
89
|
+
const nextField = useRef<TextInput>(null);
|
|
90
|
+
const confirmField = useRef<TextInput>(null);
|
|
91
|
+
const id = (name: string) => `${testIDPrefix}-${name}`;
|
|
92
|
+
// Until the user loads, show the current-password field: most accounts have one.
|
|
93
|
+
const hasPassword = user ? user.passwordEnabled : true;
|
|
94
|
+
|
|
95
|
+
const submit = async () => {
|
|
96
|
+
if (submitting) return;
|
|
97
|
+
if (!isLoaded || !user) return setError(ACCOUNT_MESSAGES.notReady);
|
|
98
|
+
if (hasPassword && !current) return setError(ACCOUNT_MESSAGES.missingCurrentPassword);
|
|
99
|
+
if (!next) return setError(ACCOUNT_MESSAGES.missingNewPassword);
|
|
100
|
+
if (minPasswordLength && next.length < minPasswordLength) {
|
|
101
|
+
return setError(`Use at least ${minPasswordLength} characters for the password`);
|
|
102
|
+
}
|
|
103
|
+
if (next !== confirm) return setError(ACCOUNT_MESSAGES.passwordMismatch);
|
|
104
|
+
setSubmitting(true);
|
|
105
|
+
setError("");
|
|
106
|
+
// React Compiler memoises a component only when its `try` has no branch and
|
|
107
|
+
// no `finally`, so the branches sit before and after it.
|
|
108
|
+
const params = hasPassword
|
|
109
|
+
? { newPassword: next, signOutOfOtherSessions, currentPassword: current }
|
|
110
|
+
: { newPassword: next, signOutOfOtherSessions };
|
|
111
|
+
let failed = false;
|
|
112
|
+
let failure: unknown;
|
|
113
|
+
try {
|
|
114
|
+
await withAuthTimeout(user.updatePassword(params), "update-password", stageTimeoutMs);
|
|
115
|
+
} catch (caught) {
|
|
116
|
+
failed = true;
|
|
117
|
+
failure = caught;
|
|
118
|
+
}
|
|
119
|
+
setSubmitting(false);
|
|
120
|
+
if (failed) {
|
|
121
|
+
logAuthFailure("update-password", failure);
|
|
122
|
+
return setError(authErrorMessage(failure, ACCOUNT_MESSAGES.changePasswordFailed));
|
|
123
|
+
}
|
|
124
|
+
setCurrent("");
|
|
125
|
+
setNext("");
|
|
126
|
+
setConfirm("");
|
|
127
|
+
if (onSuccess) onSuccess();
|
|
128
|
+
else router.back();
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
return (
|
|
132
|
+
<AuthScreen
|
|
133
|
+
testID={id("screen")}
|
|
134
|
+
title={title ?? (hasPassword ? ACCOUNT_MESSAGES.changePasswordTitle : ACCOUNT_MESSAGES.setPasswordTitle)}
|
|
135
|
+
subtitle={subtitle ?? (hasPassword ? ACCOUNT_MESSAGES.changePasswordSubtitle : ACCOUNT_MESSAGES.setPasswordSubtitle)}
|
|
136
|
+
header={header}
|
|
137
|
+
footer={footer}
|
|
138
|
+
>
|
|
139
|
+
{hasPassword ? (
|
|
140
|
+
<PasswordInput
|
|
141
|
+
testID={id("current")}
|
|
142
|
+
accessibilityLabel={ACCOUNT_MESSAGES.currentPasswordLabel}
|
|
143
|
+
autoCapitalize="none"
|
|
144
|
+
autoComplete="password"
|
|
145
|
+
textContentType="password"
|
|
146
|
+
returnKeyType="next"
|
|
147
|
+
submitBehavior="submit"
|
|
148
|
+
onSubmitEditing={() => nextField.current?.focus()}
|
|
149
|
+
value={current}
|
|
150
|
+
placeholder={ACCOUNT_MESSAGES.currentPasswordLabel}
|
|
151
|
+
onChangeText={setCurrent}
|
|
152
|
+
/>
|
|
153
|
+
) : null}
|
|
154
|
+
<PasswordInput
|
|
155
|
+
testID={id("new")}
|
|
156
|
+
ref={nextField}
|
|
157
|
+
accessibilityLabel={ACCOUNT_MESSAGES.newPasswordLabel}
|
|
158
|
+
autoCapitalize="none"
|
|
159
|
+
autoComplete="new-password"
|
|
160
|
+
textContentType="newPassword"
|
|
161
|
+
returnKeyType="next"
|
|
162
|
+
submitBehavior="submit"
|
|
163
|
+
onSubmitEditing={() => confirmField.current?.focus()}
|
|
164
|
+
value={next}
|
|
165
|
+
placeholder={ACCOUNT_MESSAGES.newPasswordLabel}
|
|
166
|
+
onChangeText={setNext}
|
|
167
|
+
/>
|
|
168
|
+
<PasswordInput
|
|
169
|
+
testID={id("confirm")}
|
|
170
|
+
ref={confirmField}
|
|
171
|
+
accessibilityLabel={ACCOUNT_MESSAGES.confirmPasswordLabel}
|
|
172
|
+
autoCapitalize="none"
|
|
173
|
+
autoComplete="new-password"
|
|
174
|
+
textContentType="newPassword"
|
|
175
|
+
returnKeyType="go"
|
|
176
|
+
value={confirm}
|
|
177
|
+
placeholder={ACCOUNT_MESSAGES.confirmPasswordLabel}
|
|
178
|
+
onChangeText={setConfirm}
|
|
179
|
+
onSubmitEditing={() => void submit()}
|
|
180
|
+
/>
|
|
181
|
+
<FieldError testID={id("error")} message={error} />
|
|
182
|
+
<Button testID={id("submit")} loading={submitting} onPress={() => void submit()}>
|
|
183
|
+
{hasPassword ? ACCOUNT_MESSAGES.changePassword : ACCOUNT_MESSAGES.setPassword}
|
|
184
|
+
</Button>
|
|
185
|
+
{onCancel ? (
|
|
186
|
+
<Button testID={id("cancel")} variant="ghost" disabled={submitting} onPress={onCancel}>
|
|
187
|
+
{ACCOUNT_MESSAGES.cancel}
|
|
188
|
+
</Button>
|
|
189
|
+
) : null}
|
|
190
|
+
</AuthScreen>
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export type DeleteAccountScreenProps = AccountScreenBase & {
|
|
195
|
+
/** The word the user types to confirm. Compared without case. Default `"DELETE"`. */
|
|
196
|
+
confirmWord?: string;
|
|
197
|
+
/** Called after the account is deleted, in place of `router.replace`. */
|
|
198
|
+
onDeleted?: () => void;
|
|
199
|
+
/** Where to go after the account is deleted. Default `"/"`. */
|
|
200
|
+
afterDeleteHref?: Href;
|
|
201
|
+
/** Prefix for every `testID`. Default `"delete-account"`. */
|
|
202
|
+
testIDPrefix?: string;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Deletes the signed-in user's account with `user.delete()`. The user types
|
|
207
|
+
* `confirmWord` first; the delete button stays off until it matches. Needs
|
|
208
|
+
* "Allow users to delete their accounts" turned on for the instance. Errors
|
|
209
|
+
* show under the field. Render it under `ClerkProvider`, signed in.
|
|
210
|
+
*/
|
|
211
|
+
export function DeleteAccountScreen({
|
|
212
|
+
title,
|
|
213
|
+
subtitle,
|
|
214
|
+
header,
|
|
215
|
+
footer,
|
|
216
|
+
onCancel,
|
|
217
|
+
stageTimeoutMs = DEFAULT_STAGE_TIMEOUT_MS,
|
|
218
|
+
confirmWord = "DELETE",
|
|
219
|
+
onDeleted,
|
|
220
|
+
afterDeleteHref = "/",
|
|
221
|
+
testIDPrefix = "delete-account",
|
|
222
|
+
}: DeleteAccountScreenProps) {
|
|
223
|
+
const { isLoaded, user } = useUser();
|
|
224
|
+
const router = useRouter();
|
|
225
|
+
const [typed, setTyped] = useState("");
|
|
226
|
+
const [error, setError] = useState("");
|
|
227
|
+
const [submitting, setSubmitting] = useState(false);
|
|
228
|
+
const id = (name: string) => `${testIDPrefix}-${name}`;
|
|
229
|
+
const matches = typed.trim().toLowerCase() === confirmWord.toLowerCase();
|
|
230
|
+
|
|
231
|
+
const submit = async () => {
|
|
232
|
+
if (submitting) return;
|
|
233
|
+
if (!matches) return setError(ACCOUNT_MESSAGES.deleteConfirmMismatch);
|
|
234
|
+
if (!isLoaded || !user) return setError(ACCOUNT_MESSAGES.notReady);
|
|
235
|
+
setSubmitting(true);
|
|
236
|
+
setError("");
|
|
237
|
+
// No branch inside the `try`: see `ChangePasswordScreen`.
|
|
238
|
+
let failed = false;
|
|
239
|
+
let failure: unknown;
|
|
240
|
+
try {
|
|
241
|
+
await withAuthTimeout(user.delete(), "delete-account", stageTimeoutMs);
|
|
242
|
+
} catch (caught) {
|
|
243
|
+
failed = true;
|
|
244
|
+
failure = caught;
|
|
245
|
+
}
|
|
246
|
+
setSubmitting(false);
|
|
247
|
+
if (failed) {
|
|
248
|
+
logAuthFailure("delete-account", failure);
|
|
249
|
+
return setError(authErrorMessage(failure, ACCOUNT_MESSAGES.deleteFailed));
|
|
250
|
+
}
|
|
251
|
+
if (onDeleted) onDeleted();
|
|
252
|
+
else router.replace(afterDeleteHref);
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const label = `Type ${confirmWord} to confirm`;
|
|
256
|
+
return (
|
|
257
|
+
<AuthScreen
|
|
258
|
+
testID={id("screen")}
|
|
259
|
+
title={title ?? ACCOUNT_MESSAGES.deleteTitle}
|
|
260
|
+
subtitle={subtitle ?? ACCOUNT_MESSAGES.deleteSubtitle}
|
|
261
|
+
header={header}
|
|
262
|
+
footer={footer}
|
|
263
|
+
>
|
|
264
|
+
<Input
|
|
265
|
+
testID={id("confirm")}
|
|
266
|
+
accessibilityLabel={label}
|
|
267
|
+
autoCapitalize="characters"
|
|
268
|
+
autoCorrect={false}
|
|
269
|
+
returnKeyType="done"
|
|
270
|
+
value={typed}
|
|
271
|
+
placeholder={label}
|
|
272
|
+
onChangeText={(text) => {
|
|
273
|
+
setTyped(text);
|
|
274
|
+
setError("");
|
|
275
|
+
}}
|
|
276
|
+
/>
|
|
277
|
+
<FieldError testID={id("error")} message={error} />
|
|
278
|
+
<Button
|
|
279
|
+
testID={id("submit")}
|
|
280
|
+
variant="destructive"
|
|
281
|
+
loading={submitting}
|
|
282
|
+
disabled={!matches}
|
|
283
|
+
onPress={() => void submit()}
|
|
284
|
+
>
|
|
285
|
+
{ACCOUNT_MESSAGES.deleteAccount}
|
|
286
|
+
</Button>
|
|
287
|
+
{onCancel ? (
|
|
288
|
+
<Button testID={id("cancel")} variant="ghost" disabled={submitting} onPress={onCancel}>
|
|
289
|
+
{ACCOUNT_MESSAGES.cancel}
|
|
290
|
+
</Button>
|
|
291
|
+
) : null}
|
|
292
|
+
</AuthScreen>
|
|
293
|
+
);
|
|
294
|
+
}
|
|
@@ -1,17 +1,20 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
|
-
import { ClerkLoaded, ClerkProvider, useAuth as useClerkAuth, type TokenCache } from "@clerk/
|
|
2
|
+
import { ClerkLoaded, ClerkProvider, useAuth as useClerkAuth, type TokenCache } from "@clerk/expo";
|
|
3
3
|
import { ConvexProvider, type ConvexReactClient } from "convex/react";
|
|
4
4
|
import { ConvexProviderWithClerk } from "convex/react-clerk";
|
|
5
5
|
|
|
6
6
|
export type AuthProvidersProps = {
|
|
7
7
|
children: ReactNode;
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* The Clerk publishable key. The app reads it from its own config; this component does not check it.
|
|
10
|
+
* `@clerk/expo` needs it: pass it unless `clerk` is `false`.
|
|
11
|
+
*/
|
|
9
12
|
publishableKey?: string;
|
|
10
|
-
/** `tokenCache` from `@clerk/
|
|
13
|
+
/** `tokenCache` from `@clerk/expo/token-cache`, so the session survives a restart. */
|
|
11
14
|
tokenCache?: TokenCache;
|
|
12
15
|
/** A Convex client. Without one, the children sit under Clerk only (vmem). */
|
|
13
16
|
convex?: ConvexReactClient | null;
|
|
14
|
-
/** `useAuth` from `@clerk/
|
|
17
|
+
/** `useAuth` from `@clerk/expo`, for `ConvexProviderWithClerk`. Default: the one this package imports. */
|
|
15
18
|
useAuth?: typeof useClerkAuth;
|
|
16
19
|
/** Wrap the children in `ClerkLoaded`, so they wait for the session. Default `true`. */
|
|
17
20
|
loaded?: boolean;
|
|
@@ -42,7 +45,7 @@ export function AuthProviders({
|
|
|
42
45
|
}
|
|
43
46
|
const inner = loaded ? <ClerkLoaded>{children}</ClerkLoaded> : children;
|
|
44
47
|
return (
|
|
45
|
-
<ClerkProvider publishableKey={publishableKey} tokenCache={tokenCache}>
|
|
48
|
+
<ClerkProvider publishableKey={publishableKey ?? ""} tokenCache={tokenCache}>
|
|
46
49
|
{convex ? (
|
|
47
50
|
<ConvexProviderWithClerk client={convex} useAuth={useAuth}>
|
|
48
51
|
{inner}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { useEffect, useState } from "react";
|
|
2
2
|
import { Platform } from "react-native";
|
|
3
|
-
import { useSSO } from "@clerk/
|
|
3
|
+
import { useSSO } from "@clerk/expo";
|
|
4
4
|
import * as AuthSession from "expo-auth-session";
|
|
5
5
|
import * as WebBrowser from "expo-web-browser";
|
|
6
6
|
import { useRouter, type Href } from "expo-router";
|
|
@@ -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/
|
|
3
|
+
import { useSignIn, useSignUp } from "@clerk/expo/legacy";
|
|
4
4
|
import { useRouter, type Href } from "expo-router";
|
|
5
5
|
import { Button } from "../Button";
|
|
6
6
|
import { textStyle } from "../controlStyles";
|
package/src/auth/authErrors.ts
CHANGED
package/src/auth/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// Clerk + Convex providers and the shared sign-in and sign-up screens. On
|
|
2
|
-
// `@vvedantb/native-ui/auth` only: they import `@clerk/
|
|
2
|
+
// `@vvedantb/native-ui/auth` only: they import `@clerk/expo`, `convex`,
|
|
3
3
|
// `expo-auth-session`, `expo-web-browser` and `expo-router`, and Metro does not
|
|
4
4
|
// tree-shake.
|
|
5
5
|
export { AuthProviders } from "./AuthProviders";
|
|
@@ -36,6 +36,8 @@ export type {
|
|
|
36
36
|
SignInScreenProps,
|
|
37
37
|
SignUpScreenProps,
|
|
38
38
|
} from "./SignInScreen";
|
|
39
|
+
export { ChangePasswordScreen, DeleteAccountScreen, ACCOUNT_MESSAGES } from "./AccountScreens";
|
|
40
|
+
export type { ChangePasswordScreenProps, DeleteAccountScreenProps } from "./AccountScreens";
|
|
39
41
|
export { SecondFactorScreen } from "./SecondFactorScreen";
|
|
40
42
|
export type { SecondFactorScreenProps } from "./SecondFactorScreen";
|
|
41
43
|
export {
|
package/src/auth/totp.ts
CHANGED
package/src/entries/auth.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// `@vvedantb/native-ui/auth`: `AuthProviders`, `SignInScreen`, `SignUpScreen`,
|
|
2
2
|
// `GoogleSignInButton` and the auth frame. Not on the root entry: it imports
|
|
3
|
-
// `@clerk/
|
|
3
|
+
// `@clerk/expo`, `convex`, `expo-auth-session`, `expo-web-browser` and
|
|
4
4
|
// `expo-router`, and Metro does not tree-shake.
|
|
5
5
|
export * from "../auth";
|