@misiki/gocommerce-connector 0.4.7 → 0.4.9

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.
@@ -2066,11 +2066,24 @@ var orderService = new OrderService();
2066
2066
 
2067
2067
  // src/services/user-service.ts
2068
2068
  var UserService = class extends BaseService {
2069
- /** Email and password. The answer is the user object the storefront keeps in its `me` cookie. */
2070
- async login({ email, password }) {
2069
+ /**
2070
+ * Email and password. The answer is the user object the storefront keeps in its `me` cookie.
2071
+ * With `phone` and `phoneToken` from `verifyOtp({ askIfNew: true })`, the phone is added to
2072
+ * the account as it signs in (D123).
2073
+ */
2074
+ async login({
2075
+ email,
2076
+ password,
2077
+ phone,
2078
+ phoneToken
2079
+ }) {
2071
2080
  const auth = await this.account("/x/identity/login", {
2072
2081
  method: "POST",
2073
- body: { email: String(email ?? "").trim(), password: String(password ?? "") }
2082
+ body: {
2083
+ email: String(email ?? "").trim(),
2084
+ password: String(password ?? ""),
2085
+ ...phoneToken ? { phone: String(phone ?? "").trim(), phone_token: phoneToken } : {}
2086
+ }
2074
2087
  });
2075
2088
  return this.signedIn(auth);
2076
2089
  }
@@ -2234,19 +2247,42 @@ var AuthService = class extends UserService {
2234
2247
  return { errorCode: err.code ?? "invalid_token", message: err.message };
2235
2248
  }
2236
2249
  }
2237
- /** Texts a 4-digit sign-in code to `phone`, which needs its country code. */
2238
- async getOtp({ phone }) {
2250
+ /**
2251
+ * Texts a 4-digit sign-in code to `phone`, which needs its country code. `domain`, the page's
2252
+ * host and in the browser read from it, lets the engine end the text "@domain #code" when the
2253
+ * host is the store's own, which Chrome on Android reads to fill the code in (D124).
2254
+ */
2255
+ async getOtp({ phone, domain }) {
2256
+ const site = domain ?? (typeof window !== "undefined" ? window.location?.hostname : void 0);
2239
2257
  const res = await this.account("/x/identity/otp", {
2240
2258
  method: "POST",
2241
- body: { phone: String(phone ?? "").trim() }
2259
+ body: { phone: String(phone ?? "").trim(), ...site ? { domain: site } : {} }
2242
2260
  });
2243
2261
  return { expiresInMinutes: res?.expires_in_minutes ?? 10 };
2244
2262
  }
2245
- /** Signs in the account on `phone` with the code it was sent, opening one the first time. */
2246
- async verifyOtp({ phone, otp }) {
2247
- const auth = await this.account("/x/identity/otp/verify", {
2263
+ /**
2264
+ * Signs in the account on `phone` with the code it was sent, opening one the first time.
2265
+ * With `askIfNew`, on a store that signs in either way, a phone with no account opens none:
2266
+ * the answer is `{ newPhone: true, phoneToken }`, and the storefront asks "Already have an
2267
+ * account?" — `login` with the token adds the phone to it, `openPhoneAccount` opens a new one (D123).
2268
+ */
2269
+ async verifyOtp({ phone, otp, askIfNew }) {
2270
+ const res = await this.account("/x/identity/otp/verify", {
2248
2271
  method: "POST",
2249
- body: { phone: String(phone ?? "").trim(), code: String(otp ?? "").trim() }
2272
+ body: {
2273
+ phone: String(phone ?? "").trim(),
2274
+ code: String(otp ?? "").trim(),
2275
+ ...askIfNew ? { ask_if_new: true } : {}
2276
+ }
2277
+ });
2278
+ if ("new_phone" in res && res.new_phone) return { newPhone: true, phoneToken: res.phone_token };
2279
+ return this.signedIn(res);
2280
+ }
2281
+ /** Opens a new account on a phone `verifyOtp({ askIfNew: true })` proved, and signs it in (D123). */
2282
+ async openPhoneAccount({ phone, phoneToken }) {
2283
+ const auth = await this.account("/x/identity/otp/new-account", {
2284
+ method: "POST",
2285
+ body: { phone: String(phone ?? "").trim(), phone_token: phoneToken }
2250
2286
  });
2251
2287
  return this.signedIn(auth);
2252
2288
  }