@logto/react 0.1.2 → 0.1.3
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/context.d.ts +2 -0
- package/lib/context.js +2 -0
- package/lib/hooks/index.d.ts +3 -2
- package/lib/hooks/index.js +30 -20
- package/lib/index.d.ts +2 -2
- package/lib/index.js +3 -5
- package/lib/provider.d.ts +4 -4
- package/lib/provider.js +6 -3
- package/package.json +8 -3
package/lib/context.d.ts
CHANGED
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
import LogtoClient from '@logto/browser';
|
|
3
3
|
export declare type LogtoContextProps = {
|
|
4
4
|
logtoClient?: LogtoClient;
|
|
5
|
+
isAuthenticated: boolean;
|
|
5
6
|
loadingCount: number;
|
|
7
|
+
setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;
|
|
6
8
|
setLoadingCount: React.Dispatch<React.SetStateAction<number>>;
|
|
7
9
|
};
|
|
8
10
|
export declare const throwContextError: () => never;
|
package/lib/context.js
CHANGED
|
@@ -8,6 +8,8 @@ const throwContextError = () => {
|
|
|
8
8
|
exports.throwContextError = throwContextError;
|
|
9
9
|
exports.LogtoContext = (0, react_1.createContext)({
|
|
10
10
|
logtoClient: undefined,
|
|
11
|
+
isAuthenticated: false,
|
|
11
12
|
loadingCount: 0,
|
|
13
|
+
setIsAuthenticated: exports.throwContextError,
|
|
12
14
|
setLoadingCount: exports.throwContextError,
|
|
13
15
|
});
|
package/lib/hooks/index.d.ts
CHANGED
|
@@ -9,5 +9,6 @@ declare type Logto = {
|
|
|
9
9
|
signIn: (redirectUri: string) => Promise<void>;
|
|
10
10
|
signOut: (postLogoutRedirectUri: string) => Promise<void>;
|
|
11
11
|
};
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
declare const useHandleSignInCallback: () => void;
|
|
13
|
+
declare const useLogto: () => Logto;
|
|
14
|
+
export { useLogto, useHandleSignInCallback };
|
package/lib/hooks/index.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useHandleSignInCallback = exports.useLogto = void 0;
|
|
3
4
|
const react_1 = require("react");
|
|
4
5
|
const context_1 = require("../context");
|
|
5
|
-
|
|
6
|
-
const {
|
|
7
|
-
const [isAuthenticated, setIsAuthenticated] = (0, react_1.useState)(logtoClient?.isAuthenticated ?? false);
|
|
8
|
-
const isLoading = loadingCount > 0;
|
|
6
|
+
const useLoadingState = () => {
|
|
7
|
+
const { setLoadingCount } = (0, react_1.useContext)(context_1.LogtoContext);
|
|
9
8
|
const setLoadingState = (0, react_1.useCallback)((state) => {
|
|
10
9
|
if (state) {
|
|
11
10
|
setLoadingCount((count) => count + 1);
|
|
@@ -14,22 +13,38 @@ function useLogto() {
|
|
|
14
13
|
setLoadingCount((count) => Math.max(0, count - 1));
|
|
15
14
|
}
|
|
16
15
|
}, [setLoadingCount]);
|
|
17
|
-
|
|
16
|
+
return setLoadingState;
|
|
17
|
+
};
|
|
18
|
+
const useHandleSignInCallback = () => {
|
|
19
|
+
const { logtoClient, isAuthenticated, setIsAuthenticated } = (0, react_1.useContext)(context_1.LogtoContext);
|
|
20
|
+
const setLoadingState = useLoadingState();
|
|
21
|
+
const handleSignInCallback = (0, react_1.useCallback)(async (callbackUri) => {
|
|
18
22
|
if (!logtoClient) {
|
|
19
23
|
return (0, context_1.throwContextError)();
|
|
20
24
|
}
|
|
21
25
|
setLoadingState(true);
|
|
22
|
-
await logtoClient.
|
|
26
|
+
await logtoClient.handleSignInCallback(callbackUri);
|
|
23
27
|
setLoadingState(false);
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
setIsAuthenticated(true);
|
|
29
|
+
}, [logtoClient, setIsAuthenticated, setLoadingState]);
|
|
30
|
+
(0, react_1.useEffect)(() => {
|
|
31
|
+
if (!isAuthenticated && logtoClient?.isSignInRedirected(window.location.href)) {
|
|
32
|
+
void handleSignInCallback(window.location.href);
|
|
33
|
+
}
|
|
34
|
+
}, [handleSignInCallback, isAuthenticated, logtoClient]);
|
|
35
|
+
};
|
|
36
|
+
exports.useHandleSignInCallback = useHandleSignInCallback;
|
|
37
|
+
const useLogto = () => {
|
|
38
|
+
const { logtoClient, loadingCount, isAuthenticated, setIsAuthenticated } = (0, react_1.useContext)(context_1.LogtoContext);
|
|
39
|
+
const setLoadingState = useLoadingState();
|
|
40
|
+
const isLoading = loadingCount > 0;
|
|
41
|
+
const signIn = (0, react_1.useCallback)(async (redirectUri) => {
|
|
26
42
|
if (!logtoClient) {
|
|
27
43
|
return (0, context_1.throwContextError)();
|
|
28
44
|
}
|
|
29
45
|
setLoadingState(true);
|
|
30
|
-
await logtoClient.
|
|
46
|
+
await logtoClient.signIn(redirectUri);
|
|
31
47
|
setLoadingState(false);
|
|
32
|
-
setIsAuthenticated(true);
|
|
33
48
|
}, [logtoClient, setLoadingState]);
|
|
34
49
|
const signOut = (0, react_1.useCallback)(async (postLogoutRedirectUri) => {
|
|
35
50
|
if (!logtoClient) {
|
|
@@ -39,7 +54,7 @@ function useLogto() {
|
|
|
39
54
|
await logtoClient.signOut(postLogoutRedirectUri);
|
|
40
55
|
setLoadingState(false);
|
|
41
56
|
setIsAuthenticated(false);
|
|
42
|
-
}, [logtoClient, setLoadingState]);
|
|
57
|
+
}, [logtoClient, setIsAuthenticated, setLoadingState]);
|
|
43
58
|
const fetchUserInfo = (0, react_1.useCallback)(async () => {
|
|
44
59
|
if (!logtoClient) {
|
|
45
60
|
return (0, context_1.throwContextError)();
|
|
@@ -49,12 +64,12 @@ function useLogto() {
|
|
|
49
64
|
setLoadingState(false);
|
|
50
65
|
return userInfo;
|
|
51
66
|
}, [logtoClient, setLoadingState]);
|
|
52
|
-
const getAccessToken = (0, react_1.useCallback)(async () => {
|
|
67
|
+
const getAccessToken = (0, react_1.useCallback)(async (resource) => {
|
|
53
68
|
if (!logtoClient) {
|
|
54
69
|
return (0, context_1.throwContextError)();
|
|
55
70
|
}
|
|
56
71
|
setLoadingState(true);
|
|
57
|
-
const accessToken = await logtoClient.getAccessToken();
|
|
72
|
+
const accessToken = await logtoClient.getAccessToken(resource);
|
|
58
73
|
setLoadingState(false);
|
|
59
74
|
return accessToken;
|
|
60
75
|
}, [logtoClient, setLoadingState]);
|
|
@@ -64,11 +79,6 @@ function useLogto() {
|
|
|
64
79
|
}
|
|
65
80
|
return logtoClient.getIdTokenClaims();
|
|
66
81
|
}, [logtoClient]);
|
|
67
|
-
(0, react_1.useEffect)(() => {
|
|
68
|
-
if (!isAuthenticated && logtoClient?.isSignInRedirected(window.location.href)) {
|
|
69
|
-
void handleSignInCallback(window.location.href);
|
|
70
|
-
}
|
|
71
|
-
}, [handleSignInCallback, isAuthenticated, logtoClient]);
|
|
72
82
|
if (!logtoClient) {
|
|
73
83
|
return (0, context_1.throwContextError)();
|
|
74
84
|
}
|
|
@@ -81,5 +91,5 @@ function useLogto() {
|
|
|
81
91
|
getAccessToken,
|
|
82
92
|
getIdTokenClaims,
|
|
83
93
|
};
|
|
84
|
-
}
|
|
85
|
-
exports.
|
|
94
|
+
};
|
|
95
|
+
exports.useLogto = useLogto;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export type { LogtoContextProps } from './context';
|
|
2
|
-
export type { IdTokenClaims, UserInfoResponse } from '@logto/browser';
|
|
2
|
+
export type { LogtoConfig, IdTokenClaims, UserInfoResponse } from '@logto/browser';
|
|
3
3
|
export * from './provider';
|
|
4
|
-
export {
|
|
4
|
+
export { useLogto, useHandleSignInCallback } from './hooks';
|
package/lib/index.js
CHANGED
|
@@ -13,11 +13,9 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
13
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
-
};
|
|
19
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.useLogto = void 0;
|
|
17
|
+
exports.useHandleSignInCallback = exports.useLogto = void 0;
|
|
21
18
|
__exportStar(require("./provider"), exports);
|
|
22
19
|
var hooks_1 = require("./hooks");
|
|
23
|
-
Object.defineProperty(exports, "useLogto", { enumerable: true, get: function () { return
|
|
20
|
+
Object.defineProperty(exports, "useLogto", { enumerable: true, get: function () { return hooks_1.useLogto; } });
|
|
21
|
+
Object.defineProperty(exports, "useHandleSignInCallback", { enumerable: true, get: function () { return hooks_1.useHandleSignInCallback; } });
|
package/lib/provider.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { LogtoConfig } from '@logto/browser';
|
|
2
|
-
import
|
|
2
|
+
import { ReactNode } from 'react';
|
|
3
3
|
export declare type LogtoProviderProps = {
|
|
4
|
-
|
|
5
|
-
children?:
|
|
4
|
+
config: LogtoConfig;
|
|
5
|
+
children?: ReactNode;
|
|
6
6
|
};
|
|
7
|
-
export declare const LogtoProvider: ({
|
|
7
|
+
export declare const LogtoProvider: ({ config, children }: LogtoProviderProps) => JSX.Element;
|
package/lib/provider.js
CHANGED
|
@@ -30,14 +30,17 @@ exports.LogtoProvider = void 0;
|
|
|
30
30
|
const browser_1 = __importDefault(require("@logto/browser"));
|
|
31
31
|
const react_1 = __importStar(require("react"));
|
|
32
32
|
const context_1 = require("./context");
|
|
33
|
-
const LogtoProvider = ({
|
|
33
|
+
const LogtoProvider = ({ config, children }) => {
|
|
34
34
|
const [loadingCount, setLoadingCount] = (0, react_1.useState)(0);
|
|
35
|
-
const memorizedLogtoClient = (0, react_1.useMemo)(() => ({ logtoClient: new browser_1.default(
|
|
35
|
+
const memorizedLogtoClient = (0, react_1.useMemo)(() => ({ logtoClient: new browser_1.default(config) }), [config]);
|
|
36
|
+
const [isAuthenticated, setIsAuthenticated] = (0, react_1.useState)(memorizedLogtoClient.logtoClient.isAuthenticated);
|
|
36
37
|
const memorizedContextValue = (0, react_1.useMemo)(() => ({
|
|
37
38
|
...memorizedLogtoClient,
|
|
39
|
+
isAuthenticated,
|
|
40
|
+
setIsAuthenticated,
|
|
38
41
|
loadingCount,
|
|
39
42
|
setLoadingCount,
|
|
40
|
-
}), [memorizedLogtoClient, loadingCount]);
|
|
43
|
+
}), [memorizedLogtoClient, isAuthenticated, loadingCount]);
|
|
41
44
|
return react_1.default.createElement(context_1.LogtoContext.Provider, { value: memorizedContextValue }, children);
|
|
42
45
|
};
|
|
43
46
|
exports.LogtoProvider = LogtoProvider;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/react",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"main": "./lib/index.js",
|
|
5
5
|
"exports": "./lib/index.js",
|
|
6
6
|
"typings": "./lib/index.d.ts",
|
|
@@ -8,6 +8,11 @@
|
|
|
8
8
|
"lib"
|
|
9
9
|
],
|
|
10
10
|
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/logto-io/js.git",
|
|
14
|
+
"directory": "packages/react"
|
|
15
|
+
},
|
|
11
16
|
"scripts": {
|
|
12
17
|
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
13
18
|
"preinstall": "npx only-allow pnpm",
|
|
@@ -19,7 +24,7 @@
|
|
|
19
24
|
"prepack": "pnpm test && pnpm build"
|
|
20
25
|
},
|
|
21
26
|
"dependencies": {
|
|
22
|
-
"@logto/browser": "^0.1.
|
|
27
|
+
"@logto/browser": "^0.1.3",
|
|
23
28
|
"@silverhand/essentials": "^1.1.6"
|
|
24
29
|
},
|
|
25
30
|
"devDependencies": {
|
|
@@ -50,5 +55,5 @@
|
|
|
50
55
|
"publishConfig": {
|
|
51
56
|
"access": "public"
|
|
52
57
|
},
|
|
53
|
-
"gitHead": "
|
|
58
|
+
"gitHead": "2a59d35046744dd1284eb81804cf0ef9a35f41e6"
|
|
54
59
|
}
|