@insforge/react 0.4.5 → 0.4.6

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.d.cts CHANGED
@@ -69,6 +69,11 @@ interface InsforgeContextValue {
69
69
  interface InsforgeProviderProps {
70
70
  children: ReactNode;
71
71
  baseUrl: string;
72
+ /**
73
+ * URL to redirect to after successful sign in (when token is detected in URL)
74
+ * @default '/'
75
+ */
76
+ afterSignInUrl?: string;
72
77
  onAuthChange?: (user: InsforgeUser | null) => void;
73
78
  onSignIn?: (authToken: string) => Promise<void>;
74
79
  onSignOut?: () => Promise<void>;
@@ -86,7 +91,10 @@ interface InsforgeProviderProps {
86
91
  *
87
92
  * export default function App() {
88
93
  * return (
89
- * <InsforgeProvider baseUrl={process.env.VITE_INSFORGE_BASE_URL}>
94
+ * <InsforgeProvider
95
+ * baseUrl={process.env.VITE_INSFORGE_BASE_URL}
96
+ * afterSignInUrl="/dashboard"
97
+ * >
90
98
  * {children}
91
99
  * </InsforgeProvider>
92
100
  * );
@@ -109,7 +117,7 @@ interface InsforgeProviderProps {
109
117
  * </InsforgeProvider>
110
118
  * ```
111
119
  */
112
- declare function InsforgeProvider({ children, baseUrl, onAuthChange, onSignIn, onSignOut, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
120
+ declare function InsforgeProvider({ children, baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
113
121
  /**
114
122
  * Hook to access Insforge context
115
123
  *
package/dist/index.d.ts CHANGED
@@ -69,6 +69,11 @@ interface InsforgeContextValue {
69
69
  interface InsforgeProviderProps {
70
70
  children: ReactNode;
71
71
  baseUrl: string;
72
+ /**
73
+ * URL to redirect to after successful sign in (when token is detected in URL)
74
+ * @default '/'
75
+ */
76
+ afterSignInUrl?: string;
72
77
  onAuthChange?: (user: InsforgeUser | null) => void;
73
78
  onSignIn?: (authToken: string) => Promise<void>;
74
79
  onSignOut?: () => Promise<void>;
@@ -86,7 +91,10 @@ interface InsforgeProviderProps {
86
91
  *
87
92
  * export default function App() {
88
93
  * return (
89
- * <InsforgeProvider baseUrl={process.env.VITE_INSFORGE_BASE_URL}>
94
+ * <InsforgeProvider
95
+ * baseUrl={process.env.VITE_INSFORGE_BASE_URL}
96
+ * afterSignInUrl="/dashboard"
97
+ * >
90
98
  * {children}
91
99
  * </InsforgeProvider>
92
100
  * );
@@ -109,7 +117,7 @@ interface InsforgeProviderProps {
109
117
  * </InsforgeProvider>
110
118
  * ```
111
119
  */
112
- declare function InsforgeProvider({ children, baseUrl, onAuthChange, onSignIn, onSignOut, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
120
+ declare function InsforgeProvider({ children, baseUrl, afterSignInUrl, onAuthChange, onSignIn, onSignOut, }: InsforgeProviderProps): react_jsx_runtime.JSX.Element;
113
121
  /**
114
122
  * Hook to access Insforge context
115
123
  *
package/dist/index.js CHANGED
@@ -23,6 +23,7 @@ var InsforgeContext = createContext(void 0);
23
23
  function InsforgeProvider({
24
24
  children,
25
25
  baseUrl,
26
+ afterSignInUrl = "/",
26
27
  onAuthChange,
27
28
  onSignIn,
28
29
  onSignOut
@@ -30,6 +31,7 @@ function InsforgeProvider({
30
31
  const [user, setUser] = useState(null);
31
32
  const [isLoaded, setIsLoaded] = useState(false);
32
33
  const refreshIntervalRef = useRef(null);
34
+ const hasProcessedCallbackRef = useRef(false);
33
35
  const [insforge] = useState(() => createClient({ baseUrl }));
34
36
  const loadAuthState = useCallback(async () => {
35
37
  try {
@@ -109,6 +111,22 @@ function InsforgeProvider({
109
111
  }
110
112
  };
111
113
  }, [loadAuthState]);
114
+ useEffect(() => {
115
+ if (!isLoaded || hasProcessedCallbackRef.current) {
116
+ return;
117
+ }
118
+ const searchParams = new URLSearchParams(window.location.search);
119
+ const accessToken = searchParams.get("access_token");
120
+ if (accessToken && !!user) {
121
+ hasProcessedCallbackRef.current = true;
122
+ const url = new URL(window.location.href);
123
+ url.search = "";
124
+ window.history.replaceState({}, "", url.toString());
125
+ setTimeout(() => {
126
+ window.location.href = afterSignInUrl;
127
+ }, 100);
128
+ }
129
+ }, [isLoaded, user, afterSignInUrl]);
112
130
  const getPublicAuthConfig = useCallback(async () => {
113
131
  try {
114
132
  const result = await insforge.auth.getPublicAuthConfig();
@@ -2367,9 +2385,9 @@ function useUser() {
2367
2385
  }
2368
2386
  function RedirectToAuth({ baseUrl, path }) {
2369
2387
  useEffect(() => {
2370
- const callbackUrl = `${window.location.origin}/auth/callback`;
2388
+ const currentUrl = window.location.href;
2371
2389
  const authUrl = new URL(path, baseUrl);
2372
- authUrl.searchParams.set("redirect", callbackUrl);
2390
+ authUrl.searchParams.set("redirect", currentUrl);
2373
2391
  window.location.replace(authUrl.toString());
2374
2392
  }, [baseUrl, path]);
2375
2393
  return null;