@logto/react 0.1.12 → 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.
- package/lib/hooks/index.d.ts +1 -1
- package/lib/hooks/index.js +12 -6
- package/package.json +4 -4
package/lib/hooks/index.d.ts
CHANGED
|
@@ -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: (
|
|
12
|
+
declare const useHandleSignInCallback: (callback?: (() => void) | undefined) => {
|
|
13
13
|
isLoading: boolean;
|
|
14
14
|
isAuthenticated: boolean;
|
|
15
15
|
error: Error | undefined;
|
package/lib/hooks/index.js
CHANGED
|
@@ -29,10 +29,15 @@ const useErrorHandler = () => {
|
|
|
29
29
|
}, [setError]);
|
|
30
30
|
return { handleError };
|
|
31
31
|
};
|
|
32
|
-
const useHandleSignInCallback = (
|
|
32
|
+
const useHandleSignInCallback = (callback) => {
|
|
33
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)();
|
|
@@ -41,7 +46,7 @@ const useHandleSignInCallback = (returnToPageUrl = window.location.origin) => {
|
|
|
41
46
|
setLoadingState(true);
|
|
42
47
|
await logtoClient.handleSignInCallback(callbackUri);
|
|
43
48
|
setIsAuthenticated(true);
|
|
44
|
-
|
|
49
|
+
callbackRef.current?.();
|
|
45
50
|
}
|
|
46
51
|
catch (error) {
|
|
47
52
|
handleError(error, 'Unexpected error occurred while handling sign in callback.');
|
|
@@ -49,10 +54,11 @@ const useHandleSignInCallback = (returnToPageUrl = window.location.origin) => {
|
|
|
49
54
|
finally {
|
|
50
55
|
setLoadingState(false);
|
|
51
56
|
}
|
|
52
|
-
}, [logtoClient,
|
|
57
|
+
}, [logtoClient, setLoadingState, setIsAuthenticated, handleError]);
|
|
53
58
|
(0, react_1.useEffect)(() => {
|
|
54
|
-
|
|
55
|
-
|
|
59
|
+
const currentPageUrl = window.location.href;
|
|
60
|
+
if (!isAuthenticated && logtoClient?.isSignInRedirected(currentPageUrl)) {
|
|
61
|
+
void handleSignInCallback(currentPageUrl);
|
|
56
62
|
}
|
|
57
63
|
}, [handleSignInCallback, isAuthenticated, logtoClient]);
|
|
58
64
|
return {
|
|
@@ -91,7 +97,7 @@ const useLogto = () => {
|
|
|
91
97
|
await logtoClient.signOut(postLogoutRedirectUri);
|
|
92
98
|
// We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately
|
|
93
99
|
// even before navigating to the oidc end session endpoint, which might cause rendering problems.
|
|
94
|
-
//
|
|
100
|
+
// Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
|
|
95
101
|
}
|
|
96
102
|
catch (error) {
|
|
97
103
|
handleError(error, 'Unexpected error occurred while signing out.');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.15",
|
|
4
4
|
"main": "./lib/index.js",
|
|
5
5
|
"exports": "./lib/index.js",
|
|
6
6
|
"typings": "./lib/index.d.ts",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"prepack": "pnpm test"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@logto/browser": "^0.1.
|
|
27
|
+
"@logto/browser": "^0.1.14",
|
|
28
28
|
"@silverhand/essentials": "^1.1.6"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@types/react": "^17.0.39",
|
|
39
39
|
"eslint": "^8.9.0",
|
|
40
40
|
"jest": "^27.5.1",
|
|
41
|
-
"lint-staged": "^
|
|
41
|
+
"lint-staged": "^13.0.0",
|
|
42
42
|
"postcss": "^8.4.6",
|
|
43
43
|
"prettier": "^2.5.1",
|
|
44
44
|
"react": "^17.0.2",
|
|
@@ -56,5 +56,5 @@
|
|
|
56
56
|
"publishConfig": {
|
|
57
57
|
"access": "public"
|
|
58
58
|
},
|
|
59
|
-
"gitHead": "
|
|
59
|
+
"gitHead": "51665f47934ef390136ee8ff53393f20f9fb8221"
|
|
60
60
|
}
|