@oxy.so/services 15.0.1 → 15.1.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.
Files changed (107) hide show
  1. package/android/src/main/java/so/oxy/security/OxyCallerPolicy.kt +0 -2
  2. package/lib/commonjs/ui/components/OxyProvider.js +8 -1
  3. package/lib/commonjs/ui/components/OxyProvider.js.map +1 -1
  4. package/lib/commonjs/ui/components/accountDialogHost.js +48 -0
  5. package/lib/commonjs/ui/components/accountDialogHost.js.map +1 -0
  6. package/lib/commonjs/ui/components/authChooser/primitives.js +5 -3
  7. package/lib/commonjs/ui/components/authChooser/primitives.js.map +1 -1
  8. package/lib/commonjs/ui/components/signIn/InlineCommonsQr.js +28 -25
  9. package/lib/commonjs/ui/components/signIn/InlineCommonsQr.js.map +1 -1
  10. package/lib/commonjs/ui/components/signIn/OxySignInPanel.js +63 -23
  11. package/lib/commonjs/ui/components/signIn/OxySignInPanel.js.map +1 -1
  12. package/lib/commonjs/ui/components/signIn/OxySignUpPanel.js +97 -27
  13. package/lib/commonjs/ui/components/signIn/OxySignUpPanel.js.map +1 -1
  14. package/lib/commonjs/ui/hooks/useUsernameAvailability.js +31 -11
  15. package/lib/commonjs/ui/hooks/useUsernameAvailability.js.map +1 -1
  16. package/lib/commonjs/ui/screens/CreateAccountScreen.js +6 -3
  17. package/lib/commonjs/ui/screens/CreateAccountScreen.js.map +1 -1
  18. package/lib/commonjs/ui/screens/OxyOneScreen.js +2 -2
  19. package/lib/commonjs/ui/screens/OxyOneScreen.js.map +1 -1
  20. package/lib/commonjs/ui/utils/handlePreview.js +20 -0
  21. package/lib/commonjs/ui/utils/handlePreview.js.map +1 -0
  22. package/lib/commonjs/ui/utils/usernameLookupBudget.js +95 -0
  23. package/lib/commonjs/ui/utils/usernameLookupBudget.js.map +1 -0
  24. package/lib/commonjs/ui/utils/usernamePolicy.js +7 -2
  25. package/lib/commonjs/ui/utils/usernamePolicy.js.map +1 -1
  26. package/lib/module/ui/components/OxyProvider.js +8 -1
  27. package/lib/module/ui/components/OxyProvider.js.map +1 -1
  28. package/lib/module/ui/components/accountDialogHost.js +42 -0
  29. package/lib/module/ui/components/accountDialogHost.js.map +1 -0
  30. package/lib/module/ui/components/authChooser/primitives.js +5 -3
  31. package/lib/module/ui/components/authChooser/primitives.js.map +1 -1
  32. package/lib/module/ui/components/signIn/InlineCommonsQr.js +28 -25
  33. package/lib/module/ui/components/signIn/InlineCommonsQr.js.map +1 -1
  34. package/lib/module/ui/components/signIn/OxySignInPanel.js +64 -24
  35. package/lib/module/ui/components/signIn/OxySignInPanel.js.map +1 -1
  36. package/lib/module/ui/components/signIn/OxySignUpPanel.js +98 -28
  37. package/lib/module/ui/components/signIn/OxySignUpPanel.js.map +1 -1
  38. package/lib/module/ui/hooks/useUsernameAvailability.js +32 -12
  39. package/lib/module/ui/hooks/useUsernameAvailability.js.map +1 -1
  40. package/lib/module/ui/screens/CreateAccountScreen.js +6 -3
  41. package/lib/module/ui/screens/CreateAccountScreen.js.map +1 -1
  42. package/lib/module/ui/screens/OxyOneScreen.js +2 -2
  43. package/lib/module/ui/screens/OxyOneScreen.js.map +1 -1
  44. package/lib/module/ui/utils/handlePreview.js +16 -0
  45. package/lib/module/ui/utils/handlePreview.js.map +1 -0
  46. package/lib/module/ui/utils/usernameLookupBudget.js +88 -0
  47. package/lib/module/ui/utils/usernameLookupBudget.js.map +1 -0
  48. package/lib/module/ui/utils/usernamePolicy.js +8 -3
  49. package/lib/module/ui/utils/usernamePolicy.js.map +1 -1
  50. package/lib/typescript/commonjs/ui/components/OxyProvider.d.ts.map +1 -1
  51. package/lib/typescript/commonjs/ui/components/accountDialogHost.d.ts +24 -0
  52. package/lib/typescript/commonjs/ui/components/accountDialogHost.d.ts.map +1 -0
  53. package/lib/typescript/commonjs/ui/components/authChooser/primitives.d.ts +5 -3
  54. package/lib/typescript/commonjs/ui/components/authChooser/primitives.d.ts.map +1 -1
  55. package/lib/typescript/commonjs/ui/components/signIn/InlineCommonsQr.d.ts +6 -0
  56. package/lib/typescript/commonjs/ui/components/signIn/InlineCommonsQr.d.ts.map +1 -1
  57. package/lib/typescript/commonjs/ui/components/signIn/OxySignInPanel.d.ts +4 -2
  58. package/lib/typescript/commonjs/ui/components/signIn/OxySignInPanel.d.ts.map +1 -1
  59. package/lib/typescript/commonjs/ui/components/signIn/OxySignUpPanel.d.ts +12 -2
  60. package/lib/typescript/commonjs/ui/components/signIn/OxySignUpPanel.d.ts.map +1 -1
  61. package/lib/typescript/commonjs/ui/hooks/useUsernameAvailability.d.ts +15 -1
  62. package/lib/typescript/commonjs/ui/hooks/useUsernameAvailability.d.ts.map +1 -1
  63. package/lib/typescript/commonjs/ui/screens/CreateAccountScreen.d.ts.map +1 -1
  64. package/lib/typescript/commonjs/ui/types/navigation.d.ts +14 -0
  65. package/lib/typescript/commonjs/ui/types/navigation.d.ts.map +1 -1
  66. package/lib/typescript/commonjs/ui/utils/handlePreview.d.ts +10 -0
  67. package/lib/typescript/commonjs/ui/utils/handlePreview.d.ts.map +1 -0
  68. package/lib/typescript/commonjs/ui/utils/usernameLookupBudget.d.ts +36 -0
  69. package/lib/typescript/commonjs/ui/utils/usernameLookupBudget.d.ts.map +1 -0
  70. package/lib/typescript/commonjs/ui/utils/usernamePolicy.d.ts +6 -1
  71. package/lib/typescript/commonjs/ui/utils/usernamePolicy.d.ts.map +1 -1
  72. package/lib/typescript/module/ui/components/OxyProvider.d.ts.map +1 -1
  73. package/lib/typescript/module/ui/components/accountDialogHost.d.ts +24 -0
  74. package/lib/typescript/module/ui/components/accountDialogHost.d.ts.map +1 -0
  75. package/lib/typescript/module/ui/components/authChooser/primitives.d.ts +5 -3
  76. package/lib/typescript/module/ui/components/authChooser/primitives.d.ts.map +1 -1
  77. package/lib/typescript/module/ui/components/signIn/InlineCommonsQr.d.ts +6 -0
  78. package/lib/typescript/module/ui/components/signIn/InlineCommonsQr.d.ts.map +1 -1
  79. package/lib/typescript/module/ui/components/signIn/OxySignInPanel.d.ts +4 -2
  80. package/lib/typescript/module/ui/components/signIn/OxySignInPanel.d.ts.map +1 -1
  81. package/lib/typescript/module/ui/components/signIn/OxySignUpPanel.d.ts +12 -2
  82. package/lib/typescript/module/ui/components/signIn/OxySignUpPanel.d.ts.map +1 -1
  83. package/lib/typescript/module/ui/hooks/useUsernameAvailability.d.ts +15 -1
  84. package/lib/typescript/module/ui/hooks/useUsernameAvailability.d.ts.map +1 -1
  85. package/lib/typescript/module/ui/screens/CreateAccountScreen.d.ts.map +1 -1
  86. package/lib/typescript/module/ui/types/navigation.d.ts +14 -0
  87. package/lib/typescript/module/ui/types/navigation.d.ts.map +1 -1
  88. package/lib/typescript/module/ui/utils/handlePreview.d.ts +10 -0
  89. package/lib/typescript/module/ui/utils/handlePreview.d.ts.map +1 -0
  90. package/lib/typescript/module/ui/utils/usernameLookupBudget.d.ts +36 -0
  91. package/lib/typescript/module/ui/utils/usernameLookupBudget.d.ts.map +1 -0
  92. package/lib/typescript/module/ui/utils/usernamePolicy.d.ts +6 -1
  93. package/lib/typescript/module/ui/utils/usernamePolicy.d.ts.map +1 -1
  94. package/package.json +3 -3
  95. package/src/ui/components/OxyProvider.tsx +11 -1
  96. package/src/ui/components/accountDialogHost.tsx +54 -0
  97. package/src/ui/components/authChooser/primitives.tsx +5 -3
  98. package/src/ui/components/signIn/InlineCommonsQr.tsx +24 -18
  99. package/src/ui/components/signIn/OxySignInPanel.tsx +64 -22
  100. package/src/ui/components/signIn/OxySignUpPanel.tsx +117 -27
  101. package/src/ui/hooks/useUsernameAvailability.ts +57 -14
  102. package/src/ui/screens/CreateAccountScreen.tsx +6 -3
  103. package/src/ui/screens/OxyOneScreen.tsx +2 -2
  104. package/src/ui/types/navigation.ts +14 -0
  105. package/src/ui/utils/handlePreview.ts +16 -0
  106. package/src/ui/utils/usernameLookupBudget.ts +91 -0
  107. package/src/ui/utils/usernamePolicy.ts +8 -3
@@ -10,16 +10,20 @@
10
10
  * The email is confirmed before the account exists, and it is how the account
11
11
  * signs in (a code or a link each time). A password and an authenticator app
12
12
  * are added later, in the account's security settings. Commons stays the way to
13
- * hold your own key: "Create it in Commons instead" — and linking Commons later
14
- * makes the account self-custodied and deletes the email.
13
+ * hold your own key: the username step says what that means, then "Create it in
14
+ * Commons instead" — and linking Commons later makes the account self-custodied
15
+ * and deletes the email. A host may preview the profile the username becomes
16
+ * (`handlePreviewTemplate`); this screen never names an app's domain itself.
15
17
  */
16
18
 
17
19
  import type React from 'react';
18
20
  import { useEffect, useState } from 'react';
19
- import { Linking, Platform, View } from 'react-native';
21
+ import { Linking, Platform, StyleSheet, View } from 'react-native';
20
22
  import { DatePicker } from '@oxy.so/bloom/date-picker';
21
23
  import { TextFieldLabel } from '@oxy.so/bloom/text-field';
24
+ import { useTheme } from '@oxy.so/bloom/theme';
22
25
  import { toast } from '@oxy.so/bloom/toast';
26
+ import { Text } from '@oxy.so/bloom/typography';
23
27
  import type { EmailVerificationConfirmResponse } from '@oxy.so/contracts';
24
28
  import {
25
29
  MIN_BIRTH_YEAR,
@@ -33,11 +37,18 @@ import {
33
37
  import { useOxy } from '../../context/oxyRuntimeContext';
34
38
  import { useAccountDialogSnapshot } from '../../hooks/accountDialogSnapshot';
35
39
  import { useI18n } from '../../hooks/useI18n';
40
+ import { useUsernameAvailability } from '../../hooks/useUsernameAvailability';
36
41
  import { getCommonsAcquisitionUrl } from '../../utils/commonsStoreLinks';
37
42
  import { isWebBrowser } from '../../utils/isWebBrowser';
38
43
  import { fromCalendarDate, toCalendarDate } from '../../utils/calendarDate';
44
+ import { renderHandlePreview } from '../../utils/handlePreview';
45
+ import {
46
+ shareUsernameLookup,
47
+ usernameLookupCooldownSeconds,
48
+ } from '../../utils/usernameLookupBudget';
39
49
  import { checkUsernameDraft, describeUsernameRefusal } from '../../utils/usernamePolicy';
40
50
  import { SubtleLink } from '../authChooser/primitives';
51
+ import { useAccountDialogHost } from '../accountDialogHost';
41
52
  import { OxyAuthScreen, OxyAuthScreenHeader, OxyAuthTerms } from './OxyAuthScreen';
42
53
  import { readSignInFlow, writeSignInFlow } from './signInFlowStore';
43
54
  import {
@@ -70,6 +81,14 @@ export interface OxySignUpPanelProps {
70
81
  * this screen (`signInFlowStore`). `page` — a page of its own.
71
82
  */
72
83
  host?: 'dialog' | 'page';
84
+ /**
85
+ * Preview of the public profile this username becomes. `{username}` is
86
+ * replaced with the typed handle. The host writes its own origin, for
87
+ * example `{base}/@{username}` with that base already filled in. Omitted,
88
+ * the screen uses the template from `OxyProvider`, or shows none. Wins over
89
+ * the provider when both are set.
90
+ */
91
+ handlePreviewTemplate?: string | null;
73
92
  }
74
93
 
75
94
  /** What the dialog keeps of this screen across a remount. */
@@ -86,8 +105,11 @@ export const OxySignUpPanel: React.FC<OxySignUpPanelProps> = ({
86
105
  onSignedIn,
87
106
  onSignIn,
88
107
  host = 'page',
108
+ handlePreviewTemplate,
89
109
  }) => {
110
+ const theme = useTheme();
90
111
  const { t } = useI18n();
112
+ const hostDialog = useAccountDialogHost();
91
113
  const {
92
114
  oxyServices,
93
115
  handleWebSession,
@@ -104,6 +126,22 @@ export const OxySignUpPanel: React.FC<OxySignUpPanelProps> = ({
104
126
  const [dateOfBirth, setDateOfBirth] = useState(saved?.dateOfBirth ?? '');
105
127
  const [pending, setPending] = useState(false);
106
128
  const [error, setError] = useState<string | null>(null);
129
+ // A new account is not the one already signed in on this device, so a matching
130
+ // handle is checked rather than treated as "unchanged".
131
+ const availability = useUsernameAvailability(username, step.name === 'username', {
132
+ matchCurrentUser: false,
133
+ // Preview lookups share the ten-a-minute quota with the check below.
134
+ reserveSubmit: true,
135
+ });
136
+ const previewTemplate = (handlePreviewTemplate ?? hostDialog.handlePreviewTemplate)?.trim() || '';
137
+ const handlePreview = renderHandlePreview(previewTemplate, username);
138
+ const liveTaken =
139
+ !error && availability.status === 'taken' && availability.candidate === username.trim();
140
+ const showAvailable =
141
+ !error &&
142
+ availability.status === 'available' &&
143
+ availability.candidate === username.trim() &&
144
+ username.trim().length > 0;
107
145
 
108
146
  useEffect(() => {
109
147
  writeSignInFlow(flowOwner, SIGN_UP_FLOW_KEY, {
@@ -124,7 +162,8 @@ export const OxySignUpPanel: React.FC<OxySignUpPanelProps> = ({
124
162
  setError(
125
163
  reason instanceof UsernameTakenError
126
164
  ? t('signup.username.taken')
127
- : describeApiError(reason, t),
165
+ : // A name the server refuses (reserved, say) is a rule to follow.
166
+ (describeUsernameRefusal(reason, username, t) ?? describeApiError(reason, t)),
128
167
  );
129
168
  })
130
169
  .finally(() => setPending(false));
@@ -141,9 +180,23 @@ export const OxySignUpPanel: React.FC<OxySignUpPanelProps> = ({
141
180
  }
142
181
  const handle = draft.username;
143
182
  setUsername(handle);
183
+ // The live check already answered for this exact handle.
184
+ if (availability.status === 'taken' && availability.candidate === handle) {
185
+ setError(t('signup.username.taken'));
186
+ return;
187
+ }
144
188
  run(async () => {
145
- const { available } = await oxyServices.auth.checkUsername(handle);
146
- if (!available) throw new UsernameTakenError();
189
+ // Reuses the preview's answer for this exact handle, or the one lookup
190
+ // the preview left unused. Past that, wait out the window — sending
191
+ // would be refused and the refusal would count too.
192
+ const outcome = await shareUsernameLookup(handle, 'submit', (name) =>
193
+ oxyServices.auth.checkUsername(name),
194
+ );
195
+ if (outcome === 'deferred') {
196
+ setError(t('signin.errors.rateLimited', { seconds: usernameLookupCooldownSeconds() }));
197
+ return;
198
+ }
199
+ if (!outcome.available) throw new UsernameTakenError();
147
200
  setStep({ name: 'birthdate' });
148
201
  });
149
202
  };
@@ -344,33 +397,57 @@ export const OxySignUpPanel: React.FC<OxySignUpPanelProps> = ({
344
397
  <OxyAuthScreen>
345
398
  <OxyAuthScreenHeader title={t('signup.title')} description={t('signup.subtitle')} />
346
399
  {progress(0)}
347
- <AccountFlowField
348
- label={t('signup.username.label')}
349
- value={username}
350
- onChange={(value) => {
351
- setUsername(stripDisallowedUsernameCharacters(value));
352
- if (error) setError(null);
353
- }}
354
- onSubmit={submitUsername}
355
- error={error}
356
- disabled={pending}
357
- placeholder={t('signup.username.placeholder')}
358
- hint={t('usernamePolicy.hint')}
359
- maxLength={USERNAME_MAX_LENGTH}
360
- autoComplete="username"
361
- testID="signup-username"
362
- />
400
+ <View style={styles.usernameBlock}>
401
+ <AccountFlowField
402
+ label={t('signup.username.label')}
403
+ value={username}
404
+ onChange={(value) => {
405
+ setUsername(stripDisallowedUsernameCharacters(value));
406
+ if (error) setError(null);
407
+ }}
408
+ onSubmit={submitUsername}
409
+ error={error ?? (liveTaken ? t('signup.username.taken') : null)}
410
+ disabled={pending}
411
+ placeholder={t('signup.username.placeholder')}
412
+ hint={t('usernamePolicy.hint')}
413
+ maxLength={USERNAME_MAX_LENGTH}
414
+ autoComplete="username"
415
+ testID="signup-username"
416
+ />
417
+ {showAvailable ? (
418
+ <Text
419
+ testID="signup-username-status"
420
+ accessibilityLiveRegion="polite"
421
+ style={[styles.status, { color: theme.colors.success }]}
422
+ >
423
+ {t('signup.username.available')}
424
+ </Text>
425
+ ) : null}
426
+ {handlePreview ? (
427
+ <Text
428
+ testID="signup-handle-preview"
429
+ style={[styles.status, { color: theme.colors.textSecondary }]}
430
+ >
431
+ {handlePreview}
432
+ </Text>
433
+ ) : null}
434
+ </View>
363
435
  <AccountFlowAction
364
436
  label={t('signin.actions.continue')}
365
437
  onPress={submitUsername}
366
438
  pending={pending}
367
439
  testID="signup-username-continue"
368
440
  />
369
- <SubtleLink
370
- label={t('signup.createInCommons')}
371
- onPress={createInCommons}
372
- testID="signup-commons-instead"
373
- />
441
+ <View style={styles.commons}>
442
+ <AccountFlowNote testID="signup-commons-note">
443
+ {t('signup.commonsSubtitle')}
444
+ </AccountFlowNote>
445
+ <SubtleLink
446
+ label={t('signup.createInCommons')}
447
+ onPress={createInCommons}
448
+ testID="signup-commons-instead"
449
+ />
450
+ </View>
374
451
  <SubtleLink
375
452
  label={t('signup.backToSignInLink')}
376
453
  onPress={onSignIn}
@@ -384,3 +461,16 @@ export const OxySignUpPanel: React.FC<OxySignUpPanelProps> = ({
384
461
 
385
462
  /** The username was taken between typing it and creating the account. */
386
463
  class UsernameTakenError extends Error {}
464
+
465
+ const styles = StyleSheet.create({
466
+ usernameBlock: {
467
+ gap: 8,
468
+ },
469
+ status: {
470
+ fontSize: 16,
471
+ lineHeight: 22,
472
+ },
473
+ commons: {
474
+ gap: 8,
475
+ },
476
+ });
@@ -1,19 +1,45 @@
1
1
  import { useEffect, useState } from 'react';
2
2
  import { useOxy } from '../context/oxyRuntimeContext';
3
- import { checkUsernameDraft } from '../utils/usernamePolicy';
3
+ import { shareUsernameLookup } from '../utils/usernameLookupBudget';
4
+ import { checkUsernameDraft, describeUsernameRefusal } from '../utils/usernamePolicy';
4
5
  import { useI18n } from './useI18n';
5
6
 
6
- type CheckResult = { candidate: string; status: 'available' | 'taken' | 'error' };
7
+ type CheckResult =
8
+ | { candidate: string; status: 'available' | 'taken' | 'error' | 'deferred' }
9
+ // The request failed; whether the server refused the NAME (reserved, say) is
10
+ // decided at render, where the localized copy is available.
11
+ | { candidate: string; status: 'rejected'; error: unknown };
12
+
13
+ export interface UsernameAvailabilityOptions {
14
+ /**
15
+ * When true (the default), a handle that already belongs to the signed-in
16
+ * account is "unchanged" and is not sent to the server. Signup passes false:
17
+ * a new account needs its own free handle, even on a device that is signed in.
18
+ */
19
+ matchCurrentUser?: boolean;
20
+ /**
21
+ * Count preview lookups against the production ten-a-minute quota and stop
22
+ * one short of it. Signup sets this so Continue still has a lookup left.
23
+ * Profile editing leaves it off: saving there does not look the name up again.
24
+ */
25
+ reserveSubmit?: boolean;
26
+ }
7
27
 
8
28
  /** Debounced server check; a response only applies to the draft that requested it. */
9
- export function useUsernameAvailability(value: string, enabled: boolean) {
29
+ export function useUsernameAvailability(
30
+ value: string,
31
+ enabled: boolean,
32
+ options?: UsernameAvailabilityOptions,
33
+ ) {
10
34
  const { oxyServices, user } = useOxy();
11
35
  const { t } = useI18n();
36
+ const matchCurrentUser = options?.matchCurrentUser !== false;
37
+ const reserveSubmit = options?.reserveSubmit === true;
12
38
  const candidate = value.trim();
13
39
  // The policy of the signed-in account's kind: a bot's handle ends in `bot`.
14
40
  const checked = enabled ? checkUsernameDraft(candidate, t, user?.kind) : null;
15
41
  const invalid = checked && !checked.ok ? checked.error : undefined;
16
- const unchanged = candidate.toLowerCase() === user?.username?.toLowerCase();
42
+ const unchanged = matchCurrentUser && candidate.toLowerCase() === user?.username?.toLowerCase();
17
43
  const [result, setResult] = useState<CheckResult>();
18
44
  const [attempt, setAttempt] = useState(0);
19
45
 
@@ -23,12 +49,22 @@ export function useUsernameAvailability(value: string, enabled: boolean) {
23
49
  setResult(undefined);
24
50
  let active = true;
25
51
  const timer = setTimeout(() => {
26
- void oxyServices.auth.checkUsername(candidate).then(
27
- ({ available }) => {
28
- if (active) setResult({ candidate, status: available ? 'available' : 'taken' });
52
+ const lookup = reserveSubmit
53
+ ? shareUsernameLookup(candidate, 'background', (name) =>
54
+ oxyServices.auth.checkUsername(name),
55
+ )
56
+ : oxyServices.auth.checkUsername(candidate).then(({ available }) => ({ available }));
57
+ void lookup.then(
58
+ (outcome) => {
59
+ if (!active) return;
60
+ if (outcome === 'deferred') {
61
+ setResult({ candidate, status: 'deferred' });
62
+ return;
63
+ }
64
+ setResult({ candidate, status: outcome.available ? 'available' : 'taken' });
29
65
  },
30
- () => {
31
- if (active) setResult({ candidate, status: 'error' });
66
+ (error: unknown) => {
67
+ if (active) setResult({ candidate, status: 'rejected', error });
32
68
  },
33
69
  );
34
70
  }, 350);
@@ -36,21 +72,28 @@ export function useUsernameAvailability(value: string, enabled: boolean) {
36
72
  active = false;
37
73
  clearTimeout(timer);
38
74
  };
39
- }, [candidate, enabled, invalid, unchanged, oxyServices, attempt]);
75
+ }, [candidate, enabled, invalid, unchanged, oxyServices, attempt, reserveSubmit]);
40
76
 
77
+ // A failed request is either the server refusing the name itself (a rule the
78
+ // person can follow) or a failure of the check (which they can retry).
79
+ const current = result?.candidate === candidate ? result : undefined;
80
+ const refusal =
81
+ current?.status === 'rejected' ? describeUsernameRefusal(current.error, candidate, t) : null;
41
82
  const status = !enabled
42
83
  ? 'idle'
43
84
  : unchanged
44
85
  ? 'unchanged'
45
- : invalid
86
+ : invalid || refusal
46
87
  ? 'invalid'
47
- : result?.candidate === candidate
48
- ? result.status
88
+ : current
89
+ ? current.status === 'rejected'
90
+ ? 'error'
91
+ : current.status
49
92
  : 'checking';
50
93
  return {
51
94
  candidate,
52
95
  status,
53
- invalid,
96
+ invalid: invalid ?? refusal ?? undefined,
54
97
  canSave: status === 'available' || status === 'unchanged',
55
98
  retry: () => {
56
99
  setResult(undefined);
@@ -200,10 +200,13 @@ const CreateAccountScreen: React.FC<BaseScreenProps> = ({
200
200
  ? t('accounts.create.username.available')
201
201
  : t('accounts.create.username.taken'),
202
202
  );
203
- } catch {
203
+ } catch (error) {
204
204
  if (seq !== usernameCheckSeqRef.current) return;
205
- setUsernameStatus('idle');
206
- setUsernameMessage(t('accounts.create.username.checkFailed'));
205
+ // A name the server itself refuses (reserved) is a rule to follow,
206
+ // not a failed check: say which rule, as the format check does.
207
+ const reason = describeUsernameRefusal(error, value, t);
208
+ setUsernameStatus(reason === null ? 'idle' : 'invalid');
209
+ setUsernameMessage(reason ?? t('accounts.create.username.checkFailed'));
207
210
  }
208
211
  }, DEBOUNCE_MS);
209
212
  },
@@ -27,8 +27,8 @@ import { useSurfaceHeader } from '../hooks/useSurfaceHeader';
27
27
 
28
28
  const apps = {
29
29
  personal: ['alia', 'mention', 'inbox', 'noted', 'clarity', 'homiio'],
30
- creator: ['alia', 'mention', 'inbox', 'noted', 'clarity', 'schedio', 'mercaria'],
31
- business: ['alia', 'mention', 'inbox', 'noted', 'clarity', 'schedio'],
30
+ creator: ['alia', 'mention', 'inbox', 'noted', 'clarity', 'mercaria'],
31
+ business: ['alia', 'mention', 'inbox', 'noted', 'clarity'],
32
32
  } as const;
33
33
 
34
34
  /** The shared Bloom presentation, populated with Oxy's approved commercial terms.
@@ -86,6 +86,20 @@ export interface OxyProviderProps {
86
86
  * flow cannot start.
87
87
  */
88
88
  clientId?: string;
89
+ /**
90
+ * The app this provider's account dialog is signing into. When set, the
91
+ * sign-in screen says that one Oxy account works across this app and the
92
+ * others. Omitted, the line stays generic and names no app.
93
+ */
94
+ appName?: string;
95
+ /**
96
+ * Preview of the public profile a new username becomes, shown while signing
97
+ * up. `{username}` is replaced with the typed handle. Pass the host's own
98
+ * profile path, for example `{base}/@{username}` with the host's origin
99
+ * already written in place of `{base}` (`https://example.com/@{username}`).
100
+ * This SDK does not know or insert an app's domain.
101
+ */
102
+ handlePreviewTemplate?: string;
89
103
  baseURL?: string;
90
104
  authWebUrl?: string;
91
105
  authRedirectUri?: string;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The public profile a typed username would have, from a host template.
3
+ *
4
+ * `{username}` is the only token. The host writes its own origin into the
5
+ * template — `{base}/@{username}` means that host's base, not a domain this
6
+ * SDK knows. An empty handle, or a template with no `{username}`, previews
7
+ * nothing.
8
+ */
9
+ export function renderHandlePreview(
10
+ template: string | null | undefined,
11
+ username: string,
12
+ ): string | null {
13
+ const handle = username.trim();
14
+ if (!template || !handle || !template.includes('{username}')) return null;
15
+ return template.split('{username}').join(handle);
16
+ }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Shared budget for `GET /auth/check-username`.
3
+ *
4
+ * Outside development that route allows ten lookups a minute per IP
5
+ * (`checkLimiter` in `packages/api/src/routes/auth.ts`). Signup spends the
6
+ * same budget on a debounced preview while a name is typed and again when
7
+ * Continue confirms it. Preview stops one short of the limit, so typing
8
+ * cannot take the request Continue needs. A lookup already in flight or
9
+ * already answered for the same name is reused. Creating the account still
10
+ * checks uniqueness on its own.
11
+ */
12
+
13
+ /** Production ceiling of `checkLimiter`. Development's higher cap is ignored. */
14
+ export const USERNAME_LOOKUP_LIMIT = 10;
15
+
16
+ /** How long `checkLimiter` remembers a lookup. */
17
+ export const USERNAME_LOOKUP_WINDOW_MS = 60_000;
18
+
19
+ /**
20
+ * Preview lookups a single window may start. One slot stays for an explicit
21
+ * submit (`Continue`), which is the request typing must not spend.
22
+ */
23
+ export const USERNAME_LOOKUP_BACKGROUND_MAX = USERNAME_LOOKUP_LIMIT - 1;
24
+
25
+ export type UsernameLookupPurpose = 'background' | 'submit';
26
+
27
+ type Settled = { at: number; available: boolean };
28
+
29
+ const sentAt: number[] = [];
30
+ const settled = new Map<string, Settled>();
31
+ const inflight = new Map<string, Promise<{ available: boolean }>>();
32
+
33
+ /** Tests start from an empty window. */
34
+ export function resetUsernameLookupBudget(): void {
35
+ sentAt.length = 0;
36
+ settled.clear();
37
+ inflight.clear();
38
+ }
39
+
40
+ function prune(now: number): void {
41
+ const cutoff = now - USERNAME_LOOKUP_WINDOW_MS;
42
+ while (sentAt.length > 0 && sentAt[0] <= cutoff) sentAt.shift();
43
+ for (const [key, value] of settled) {
44
+ if (value.at <= cutoff) settled.delete(key);
45
+ }
46
+ }
47
+
48
+ function roomFor(purpose: UsernameLookupPurpose): number {
49
+ return purpose === 'background' ? USERNAME_LOOKUP_BACKGROUND_MAX : USERNAME_LOOKUP_LIMIT;
50
+ }
51
+
52
+ /** Seconds until a full window will accept another explicit lookup. `0` when it already can. */
53
+ export function usernameLookupCooldownSeconds(now = Date.now()): number {
54
+ prune(now);
55
+ if (sentAt.length < USERNAME_LOOKUP_LIMIT) return 0;
56
+ const oldest = sentAt[0];
57
+ if (oldest === undefined) return 0;
58
+ return Math.max(1, Math.ceil((oldest + USERNAME_LOOKUP_WINDOW_MS - now) / 1000));
59
+ }
60
+
61
+ /**
62
+ * One network lookup per name at a time, counted against the production quota.
63
+ * `'deferred'` means this call would have been one past the purpose's room, so
64
+ * the caller must not hit the API (a 429 counts against the same window).
65
+ */
66
+ export async function shareUsernameLookup(
67
+ username: string,
68
+ purpose: UsernameLookupPurpose,
69
+ check: (username: string) => Promise<{ available: boolean }>,
70
+ ): Promise<{ available: boolean } | 'deferred'> {
71
+ const key = username.trim().toLowerCase();
72
+ const now = Date.now();
73
+ prune(now);
74
+ const cached = settled.get(key);
75
+ if (cached) return { available: cached.available };
76
+ const pending = inflight.get(key);
77
+ if (pending) return pending;
78
+ if (sentAt.length >= roomFor(purpose)) return 'deferred';
79
+ // Record before the request starts so a second caller in this turn sees it.
80
+ sentAt.push(now);
81
+ const request = check(username).then((result) => {
82
+ settled.set(key, { at: Date.now(), available: result.available });
83
+ return result;
84
+ });
85
+ inflight.set(key, request);
86
+ try {
87
+ return await request;
88
+ } finally {
89
+ inflight.delete(key);
90
+ }
91
+ }
@@ -18,7 +18,7 @@ import {
18
18
  USERNAME_INVALID_MESSAGE,
19
19
  USERNAME_MAX_LENGTH,
20
20
  USERNAME_MIN_LENGTH,
21
- usernameCheckForAccountKind,
21
+ usernameFormatCheckForAccountKind,
22
22
  } from '@oxy.so/contracts/client';
23
23
  import type { Translate } from '../hooks/useI18n';
24
24
  import { errorCode } from './apiErrorMessage';
@@ -26,7 +26,12 @@ import { errorCode } from './apiErrorMessage';
26
26
  export type UsernameDraftResult = { ok: true; username: string } | { ok: false; error: string };
27
27
 
28
28
  /**
29
- * Hold a typed username to the policy for `kind` (`bot` handles end in `bot`).
29
+ * Hold a typed username to the SHAPE the policy requires for `kind` (`bot`
30
+ * handles end in `bot`).
31
+ *
32
+ * Reserved names are not judged here: the list belongs to the server, which
33
+ * answers for them (see {@link describeUsernameRefusal}), so a name it has
34
+ * released is never refused by a bundle that predates the release.
30
35
  * On success `username` is the trimmed handle to send; on failure `error` is
31
36
  * the localized reason, specific to the rule that failed.
32
37
  */
@@ -37,7 +42,7 @@ export function checkUsernameDraft(
37
42
  ): UsernameDraftResult {
38
43
  const username = value.trim();
39
44
  if (!username) return { ok: false, error: t('usernamePolicy.required') };
40
- const checked = usernameCheckForAccountKind(kind)(username, []);
45
+ const checked = usernameFormatCheckForAccountKind(kind)(username, []);
41
46
  if (checked.ok) return { ok: true, username: checked.value };
42
47
  return { ok: false, error: describeUsernameIssue(username, checked.issue.message, t) };
43
48
  }