@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.
@@ -4672,10 +4672,16 @@ declare const orderService: OrderService;
4672
4672
  * as "Accounts are not available on this store" rather than as a route error.
4673
4673
  */
4674
4674
  declare class UserService extends BaseService {
4675
- /** Email and password. The answer is the user object the storefront keeps in its `me` cookie. */
4676
- login({ email, password }: {
4675
+ /**
4676
+ * Email and password. The answer is the user object the storefront keeps in its `me` cookie.
4677
+ * With `phone` and `phoneToken` from `verifyOtp({ askIfNew: true })`, the phone is added to
4678
+ * the account as it signs in (D123).
4679
+ */
4680
+ login({ email, password, phone, phoneToken, }: {
4677
4681
  email: string;
4678
4682
  password: string;
4683
+ phone?: string;
4684
+ phoneToken?: string;
4679
4685
  cartId?: string | null;
4680
4686
  }): Promise<{
4681
4687
  id: string;
@@ -4993,16 +4999,49 @@ declare class AuthService extends UserService {
4993
4999
  errorCode: string;
4994
5000
  message: string;
4995
5001
  }>;
4996
- /** Texts a 4-digit sign-in code to `phone`, which needs its country code. */
4997
- getOtp({ phone }: {
5002
+ /**
5003
+ * Texts a 4-digit sign-in code to `phone`, which needs its country code. `domain`, the page's
5004
+ * host and in the browser read from it, lets the engine end the text "@domain #code" when the
5005
+ * host is the store's own, which Chrome on Android reads to fill the code in (D124).
5006
+ */
5007
+ getOtp({ phone, domain }: {
4998
5008
  phone: string;
5009
+ domain?: string;
4999
5010
  }): Promise<{
5000
5011
  expiresInMinutes: number;
5001
5012
  }>;
5002
- /** Signs in the account on `phone` with the code it was sent, opening one the first time. */
5003
- verifyOtp({ phone, otp }: {
5013
+ /**
5014
+ * Signs in the account on `phone` with the code it was sent, opening one the first time.
5015
+ * With `askIfNew`, on a store that signs in either way, a phone with no account opens none:
5016
+ * the answer is `{ newPhone: true, phoneToken }`, and the storefront asks "Already have an
5017
+ * account?" — `login` with the token adds the phone to it, `openPhoneAccount` opens a new one (D123).
5018
+ */
5019
+ verifyOtp({ phone, otp, askIfNew }: {
5004
5020
  phone: string;
5005
5021
  otp: string;
5022
+ askIfNew?: boolean;
5023
+ }): Promise<{
5024
+ id: string;
5025
+ userId: string;
5026
+ email: string;
5027
+ phone: string | null;
5028
+ firstName: string;
5029
+ lastName: string;
5030
+ name: string;
5031
+ fullName: string;
5032
+ avatar: null;
5033
+ role: string;
5034
+ emailVerified: boolean;
5035
+ phoneVerified: boolean;
5036
+ storeId: null;
5037
+ } | {
5038
+ newPhone: true;
5039
+ phoneToken: string;
5040
+ }>;
5041
+ /** Opens a new account on a phone `verifyOtp({ askIfNew: true })` proved, and signs it in (D123). */
5042
+ openPhoneAccount({ phone, phoneToken }: {
5043
+ phone: string;
5044
+ phoneToken: string;
5006
5045
  }): Promise<{
5007
5046
  id: string;
5008
5047
  userId: string;
@@ -4672,10 +4672,16 @@ declare const orderService: OrderService;
4672
4672
  * as "Accounts are not available on this store" rather than as a route error.
4673
4673
  */
4674
4674
  declare class UserService extends BaseService {
4675
- /** Email and password. The answer is the user object the storefront keeps in its `me` cookie. */
4676
- login({ email, password }: {
4675
+ /**
4676
+ * Email and password. The answer is the user object the storefront keeps in its `me` cookie.
4677
+ * With `phone` and `phoneToken` from `verifyOtp({ askIfNew: true })`, the phone is added to
4678
+ * the account as it signs in (D123).
4679
+ */
4680
+ login({ email, password, phone, phoneToken, }: {
4677
4681
  email: string;
4678
4682
  password: string;
4683
+ phone?: string;
4684
+ phoneToken?: string;
4679
4685
  cartId?: string | null;
4680
4686
  }): Promise<{
4681
4687
  id: string;
@@ -4993,16 +4999,49 @@ declare class AuthService extends UserService {
4993
4999
  errorCode: string;
4994
5000
  message: string;
4995
5001
  }>;
4996
- /** Texts a 4-digit sign-in code to `phone`, which needs its country code. */
4997
- getOtp({ phone }: {
5002
+ /**
5003
+ * Texts a 4-digit sign-in code to `phone`, which needs its country code. `domain`, the page's
5004
+ * host and in the browser read from it, lets the engine end the text "@domain #code" when the
5005
+ * host is the store's own, which Chrome on Android reads to fill the code in (D124).
5006
+ */
5007
+ getOtp({ phone, domain }: {
4998
5008
  phone: string;
5009
+ domain?: string;
4999
5010
  }): Promise<{
5000
5011
  expiresInMinutes: number;
5001
5012
  }>;
5002
- /** Signs in the account on `phone` with the code it was sent, opening one the first time. */
5003
- verifyOtp({ phone, otp }: {
5013
+ /**
5014
+ * Signs in the account on `phone` with the code it was sent, opening one the first time.
5015
+ * With `askIfNew`, on a store that signs in either way, a phone with no account opens none:
5016
+ * the answer is `{ newPhone: true, phoneToken }`, and the storefront asks "Already have an
5017
+ * account?" — `login` with the token adds the phone to it, `openPhoneAccount` opens a new one (D123).
5018
+ */
5019
+ verifyOtp({ phone, otp, askIfNew }: {
5004
5020
  phone: string;
5005
5021
  otp: string;
5022
+ askIfNew?: boolean;
5023
+ }): Promise<{
5024
+ id: string;
5025
+ userId: string;
5026
+ email: string;
5027
+ phone: string | null;
5028
+ firstName: string;
5029
+ lastName: string;
5030
+ name: string;
5031
+ fullName: string;
5032
+ avatar: null;
5033
+ role: string;
5034
+ emailVerified: boolean;
5035
+ phoneVerified: boolean;
5036
+ storeId: null;
5037
+ } | {
5038
+ newPhone: true;
5039
+ phoneToken: string;
5040
+ }>;
5041
+ /** Opens a new account on a phone `verifyOtp({ askIfNew: true })` proved, and signs it in (D123). */
5042
+ openPhoneAccount({ phone, phoneToken }: {
5043
+ phone: string;
5044
+ phoneToken: string;
5006
5045
  }): Promise<{
5007
5046
  id: string;
5008
5047
  userId: string;
package/dist/index.cjs CHANGED
@@ -2085,11 +2085,24 @@ var orderService = new OrderService();
2085
2085
 
2086
2086
  // src/services/user-service.ts
2087
2087
  var UserService = class extends BaseService {
2088
- /** Email and password. The answer is the user object the storefront keeps in its `me` cookie. */
2089
- async login({ email, password }) {
2088
+ /**
2089
+ * Email and password. The answer is the user object the storefront keeps in its `me` cookie.
2090
+ * With `phone` and `phoneToken` from `verifyOtp({ askIfNew: true })`, the phone is added to
2091
+ * the account as it signs in (D123).
2092
+ */
2093
+ async login({
2094
+ email,
2095
+ password,
2096
+ phone,
2097
+ phoneToken
2098
+ }) {
2090
2099
  const auth = await this.account("/x/identity/login", {
2091
2100
  method: "POST",
2092
- body: { email: String(email ?? "").trim(), password: String(password ?? "") }
2101
+ body: {
2102
+ email: String(email ?? "").trim(),
2103
+ password: String(password ?? ""),
2104
+ ...phoneToken ? { phone: String(phone ?? "").trim(), phone_token: phoneToken } : {}
2105
+ }
2093
2106
  });
2094
2107
  return this.signedIn(auth);
2095
2108
  }
@@ -2253,19 +2266,42 @@ var AuthService = class extends UserService {
2253
2266
  return { errorCode: err.code ?? "invalid_token", message: err.message };
2254
2267
  }
2255
2268
  }
2256
- /** Texts a 4-digit sign-in code to `phone`, which needs its country code. */
2257
- async getOtp({ phone }) {
2269
+ /**
2270
+ * Texts a 4-digit sign-in code to `phone`, which needs its country code. `domain`, the page's
2271
+ * host and in the browser read from it, lets the engine end the text "@domain #code" when the
2272
+ * host is the store's own, which Chrome on Android reads to fill the code in (D124).
2273
+ */
2274
+ async getOtp({ phone, domain }) {
2275
+ const site = domain ?? (typeof window !== "undefined" ? window.location?.hostname : void 0);
2258
2276
  const res = await this.account("/x/identity/otp", {
2259
2277
  method: "POST",
2260
- body: { phone: String(phone ?? "").trim() }
2278
+ body: { phone: String(phone ?? "").trim(), ...site ? { domain: site } : {} }
2261
2279
  });
2262
2280
  return { expiresInMinutes: res?.expires_in_minutes ?? 10 };
2263
2281
  }
2264
- /** Signs in the account on `phone` with the code it was sent, opening one the first time. */
2265
- async verifyOtp({ phone, otp }) {
2266
- const auth = await this.account("/x/identity/otp/verify", {
2282
+ /**
2283
+ * Signs in the account on `phone` with the code it was sent, opening one the first time.
2284
+ * With `askIfNew`, on a store that signs in either way, a phone with no account opens none:
2285
+ * the answer is `{ newPhone: true, phoneToken }`, and the storefront asks "Already have an
2286
+ * account?" — `login` with the token adds the phone to it, `openPhoneAccount` opens a new one (D123).
2287
+ */
2288
+ async verifyOtp({ phone, otp, askIfNew }) {
2289
+ const res = await this.account("/x/identity/otp/verify", {
2267
2290
  method: "POST",
2268
- body: { phone: String(phone ?? "").trim(), code: String(otp ?? "").trim() }
2291
+ body: {
2292
+ phone: String(phone ?? "").trim(),
2293
+ code: String(otp ?? "").trim(),
2294
+ ...askIfNew ? { ask_if_new: true } : {}
2295
+ }
2296
+ });
2297
+ if ("new_phone" in res && res.new_phone) return { newPhone: true, phoneToken: res.phone_token };
2298
+ return this.signedIn(res);
2299
+ }
2300
+ /** Opens a new account on a phone `verifyOtp({ askIfNew: true })` proved, and signs it in (D123). */
2301
+ async openPhoneAccount({ phone, phoneToken }) {
2302
+ const auth = await this.account("/x/identity/otp/new-account", {
2303
+ method: "POST",
2304
+ body: { phone: String(phone ?? "").trim(), phone_token: phoneToken }
2269
2305
  });
2270
2306
  return this.signedIn(auth);
2271
2307
  }