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.js CHANGED
@@ -9657,10 +9657,44 @@ var setConfigs = (configs) => {
9657
9657
  });
9658
9658
  publicRequest.setBaseUrl(CONFIGS.BASE_URL);
9659
9659
  };
9660
+ var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
9660
9661
  var authorize2 = async (data) => {
9662
+ var _a, _b, _c, _d;
9663
+ if (!isBrowser) {
9664
+ const step1 = new FormData();
9665
+ step1.append("email", data.email);
9666
+ step1.append("password", data.password);
9667
+ const authResponse = await publicRequest.post(
9668
+ `v1/oauth/authorize-rn?client_id=${CONFIGS.CLIENT_ID}`,
9669
+ step1,
9670
+ { headers: { "Content-Type": "multipart/form-data" } }
9671
+ );
9672
+ const code = (_b = (_a = authResponse.data) == null ? void 0 : _a.data) == null ? void 0 : _b.code;
9673
+ if (!code) throw new Error("Authorization code not received");
9674
+ const step2 = new FormData();
9675
+ step2.append("code", code);
9676
+ step2.append("scope", "profile");
9677
+ step2.append("grant_type", "authorization_code");
9678
+ step2.append("client_id", CONFIGS.CLIENT_ID);
9679
+ step2.append("client_secret", CONFIGS.CLIENT_SECRET);
9680
+ const tokenResponse = await publicRequest.post("v1/oauth/access_token", step2, {
9681
+ headers: { "Content-Type": "multipart/form-data" }
9682
+ });
9683
+ const accessToken = (_c = tokenResponse.data) == null ? void 0 : _c.access_token;
9684
+ const refreshToken = (_d = tokenResponse.data) == null ? void 0 : _d.refresh_token;
9685
+ if (accessToken) {
9686
+ storage.setItem("access_token", accessToken);
9687
+ publicRequest.defaults.headers.common["Authorization"] = `Bearer ${accessToken}`;
9688
+ }
9689
+ if (refreshToken) {
9690
+ storage.setItem("refresh_token", refreshToken);
9691
+ }
9692
+ return getProfileData();
9693
+ }
9661
9694
  const response = await publicRequest.post(
9662
- `/auth?clientId=${CONFIGS.CLIENT_ID || "e9d8f8922797b4621e562255afe90dbf"}`,
9663
- data
9695
+ `/auth?clientId=${CONFIGS.CLIENT_ID}`,
9696
+ data,
9697
+ { withCredentials: true }
9664
9698
  );
9665
9699
  return response.data;
9666
9700
  };
@@ -9671,12 +9705,18 @@ var register = async (data) => {
9671
9705
  return response.data;
9672
9706
  };
9673
9707
  var getProfileData = async () => {
9674
- const response = await publicRequest.get("/customer/profile/");
9708
+ const response = await publicRequest.get("/customer/profile/", { withCredentials: isBrowser });
9675
9709
  return response.data;
9676
9710
  };
9677
9711
  var logout = async () => {
9678
- const response = await publicRequest.delete("/auth");
9679
- return response.data;
9712
+ storage.removeItem("access_token");
9713
+ storage.removeItem("refresh_token");
9714
+ delete publicRequest.defaults.headers.common["Authorization"];
9715
+ if (isBrowser) {
9716
+ const response = await publicRequest.delete("/auth", { withCredentials: true });
9717
+ return response.data;
9718
+ }
9719
+ return { success: true };
9680
9720
  };
9681
9721
  var checkEmailExists = async (email) => {
9682
9722
  var _a;
@@ -9689,11 +9729,7 @@ var checkEmailExists = async (email) => {
9689
9729
  const response = await publicRequest.post(
9690
9730
  url,
9691
9731
  formData,
9692
- {
9693
- headers: {
9694
- "Content-Type": "multipart/form-data"
9695
- }
9696
- }
9732
+ { headers: { "Content-Type": "multipart/form-data" } }
9697
9733
  );
9698
9734
  return {
9699
9735
  exists: response.data.exists === 1,
@@ -9707,7 +9743,6 @@ var checkEmailExists = async (email) => {
9707
9743
  };
9708
9744
  }
9709
9745
  };
9710
- var isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined";
9711
9746
  var setLoggedUserData = (data) => ({
9712
9747
  id: data.id,
9713
9748
  first_name: data.firstName,
@@ -9784,6 +9819,13 @@ publicRequest.interceptors.request.use((config) => {
9784
9819
  if (CONFIGS.BASE_URL) {
9785
9820
  config.baseURL = CONFIGS.BASE_URL + "/api";
9786
9821
  }
9822
+ if (!isBrowser && !config.headers["Authorization"]) {
9823
+ const accessToken = storage.getItem("access_token");
9824
+ if (accessToken) {
9825
+ config.headers["Authorization"] = `Bearer ${accessToken}`;
9826
+ publicRequest.defaults.headers.common["Authorization"] = `Bearer ${accessToken}`;
9827
+ }
9828
+ }
9787
9829
  return config;
9788
9830
  });
9789
9831
  publicRequest.interceptors.response.use(
@@ -9814,6 +9856,10 @@ publicRequest.interceptors.response.use(
9814
9856
  if (errorType === "invalid_token") {
9815
9857
  window.location.href = "/";
9816
9858
  }
9859
+ } else {
9860
+ storage.removeItem("access_token");
9861
+ storage.removeItem("refresh_token");
9862
+ delete publicRequest.defaults.headers.common["Authorization"];
9817
9863
  }
9818
9864
  }
9819
9865
  const authGuestToken2 = (0, import_get2.default)(error, "response.headers.authorization-guest");