@wix/himalaya-cli 0.1988.0 → 0.1990.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.
@@ -6,11 +6,20 @@
6
6
  // through `_shared/himi-account`. When a session starts, this worker leaves the screen: in
7
7
  // place of itself to the screen the shopper asked for (`returnScreenId`, e.g. My orders),
8
8
  // otherwise back to Account.
9
+ //
10
+ // Failures follow the Wix branded app, as Branded Lite's sign-in sheet (himi-store-account) does:
11
+ // `@himalaya/auth` words each IAM answer, and it shows where the visitor fixes it — a wrong password
12
+ // under Password, an unknown email under Email, a member waiting for the owner on a "Pending
13
+ // approval" panel, a password Wix reset on Reset password. The buttons are busy while Wix answers.
14
+ import { authFailure, authMessage, identityErrors, type AuthOperation } from "@himalaya/auth";
9
15
  import { defineService, navigate } from "@himalaya/state";
10
16
  import { dispatch } from "@himalaya/dispatch";
11
- import { signedInEmail, strOf } from "../_shared/himi-account";
17
+ import { memberIdentity, strOf } from "../_shared/himi-account";
12
18
  import { stashCopy, copyParam, copyTemplate } from "../_shared/himi-copy.js";
13
19
 
20
+ /** `@himalaya/auth` copy keys, through the app's copy pack first. */
21
+ const translated = (key: string) => copyParam(key, authMessage(key));
22
+
14
23
  interface SignInRouteInput {
15
24
  mode?: string;
16
25
  intent?: string;
@@ -122,9 +131,27 @@ interface S {
122
131
  // is ignored, which would make the Next key go dead.
123
132
  lastNameFocus: string;
124
133
  emailFocus: string;
134
+ passwordFocus: string;
125
135
  message: string;
126
136
  /** What `message` is: a failure ("error"), the reset-link note ("success"), or nothing (""). */
127
137
  messageKind: "" | "error" | "success";
138
+ /** `message` when it is a failure, for the error line (danger color). */
139
+ errorMessage: string;
140
+ /** `message` when it is good news (reset link sent), for the notice line. */
141
+ noticeMessage: string;
142
+ /** Said under the field the visitor fixes: an unknown or taken email, a wrong or short password. */
143
+ emailError: string;
144
+ passwordError: string;
145
+ /** `passwordError` holds "wrong email or password", which an edit to the email clears too. */
146
+ credentialError: boolean;
147
+ /** A sign-in, sign-up or reset is in flight (and stays so while a signed-in screen leaves). */
148
+ authBusy: boolean;
149
+ /** The site approves its members and this one waits for the owner: the form gives way to a panel. */
150
+ pendingApproval: boolean;
151
+ hidePendingApproval: boolean;
152
+ hideForm: boolean;
153
+ pendingTitle: string;
154
+ pendingBody: string;
128
155
  isRegistering: boolean;
129
156
  notRegistering: boolean;
130
157
  isRecovering: boolean;
@@ -136,8 +163,17 @@ function derive(s: S): S {
136
163
  const isRegistering = !!s.isRegistering;
137
164
  const isRecovering = !!s.isRecovering;
138
165
  const headline = authHeadline(s, isRegistering, isRecovering);
166
+ const pending = !!s.pendingApproval;
139
167
  return {
140
168
  ...s,
169
+ errorMessage: s.message && s.messageKind !== "success" ? s.message : "",
170
+ noticeMessage: s.message && s.messageKind === "success" ? s.message : "",
171
+ authBusy: !!s.authBusy,
172
+ pendingApproval: pending,
173
+ hidePendingApproval: !pending,
174
+ hideForm: pending,
175
+ pendingTitle: pending ? translated("auth.gate.pendingApproval.title") : "",
176
+ pendingBody: pending ? translated("auth.feedback.pending_approval") : "",
141
177
  isRegistering,
142
178
  notRegistering: !isRegistering,
143
179
  isRecovering,
@@ -148,14 +184,20 @@ function derive(s: S): S {
148
184
  };
149
185
  }
150
186
 
187
+ /** No line and no field error: what a mode switch, an open and a new attempt start from. */
188
+ const NO_FEEDBACK = { message: "", messageKind: "", emailError: "", passwordError: "", credentialError: false } as const;
189
+
151
190
  function opened(s: S, arg?: unknown): S {
152
191
  stashCopy(arg);
153
- return derive({ ...s, ...routeFrom(arg), password: "", message: "", messageKind: "", ...NO_FOCUS_MODE_SWITCH });
192
+ attempt++;
193
+ // An attempt from an earlier visit may still be out: the buttons stay busy until it settles.
194
+ return derive({ ...s, ...routeFrom(arg), ...NO_FEEDBACK, password: "", authBusy: authInFlight, pendingApproval: false, ...NO_FOCUS_MODE_SWITCH });
154
195
  }
155
196
 
156
- /** A session has started: open the screen the shopper asked for, else go back to Account. */
197
+ /** A session has started: open the screen the shopper asked for, else go back to Account. The
198
+ * buttons are released: a host that does not apply the move must not leave a dead form. */
157
199
  function leave(s: S): S {
158
- const done = derive({ ...s, password: "", message: "", messageKind: "" });
200
+ const done = derive({ ...s, password: "", message: "", messageKind: "", authBusy: false });
159
201
  if (s.returnScreenId) {
160
202
  return navigate(done, {
161
203
  screenId: s.returnScreenId,
@@ -166,6 +208,74 @@ function leave(s: S): S {
166
208
  return navigate(done, { presentation: "back" });
167
209
  }
168
210
 
211
+ // One attempt at a time: a second tap while the first waits on the network does nothing.
212
+ let authInFlight = false;
213
+ // Bumped by every open and every edit. A failure that comes back after either is about a form the
214
+ // visitor no longer sees, so it is dropped rather than shown against the new values.
215
+ let attempt = 0;
216
+ const edited = (s: S): S => { attempt++; return s; };
217
+
218
+ /** The checks the branded app makes before it calls Wix (`@himalaya/auth`): an email, a valid one,
219
+ * a password, and 6 characters or more for a new account. Said under the field, which takes focus. */
220
+ function invalid(s: S, operation: AuthOperation): S | null {
221
+ const errors = identityErrors({ email: s.email, password: s.password }, operation);
222
+ if (!errors.email && !errors.password) return null;
223
+ return derive({ ...s, message: "", messageKind: "", authBusy: false,
224
+ emailError: errors.email ? translated(errors.email) : "",
225
+ passwordError: errors.password ? translated(errors.password) : "",
226
+ emailFocus: errors.email ? bumpToken(s.emailFocus) : s.emailFocus,
227
+ passwordFocus: !errors.email && errors.password ? bumpToken(s.passwordFocus) : s.passwordFocus });
228
+ }
229
+
230
+ /** The host answers a refusal as a result as well as a throw: both are a failure. */
231
+ function rejectResult(raw: unknown, operation: AuthOperation): void {
232
+ const value = raw as { status?: string; error?: unknown; payload?: { error?: unknown } } | null;
233
+ if (value?.status === "error" || value?.error || value?.payload?.error) throw authFailure(raw, operation, translated);
234
+ }
235
+
236
+ /** A failure goes where the visitor fixes it, as the Wix branded app routes it. */
237
+ function failed(s: S, failure: { code: string; reason: string }): S {
238
+ const base = { ...s, ...NO_FEEDBACK, authBusy: false };
239
+ if (failure.code === "invalid_credentials") {
240
+ // "Wrong email or password": an edit to either field clears it.
241
+ return derive({ ...base, passwordError: failure.reason, credentialError: true, passwordFocus: bumpToken(s.passwordFocus) });
242
+ }
243
+ if (failure.code === "email_in_use" || failure.code === "account_not_found") {
244
+ return derive({ ...base, emailError: failure.reason, emailFocus: bumpToken(s.emailFocus) });
245
+ }
246
+ if (failure.code === "pending_approval") return derive({ ...base, password: "", pendingApproval: true });
247
+ if (failure.code === "password_reset_required") {
248
+ return derive({ ...base, password: "", isRegistering: false, isRecovering: true, message: failure.reason, messageKind: "error", ...NO_FOCUS_MODE_SWITCH });
249
+ }
250
+ return derive({ ...base, message: failure.reason, messageKind: "error" });
251
+ }
252
+
253
+ async function authenticate(s: S, operation: "signIn" | "register"): Promise<S> {
254
+ if (authInFlight) return s;
255
+ const checked = invalid(s, operation);
256
+ if (checked) return checked;
257
+ authInFlight = true;
258
+ const mine = attempt;
259
+ try {
260
+ await svc.beginAuth(); // the busy buttons, before the network
261
+ const raw = await dispatch(operation === "signIn" ? "himi.wix.signInDirect" : "himi.wix.registerDirect", {
262
+ email: s.email.trim(),
263
+ password: s.password,
264
+ ...(operation === "register" ? { firstName: s.firstName, lastName: s.lastName } : {}),
265
+ });
266
+ rejectResult(raw, operation);
267
+ // Signed in is the host's word: a native host can answer it before it knows the email.
268
+ if (!(await memberIdentity()).signedIn) throw { code: "unknown" };
269
+ // Signed in whatever the visitor did meanwhile: the screen leaves.
270
+ return leave(svc.getState());
271
+ } catch (error) {
272
+ if (mine !== attempt) return derive({ ...svc.getState(), authBusy: false });
273
+ return failed(svc.getState(), authFailure(error, operation, translated));
274
+ } finally {
275
+ authInFlight = false;
276
+ }
277
+ }
278
+
169
279
  const svc = defineService<S, {
170
280
  load: (s: S, arg?: unknown) => S;
171
281
  setEmail: (s: S, arg?: unknown) => S;
@@ -177,6 +287,8 @@ const svc = defineService<S, {
177
287
  showRegister: (s: S) => S;
178
288
  showSignIn: (s: S) => S;
179
289
  showRecovery: (s: S) => S;
290
+ beginAuth: (s: S) => S;
291
+ closePendingApproval: (s: S) => S;
180
292
  resetPassword: (s: S) => Promise<S>;
181
293
  signIn: (s: S) => Promise<S>;
182
294
  register: (s: S) => Promise<S>;
@@ -194,7 +306,12 @@ const svc = defineService<S, {
194
306
  lastName: "",
195
307
  lastNameFocus: "",
196
308
  emailFocus: "",
309
+ passwordFocus: "",
197
310
  message: "", messageKind: "",
311
+ errorMessage: "", noticeMessage: "",
312
+ emailError: "", passwordError: "", credentialError: false,
313
+ authBusy: false,
314
+ pendingApproval: false, hidePendingApproval: true, hideForm: false, pendingTitle: "", pendingBody: "",
198
315
  isRegistering: false,
199
316
  notRegistering: true,
200
317
  isRecovering: false,
@@ -203,10 +320,13 @@ const svc = defineService<S, {
203
320
  }),
204
321
  actions: {
205
322
  load: (s, arg) => opened(s, arg),
206
- setEmail: (s, arg) => derive({ ...s, email: strOf(arg), message: "", messageKind: "" }),
207
- setPassword: (s, arg) => derive({ ...s, password: strOf(arg), message: "", messageKind: "" }),
208
- setFirstName: (s, arg) => derive({ ...s, firstName: strOf(arg), message: "", messageKind: "" }),
209
- setLastName: (s, arg) => derive({ ...s, lastName: strOf(arg), message: "", messageKind: "" }),
323
+ // An edit clears the line and the error under the field it fixes. A change event that repeats
324
+ // the value is not an edit: it must not drop the answer to an attempt still out.
325
+ setEmail: (s, arg) => strOf(arg) === s.email ? s : derive({ ...edited(s), email: strOf(arg), message: "", messageKind: "", emailError: "",
326
+ ...(s.credentialError ? { passwordError: "", credentialError: false } : {}) }),
327
+ setPassword: (s, arg) => strOf(arg) === s.password ? s : derive({ ...edited(s), password: strOf(arg), message: "", messageKind: "", passwordError: "", credentialError: false }),
328
+ setFirstName: (s, arg) => strOf(arg) === s.firstName ? s : derive({ ...edited(s), firstName: strOf(arg), message: "", messageKind: "" }),
329
+ setLastName: (s, arg) => strOf(arg) === s.lastName ? s : derive({ ...edited(s), lastName: strOf(arg), message: "", messageKind: "" }),
210
330
  // The register form's Next keys. Only first/last name carry one: they share a
211
331
  // single visibility gate (notRegistering) so their target is always on screen.
212
332
  // Email deliberately does NOT chain to password — see the comment on the
@@ -219,49 +339,41 @@ const svc = defineService<S, {
219
339
  // register and coming back would open focused on Last name, skipping First name.
220
340
  // Only the tokens whose fields actually unmount here are cleared: see
221
341
  // NO_FOCUS_MODE_SWITCH for why Email's is deliberately NOT among them.
222
- showRegister: (s) => derive({ ...s, isRegistering: true, isRecovering: false, message: "", messageKind: "", ...NO_FOCUS_MODE_SWITCH }),
223
- showSignIn: (s) => derive({ ...s, isRegistering: false, isRecovering: false, message: "", messageKind: "", ...NO_FOCUS_MODE_SWITCH }),
224
- showRecovery: (s) => derive({ ...s, isRegistering: false, isRecovering: true, password: "", message: "", messageKind: "", ...NO_FOCUS_MODE_SWITCH }),
225
- resetPassword: async (s) => {
342
+ showRegister: (s) => derive({ ...s, isRegistering: true, isRecovering: false, ...NO_FEEDBACK, ...NO_FOCUS_MODE_SWITCH }),
343
+ showSignIn: (s) => derive({ ...s, isRegistering: false, isRecovering: false, ...NO_FEEDBACK, ...NO_FOCUS_MODE_SWITCH }),
344
+ showRecovery: (s) => derive({ ...s, isRegistering: false, isRecovering: true, password: "", ...NO_FEEDBACK, ...NO_FOCUS_MODE_SWITCH }),
345
+ beginAuth: (s) => derive({ ...s, ...NO_FEEDBACK, authBusy: true }),
346
+ /** "Back to sign in" on the Pending approval panel. */
347
+ closePendingApproval: (s) => derive({ ...s, pendingApproval: false, isRegistering: false, isRecovering: false, ...NO_FEEDBACK, ...NO_FOCUS_MODE_SWITCH }),
348
+ resetPassword: async (s): Promise<S> => {
349
+ if (authInFlight) return s;
350
+ const checked = invalid(s, "reset");
351
+ if (checked) return checked;
226
352
  const email = s.email.trim();
227
- if (!email || !email.includes("@")) return derive({ ...s, message: copyParam("storeAccount.validationEmail", "Enter a valid email address."), messageKind: "error" });
353
+ authInFlight = true;
354
+ const mine = attempt;
228
355
  try {
229
- await dispatch("himi.wix.resetPassword", { email });
356
+ await svc.beginAuth();
357
+ rejectResult(await dispatch("himi.wix.resetPassword", { email }), "reset");
358
+ if (mine !== attempt) return derive({ ...svc.getState(), authBusy: false });
230
359
  return derive({
231
- ...s,
360
+ ...svc.getState(),
232
361
  email,
362
+ authBusy: false,
233
363
  isRecovering: false,
234
- message: copyParam("storeAccount.resetSent", "If that email is registered, a reset link is on its way."), messageKind: "success",
235
- });
236
- } catch {
237
- return derive({ ...s, message: copyParam("storeAccount.resetFailed", "Reset email could not be sent. Try again."), messageKind: "error" });
238
- }
239
- },
240
- signIn: async (s) => {
241
- if (!s.email || !s.password) return derive({ ...s, message: copyParam("storeAccount.validationEmailPassword", "Enter your email and password."), messageKind: "error" });
242
- try {
243
- await dispatch("himi.wix.signInDirect", { email: s.email, password: s.password });
244
- } catch {
245
- return derive({ ...s, message: copyParam("storeAccount.signInFailed", "Sign in failed. Try again."), messageKind: "error" });
246
- }
247
- if (!(await signedInEmail())) return derive({ ...s, message: copyParam("storeAccount.signInCheckDetails", "Sign in failed. Check your details."), messageKind: "error" });
248
- return leave(s);
249
- },
250
- register: async (s) => {
251
- if (!s.email || !s.password) return derive({ ...s, message: copyParam("storeAccount.validationEmailPassword", "Enter your email and password."), messageKind: "error" });
252
- try {
253
- await dispatch("himi.wix.registerDirect", {
254
- email: s.email,
255
- password: s.password,
256
- firstName: s.firstName,
257
- lastName: s.lastName,
364
+ message: copyTemplate("storeAccount.resetSentTo", "We sent a reset link to {email}. Check your inbox.", { email }), messageKind: "success",
258
365
  });
259
- } catch {
260
- return derive({ ...s, message: copyParam("storeAccount.registerFailed", "Account creation failed. Try another email."), messageKind: "error" });
366
+ } catch (error) {
367
+ // No account for this email: said under Email, as at sign in. A second request inside
368
+ // Wix's window: "We already sent you an email".
369
+ if (mine !== attempt) return derive({ ...svc.getState(), authBusy: false });
370
+ return failed(svc.getState(), authFailure(error, "reset", translated));
371
+ } finally {
372
+ authInFlight = false;
261
373
  }
262
- if (!(await signedInEmail())) return derive({ ...s, message: copyParam("storeAccount.registerTrySignIn", "Account creation failed. Try signing in."), messageKind: "error" });
263
- return leave(s);
264
374
  },
375
+ signIn: (s) => authenticate(s, "signIn"),
376
+ register: (s) => authenticate(s, "register"),
265
377
  },
266
378
  });
267
379