@logto/react 0.1.14 → 0.1.15

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.
@@ -9,7 +9,7 @@ declare type Logto = {
9
9
  signIn: (redirectUri: string) => Promise<void>;
10
10
  signOut: (postLogoutRedirectUri: string) => Promise<void>;
11
11
  };
12
- declare const useHandleSignInCallback: (returnToPageUrl?: string) => {
12
+ declare const useHandleSignInCallback: (callback?: (() => void) | undefined) => {
13
13
  isLoading: boolean;
14
14
  isAuthenticated: boolean;
15
15
  error: Error | undefined;
@@ -29,10 +29,15 @@ const useErrorHandler = () => {
29
29
  }, [setError]);
30
30
  return { handleError };
31
31
  };
32
- const useHandleSignInCallback = (returnToPageUrl = window.location.origin) => {
33
- const { logtoClient, isAuthenticated, error } = (0, react_1.useContext)(context_1.LogtoContext);
32
+ const useHandleSignInCallback = (callback) => {
33
+ const { logtoClient, isAuthenticated, error, setIsAuthenticated } = (0, react_1.useContext)(context_1.LogtoContext);
34
34
  const { isLoading, setLoadingState } = useLoadingState();
35
35
  const { handleError } = useErrorHandler();
36
+ const callbackRef = (0, react_1.useRef)();
37
+ (0, react_1.useEffect)(() => {
38
+ // eslint-disable-next-line @silverhand/fp/no-mutation
39
+ callbackRef.current = callback; // Update ref to the latest callback.
40
+ }, [callback]);
36
41
  const handleSignInCallback = (0, react_1.useCallback)(async (callbackUri) => {
37
42
  if (!logtoClient) {
38
43
  return (0, context_1.throwContextError)();
@@ -40,10 +45,8 @@ const useHandleSignInCallback = (returnToPageUrl = window.location.origin) => {
40
45
  try {
41
46
  setLoadingState(true);
42
47
  await logtoClient.handleSignInCallback(callbackUri);
43
- // We deliberately do NOT set isAuthenticated to true here, because the app state may change immediately
44
- // even before navigating to the return page URL, which might cause rendering problems.
45
- // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
46
- window.location.assign(returnToPageUrl);
48
+ setIsAuthenticated(true);
49
+ callbackRef.current?.();
47
50
  }
48
51
  catch (error) {
49
52
  handleError(error, 'Unexpected error occurred while handling sign in callback.');
@@ -51,10 +54,11 @@ const useHandleSignInCallback = (returnToPageUrl = window.location.origin) => {
51
54
  finally {
52
55
  setLoadingState(false);
53
56
  }
54
- }, [logtoClient, returnToPageUrl, setLoadingState, handleError]);
57
+ }, [logtoClient, setLoadingState, setIsAuthenticated, handleError]);
55
58
  (0, react_1.useEffect)(() => {
56
- if (!isAuthenticated && logtoClient?.isSignInRedirected(window.location.href)) {
57
- void handleSignInCallback(window.location.href);
59
+ const currentPageUrl = window.location.href;
60
+ if (!isAuthenticated && logtoClient?.isSignInRedirected(currentPageUrl)) {
61
+ void handleSignInCallback(currentPageUrl);
58
62
  }
59
63
  }, [handleSignInCallback, isAuthenticated, logtoClient]);
60
64
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/react",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "main": "./lib/index.js",
5
5
  "exports": "./lib/index.js",
6
6
  "typings": "./lib/index.d.ts",
@@ -56,5 +56,5 @@
56
56
  "publishConfig": {
57
57
  "access": "public"
58
58
  },
59
- "gitHead": "5d05c6bd23dc35d7217031edccc01db2bae33b7b"
59
+ "gitHead": "51665f47934ef390136ee8ff53393f20f9fb8221"
60
60
  }