@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.mjs CHANGED
@@ -1613,6 +1613,9 @@ function createDashboardClient(config) {
1613
1613
  });
1614
1614
  }
1615
1615
 
1616
+ // src/auth-client.ts
1617
+ import { useState, useEffect, useCallback as useCallback2 } from "react";
1618
+
1616
1619
  // src/lib/cross-origin-auth.ts
1617
1620
  function shouldUseBearerAuth(authApiUrl) {
1618
1621
  if (typeof window === "undefined") {
@@ -1683,53 +1686,110 @@ function createAuthClient2(config) {
1683
1686
  if (!useBearerAuth) {
1684
1687
  return client;
1685
1688
  }
1686
- return {
1687
- ...client,
1688
- signIn: {
1689
- ...client.signIn,
1690
- email: async (...args) => {
1691
- const result = await client.signIn.email(...args);
1692
- const token = result.data?.token;
1693
- const sessionId = result.data?.session?.id || "session";
1694
- if (token) {
1695
- const session = {
1696
- id: sessionId,
1697
- token,
1698
- expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1e3).toISOString()
1699
- };
1700
- storeToken(projectId, session);
1701
- }
1702
- return result;
1703
- },
1704
- // Social OAuth works as-is (redirect-based, no token in response)
1705
- social: client.signIn.social
1706
- },
1707
- signUp: {
1708
- ...client.signUp,
1709
- email: async (...args) => {
1710
- const result = await client.signUp.email(...args);
1711
- const token = result.data?.token;
1712
- const sessionId = result.data?.session?.id || "session";
1713
- if (token) {
1714
- const session = {
1715
- id: sessionId,
1716
- token,
1717
- expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1e3).toISOString()
1718
- };
1719
- storeToken(projectId, session);
1720
- }
1721
- return result;
1689
+ return new Proxy({}, {
1690
+ get(_target, prop) {
1691
+ if (typeof prop === "symbol") {
1692
+ return client[prop];
1722
1693
  }
1723
- },
1724
- signOut: async (...args) => {
1725
- clearToken(projectId);
1726
- return client.signOut(...args);
1727
- },
1728
- // useSession works as-is - it uses the Bearer token from fetchOptions
1729
- useSession: client.useSession,
1730
- // Pass through other methods
1731
- getSession: client.getSession
1732
- };
1694
+ const mergeHeaders = (args) => {
1695
+ const [data, ...rest] = args;
1696
+ if (!data || typeof data !== "object") return args;
1697
+ const existingHeaders = data.fetchOptions?.headers || {};
1698
+ const mergedData = {
1699
+ ...data,
1700
+ fetchOptions: {
1701
+ ...data.fetchOptions,
1702
+ headers: {
1703
+ "X-Project-Id": projectId,
1704
+ ...existingHeaders
1705
+ }
1706
+ }
1707
+ };
1708
+ return [mergedData, ...rest];
1709
+ };
1710
+ if (prop === "signIn") {
1711
+ return new Proxy(client.signIn, {
1712
+ get(_signInTarget, signInProp) {
1713
+ if (signInProp === "email") {
1714
+ return async (...args) => {
1715
+ const mergedArgs = mergeHeaders(args);
1716
+ const result = await client.signIn.email(...mergedArgs);
1717
+ const token = result.data?.token;
1718
+ if (token) {
1719
+ const session = {
1720
+ id: result.data?.session?.id || "session",
1721
+ token,
1722
+ expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1e3).toISOString()
1723
+ };
1724
+ storeToken(projectId, session);
1725
+ }
1726
+ return result;
1727
+ };
1728
+ }
1729
+ return client.signIn[signInProp];
1730
+ }
1731
+ });
1732
+ }
1733
+ if (prop === "signUp") {
1734
+ return new Proxy(client.signUp, {
1735
+ get(_signUpTarget, signUpProp) {
1736
+ if (signUpProp === "email") {
1737
+ return async (...args) => {
1738
+ const mergedArgs = mergeHeaders(args);
1739
+ const result = await client.signUp.email(...mergedArgs);
1740
+ const token = result.data?.token;
1741
+ if (token) {
1742
+ const session = {
1743
+ id: result.data?.session?.id || "session",
1744
+ token,
1745
+ expiresAt: new Date(Date.now() + 7 * 24 * 60 * 60 * 1e3).toISOString()
1746
+ };
1747
+ storeToken(projectId, session);
1748
+ }
1749
+ return result;
1750
+ };
1751
+ }
1752
+ return client.signUp[signUpProp];
1753
+ }
1754
+ });
1755
+ }
1756
+ if (prop === "signOut") {
1757
+ return async (...args) => {
1758
+ clearToken(projectId);
1759
+ return client.signOut(...args);
1760
+ };
1761
+ }
1762
+ if (prop === "useSession") {
1763
+ return function useSession() {
1764
+ const [data, setData] = useState(null);
1765
+ const [isPending, setIsPending] = useState(true);
1766
+ const [error, setError] = useState(null);
1767
+ const refetch = useCallback2(async () => {
1768
+ setIsPending(true);
1769
+ try {
1770
+ const result = await client.getSession();
1771
+ if (result.error) {
1772
+ setError(result.error);
1773
+ setData(null);
1774
+ } else {
1775
+ setData(result.data);
1776
+ setError(null);
1777
+ }
1778
+ } catch (e) {
1779
+ setError(e);
1780
+ setData(null);
1781
+ }
1782
+ setIsPending(false);
1783
+ }, []);
1784
+ useEffect(() => {
1785
+ refetch();
1786
+ }, [refetch]);
1787
+ return { data, isPending, error, refetch };
1788
+ };
1789
+ }
1790
+ return client[prop];
1791
+ }
1792
+ });
1733
1793
  }
1734
1794
 
1735
1795
  // ../../sandpack-auth/dist/client/index.js