tf-checkout-react-native 1.0.45 → 1.0.47

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/lib/index.mjs CHANGED
@@ -9580,10 +9580,44 @@ var setConfigs = (configs) => {
9580
9580
  });
9581
9581
  publicRequest.setBaseUrl(CONFIGS.BASE_URL);
9582
9582
  };
9583
+ var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
9583
9584
  var authorize2 = async (data) => {
9585
+ var _a, _b, _c, _d;
9586
+ if (!isBrowser) {
9587
+ const step1 = new FormData();
9588
+ step1.append("email", data.email);
9589
+ step1.append("password", data.password);
9590
+ const authResponse = await publicRequest.post(
9591
+ `v1/oauth/authorize-rn?client_id=${CONFIGS.CLIENT_ID}`,
9592
+ step1,
9593
+ { headers: { "Content-Type": "multipart/form-data" } }
9594
+ );
9595
+ const code = (_b = (_a = authResponse.data) == null ? void 0 : _a.data) == null ? void 0 : _b.code;
9596
+ if (!code) throw new Error("Authorization code not received");
9597
+ const step2 = new FormData();
9598
+ step2.append("code", code);
9599
+ step2.append("scope", "profile");
9600
+ step2.append("grant_type", "authorization_code");
9601
+ step2.append("client_id", CONFIGS.CLIENT_ID);
9602
+ step2.append("client_secret", CONFIGS.CLIENT_SECRET);
9603
+ const tokenResponse = await publicRequest.post("v1/oauth/access_token", step2, {
9604
+ headers: { "Content-Type": "multipart/form-data" }
9605
+ });
9606
+ const accessToken = (_c = tokenResponse.data) == null ? void 0 : _c.access_token;
9607
+ const refreshToken = (_d = tokenResponse.data) == null ? void 0 : _d.refresh_token;
9608
+ if (accessToken) {
9609
+ storage.setItem("access_token", accessToken);
9610
+ publicRequest.defaults.headers.common["Authorization"] = `Bearer ${accessToken}`;
9611
+ }
9612
+ if (refreshToken) {
9613
+ storage.setItem("refresh_token", refreshToken);
9614
+ }
9615
+ return getProfileData();
9616
+ }
9584
9617
  const response = await publicRequest.post(
9585
- `/auth?clientId=${CONFIGS.CLIENT_ID || "e9d8f8922797b4621e562255afe90dbf"}`,
9586
- data
9618
+ `/auth?clientId=${CONFIGS.CLIENT_ID}`,
9619
+ data,
9620
+ { withCredentials: true }
9587
9621
  );
9588
9622
  return response.data;
9589
9623
  };
@@ -9594,12 +9628,18 @@ var register = async (data) => {
9594
9628
  return response.data;
9595
9629
  };
9596
9630
  var getProfileData = async () => {
9597
- const response = await publicRequest.get("/customer/profile/");
9631
+ const response = await publicRequest.get("/customer/profile/", { withCredentials: isBrowser });
9598
9632
  return response.data;
9599
9633
  };
9600
9634
  var logout = async () => {
9601
- const response = await publicRequest.delete("/auth");
9602
- return response.data;
9635
+ storage.removeItem("access_token");
9636
+ storage.removeItem("refresh_token");
9637
+ delete publicRequest.defaults.headers.common["Authorization"];
9638
+ if (isBrowser) {
9639
+ const response = await publicRequest.delete("/auth", { withCredentials: true });
9640
+ return response.data;
9641
+ }
9642
+ return { success: true };
9603
9643
  };
9604
9644
  var checkEmailExists = async (email) => {
9605
9645
  var _a;
@@ -9612,11 +9652,7 @@ var checkEmailExists = async (email) => {
9612
9652
  const response = await publicRequest.post(
9613
9653
  url,
9614
9654
  formData,
9615
- {
9616
- headers: {
9617
- "Content-Type": "multipart/form-data"
9618
- }
9619
- }
9655
+ { headers: { "Content-Type": "multipart/form-data" } }
9620
9656
  );
9621
9657
  return {
9622
9658
  exists: response.data.exists === 1,
@@ -9630,7 +9666,6 @@ var checkEmailExists = async (email) => {
9630
9666
  };
9631
9667
  }
9632
9668
  };
9633
- var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
9634
9669
  var setLoggedUserData = (data) => ({
9635
9670
  id: data.id,
9636
9671
  first_name: data.firstName,
@@ -9707,6 +9742,13 @@ publicRequest.interceptors.request.use((config) => {
9707
9742
  if (CONFIGS.BASE_URL) {
9708
9743
  config.baseURL = CONFIGS.BASE_URL + "/api";
9709
9744
  }
9745
+ if (!isBrowser && !config.headers["Authorization"]) {
9746
+ const accessToken = storage.getItem("access_token");
9747
+ if (accessToken) {
9748
+ config.headers["Authorization"] = `Bearer ${accessToken}`;
9749
+ publicRequest.defaults.headers.common["Authorization"] = `Bearer ${accessToken}`;
9750
+ }
9751
+ }
9710
9752
  return config;
9711
9753
  });
9712
9754
  publicRequest.interceptors.response.use(
@@ -9737,6 +9779,10 @@ publicRequest.interceptors.response.use(
9737
9779
  if (errorType === "invalid_token") {
9738
9780
  window.location.href = "/";
9739
9781
  }
9782
+ } else {
9783
+ storage.removeItem("access_token");
9784
+ storage.removeItem("refresh_token");
9785
+ delete publicRequest.defaults.headers.common["Authorization"];
9740
9786
  }
9741
9787
  }
9742
9788
  const authGuestToken2 = _get2(error, "response.headers.authorization-guest");