@erikey/react 0.2.0 → 0.2.2

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/dist/index.js CHANGED
@@ -1639,6 +1639,9 @@ function createDashboardClient(config) {
1639
1639
  });
1640
1640
  }
1641
1641
 
1642
+ // src/auth-client.ts
1643
+ var import_react3 = require("react");
1644
+
1642
1645
  // src/lib/cross-origin-auth.ts
1643
1646
  function shouldUseBearerAuth(authApiUrl) {
1644
1647
  if (typeof window === "undefined") {
@@ -1709,53 +1712,110 @@ function createAuthClient2(config) {
1709
1712
  if (!useBearerAuth) {
1710
1713
  return client;
1711
1714
  }
1712
- return {
1713
- ...client,
1714
- signIn: {
1715
- ...client.signIn,
1716
- email: async (...args) => {
1717
- const result = await client.signIn.email(...args);
1718
- const token = result.data?.token;
1719
- const sessionId = result.data?.session?.id || "session";
1720
- if (token) {
1721
- const session = {
1722
- id: sessionId,
1723
- token,
1724
- expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1e3).toISOString()
1725
- };
1726
- storeToken(projectId, session);
1727
- }
1728
- return result;
1729
- },
1730
- // Social OAuth works as-is (redirect-based, no token in response)
1731
- social: client.signIn.social
1732
- },
1733
- signUp: {
1734
- ...client.signUp,
1735
- email: async (...args) => {
1736
- const result = await client.signUp.email(...args);
1737
- const token = result.data?.token;
1738
- const sessionId = result.data?.session?.id || "session";
1739
- if (token) {
1740
- const session = {
1741
- id: sessionId,
1742
- token,
1743
- expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1e3).toISOString()
1744
- };
1745
- storeToken(projectId, session);
1746
- }
1747
- return result;
1715
+ return new Proxy({}, {
1716
+ get(_target, prop) {
1717
+ if (typeof prop === "symbol") {
1718
+ return client[prop];
1748
1719
  }
1749
- },
1750
- signOut: async (...args) => {
1751
- clearToken(projectId);
1752
- return client.signOut(...args);
1753
- },
1754
- // useSession works as-is - it uses the Bearer token from fetchOptions
1755
- useSession: client.useSession,
1756
- // Pass through other methods
1757
- getSession: client.getSession
1758
- };
1720
+ const mergeHeaders = (args) => {
1721
+ const [data, ...rest] = args;
1722
+ if (!data || typeof data !== "object") return args;
1723
+ const existingHeaders = data.fetchOptions?.headers || {};
1724
+ const mergedData = {
1725
+ ...data,
1726
+ fetchOptions: {
1727
+ ...data.fetchOptions,
1728
+ headers: {
1729
+ "X-Project-Id": projectId,
1730
+ ...existingHeaders
1731
+ }
1732
+ }
1733
+ };
1734
+ return [mergedData, ...rest];
1735
+ };
1736
+ if (prop === "signIn") {
1737
+ return new Proxy(client.signIn, {
1738
+ get(_signInTarget, signInProp) {
1739
+ if (signInProp === "email") {
1740
+ return async (...args) => {
1741
+ const mergedArgs = mergeHeaders(args);
1742
+ const result = await client.signIn.email(...mergedArgs);
1743
+ const token = result.data?.token;
1744
+ if (token) {
1745
+ const session = {
1746
+ id: result.data?.session?.id || "session",
1747
+ token,
1748
+ expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1e3).toISOString()
1749
+ };
1750
+ storeToken(projectId, session);
1751
+ }
1752
+ return result;
1753
+ };
1754
+ }
1755
+ return client.signIn[signInProp];
1756
+ }
1757
+ });
1758
+ }
1759
+ if (prop === "signUp") {
1760
+ return new Proxy(client.signUp, {
1761
+ get(_signUpTarget, signUpProp) {
1762
+ if (signUpProp === "email") {
1763
+ return async (...args) => {
1764
+ const mergedArgs = mergeHeaders(args);
1765
+ const result = await client.signUp.email(...mergedArgs);
1766
+ const token = result.data?.token;
1767
+ if (token) {
1768
+ const session = {
1769
+ id: result.data?.session?.id || "session",
1770
+ token,
1771
+ expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1e3).toISOString()
1772
+ };
1773
+ storeToken(projectId, session);
1774
+ }
1775
+ return result;
1776
+ };
1777
+ }
1778
+ return client.signUp[signUpProp];
1779
+ }
1780
+ });
1781
+ }
1782
+ if (prop === "signOut") {
1783
+ return async (...args) => {
1784
+ clearToken(projectId);
1785
+ return client.signOut(...args);
1786
+ };
1787
+ }
1788
+ if (prop === "useSession") {
1789
+ return function useSession() {
1790
+ const [data, setData] = (0, import_react3.useState)(null);
1791
+ const [isPending, setIsPending] = (0, import_react3.useState)(true);
1792
+ const [error, setError] = (0, import_react3.useState)(null);
1793
+ const refetch = (0, import_react3.useCallback)(async () => {
1794
+ setIsPending(true);
1795
+ try {
1796
+ const result = await client.getSession();
1797
+ if (result.error) {
1798
+ setError(result.error);
1799
+ setData(null);
1800
+ } else {
1801
+ setData(result.data);
1802
+ setError(null);
1803
+ }
1804
+ } catch (e) {
1805
+ setError(e);
1806
+ setData(null);
1807
+ }
1808
+ setIsPending(false);
1809
+ }, []);
1810
+ (0, import_react3.useEffect)(() => {
1811
+ refetch();
1812
+ }, [refetch]);
1813
+ return { data, isPending, error, refetch };
1814
+ };
1815
+ }
1816
+ return client[prop];
1817
+ }
1818
+ });
1759
1819
  }
1760
1820
 
1761
1821
  // ../../sandpack-auth/dist/client/index.js