@logto/react 3.0.16 → 4.0.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/provider.d.ts CHANGED
@@ -10,4 +10,4 @@ export type LogtoProviderProps = {
10
10
  LogtoClientClass?: typeof LogtoClient;
11
11
  children?: ReactNode;
12
12
  };
13
- export declare const LogtoProvider: ({ config, LogtoClientClass, children, unstable_enableCache, }: LogtoProviderProps) => import("react/jsx-runtime.js").JSX.Element;
13
+ export declare const LogtoProvider: ({ config, LogtoClientClass, children, unstable_enableCache, }: LogtoProviderProps) => import("react/jsx-runtime").JSX.Element;
package/package.json CHANGED
@@ -1,13 +1,11 @@
1
1
  {
2
2
  "name": "@logto/react",
3
- "version": "3.0.16",
3
+ "version": "4.0.3",
4
4
  "type": "module",
5
- "main": "./lib/index.cjs",
6
5
  "module": "./lib/index.js",
7
6
  "types": "./lib/index.d.ts",
8
7
  "exports": {
9
8
  "types": "./lib/index.d.ts",
10
- "require": "./lib/index.cjs",
11
9
  "import": "./lib/index.js",
12
10
  "default": "./lib/index.js"
13
11
  },
@@ -21,8 +19,8 @@
21
19
  "directory": "packages/react"
22
20
  },
23
21
  "dependencies": {
24
- "@silverhand/essentials": "^2.8.7",
25
- "@logto/browser": "^2.2.18"
22
+ "@silverhand/essentials": "^2.9.2",
23
+ "@logto/browser": "^3.0.3"
26
24
  },
27
25
  "devDependencies": {
28
26
  "@silverhand/eslint-config": "^6.0.1",
@@ -33,12 +31,12 @@
33
31
  "@types/react": "^18.2.56",
34
32
  "@vitest/coverage-v8": "^1.6.0",
35
33
  "eslint": "^8.57.0",
36
- "happy-dom": "^14.0.0",
34
+ "happy-dom": "^15.10.2",
37
35
  "lint-staged": "^15.0.0",
38
36
  "postcss": "^8.4.31",
39
37
  "prettier": "^3.0.0",
40
38
  "react": "^18.0.2",
41
- "stylelint": "^15.0.0",
39
+ "stylelint": "^16.0.0",
42
40
  "typescript": "^5.3.3",
43
41
  "vitest": "^1.6.0"
44
42
  },
package/lib/context.cjs DELETED
@@ -1,25 +0,0 @@
1
- 'use strict';
2
-
3
- var react = require('react');
4
-
5
- const throwContextError = () => {
6
- throw new Error('Must be used inside <LogtoProvider> context.');
7
- };
8
- /**
9
- * The context for the LogtoProvider.
10
- *
11
- * @remarks
12
- * Instead of using this context directly, in most cases you should use the `useLogto` hook.
13
- */
14
- const LogtoContext = react.createContext({
15
- logtoClient: undefined,
16
- isAuthenticated: false,
17
- isLoading: false,
18
- error: undefined,
19
- setIsAuthenticated: throwContextError,
20
- setIsLoading: throwContextError,
21
- setError: throwContextError,
22
- });
23
-
24
- exports.LogtoContext = LogtoContext;
25
- exports.throwContextError = throwContextError;
@@ -1,110 +0,0 @@
1
- 'use strict';
2
-
3
- var essentials = require('@silverhand/essentials');
4
- var react = require('react');
5
- var context = require('../context.cjs');
6
-
7
- const useErrorHandler = () => {
8
- const { setError } = react.useContext(context.LogtoContext);
9
- const handleError = react.useCallback((error, fallbackErrorMessage) => {
10
- if (error instanceof Error) {
11
- setError(error);
12
- }
13
- else if (fallbackErrorMessage) {
14
- setError(new Error(fallbackErrorMessage));
15
- }
16
- console.error(error);
17
- }, [setError]);
18
- return { handleError };
19
- };
20
- const useHandleSignInCallback = (callback) => {
21
- const { logtoClient, isAuthenticated, error, setIsAuthenticated, isLoading, setIsLoading } = react.useContext(context.LogtoContext);
22
- const { handleError } = useErrorHandler();
23
- const callbackRef = react.useRef();
24
- react.useEffect(() => {
25
- // eslint-disable-next-line @silverhand/fp/no-mutation
26
- callbackRef.current = callback; // Update ref to the latest callback.
27
- }, [callback]);
28
- react.useEffect(() => {
29
- if (!logtoClient || isLoading || error) {
30
- return;
31
- }
32
- (async () => {
33
- const currentPageUrl = window.location.href;
34
- const isRedirected = await logtoClient.isSignInRedirected(currentPageUrl);
35
- if (!isAuthenticated && isRedirected) {
36
- setIsLoading(true);
37
- await essentials.trySafe(async () => {
38
- await logtoClient.handleSignInCallback(currentPageUrl);
39
- setIsAuthenticated(true);
40
- callbackRef.current?.();
41
- }, (error) => {
42
- handleError(error, 'Unexpected error occurred while handling sign in callback.');
43
- });
44
- setIsLoading(false);
45
- }
46
- })();
47
- }, [
48
- error,
49
- handleError,
50
- isAuthenticated,
51
- isLoading,
52
- logtoClient,
53
- setIsAuthenticated,
54
- setIsLoading,
55
- ]);
56
- return {
57
- isLoading,
58
- isAuthenticated,
59
- error,
60
- };
61
- };
62
- const useLogto = () => {
63
- const { logtoClient, isAuthenticated, error, isLoading, setIsLoading } = react.useContext(context.LogtoContext);
64
- const { handleError } = useErrorHandler();
65
- const client = logtoClient ?? context.throwContextError();
66
- const proxy = react.useCallback((run, resetLoadingState = true) => {
67
- return async (...args) => {
68
- try {
69
- setIsLoading(true);
70
- return await run(...args);
71
- }
72
- catch (error) {
73
- handleError(error, `Unexpected error occurred while calling ${run.name}.`);
74
- }
75
- finally {
76
- if (resetLoadingState) {
77
- setIsLoading(false);
78
- }
79
- }
80
- };
81
- }, [setIsLoading, handleError]);
82
- const methods = react.useMemo(() => ({
83
- getRefreshToken: proxy(client.getRefreshToken.bind(client)),
84
- getAccessToken: proxy(client.getAccessToken.bind(client)),
85
- getAccessTokenClaims: proxy(client.getAccessTokenClaims.bind(client)),
86
- getOrganizationToken: proxy(client.getOrganizationToken.bind(client)),
87
- getOrganizationTokenClaims: proxy(client.getOrganizationTokenClaims.bind(client)),
88
- getIdToken: proxy(client.getIdToken.bind(client)),
89
- getIdTokenClaims: proxy(client.getIdTokenClaims.bind(client)),
90
- // eslint-disable-next-line no-restricted-syntax -- TypeScript cannot infer the correct type.
91
- signIn: proxy(client.signIn.bind(client), false),
92
- // We deliberately do NOT set isAuthenticated to false in the function below, because the app state
93
- // may change immediately even before navigating to the oidc end session endpoint, which might cause
94
- // rendering problems.
95
- // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
96
- signOut: proxy(client.signOut.bind(client)),
97
- fetchUserInfo: proxy(client.fetchUserInfo.bind(client)),
98
- clearAccessToken: proxy(client.clearAccessToken.bind(client)),
99
- clearAllTokens: proxy(client.clearAllTokens.bind(client)),
100
- }), [client, proxy]);
101
- return {
102
- isAuthenticated,
103
- isLoading,
104
- error,
105
- ...methods,
106
- };
107
- };
108
-
109
- exports.useHandleSignInCallback = useHandleSignInCallback;
110
- exports.useLogto = useLogto;
package/lib/index.cjs DELETED
@@ -1,59 +0,0 @@
1
- 'use strict';
2
-
3
- var LogtoClient = require('@logto/browser');
4
- var provider = require('./provider.cjs');
5
- var index = require('./hooks/index.cjs');
6
-
7
-
8
-
9
- Object.defineProperty(exports, "LogtoClientError", {
10
- enumerable: true,
11
- get: function () { return LogtoClient.LogtoClientError; }
12
- });
13
- Object.defineProperty(exports, "LogtoError", {
14
- enumerable: true,
15
- get: function () { return LogtoClient.LogtoError; }
16
- });
17
- Object.defineProperty(exports, "LogtoRequestError", {
18
- enumerable: true,
19
- get: function () { return LogtoClient.LogtoRequestError; }
20
- });
21
- Object.defineProperty(exports, "OidcError", {
22
- enumerable: true,
23
- get: function () { return LogtoClient.OidcError; }
24
- });
25
- Object.defineProperty(exports, "PersistKey", {
26
- enumerable: true,
27
- get: function () { return LogtoClient.PersistKey; }
28
- });
29
- Object.defineProperty(exports, "Prompt", {
30
- enumerable: true,
31
- get: function () { return LogtoClient.Prompt; }
32
- });
33
- Object.defineProperty(exports, "ReservedResource", {
34
- enumerable: true,
35
- get: function () { return LogtoClient.ReservedResource; }
36
- });
37
- Object.defineProperty(exports, "ReservedScope", {
38
- enumerable: true,
39
- get: function () { return LogtoClient.ReservedScope; }
40
- });
41
- Object.defineProperty(exports, "UserScope", {
42
- enumerable: true,
43
- get: function () { return LogtoClient.UserScope; }
44
- });
45
- Object.defineProperty(exports, "buildOrganizationUrn", {
46
- enumerable: true,
47
- get: function () { return LogtoClient.buildOrganizationUrn; }
48
- });
49
- Object.defineProperty(exports, "getOrganizationIdFromUrn", {
50
- enumerable: true,
51
- get: function () { return LogtoClient.getOrganizationIdFromUrn; }
52
- });
53
- Object.defineProperty(exports, "organizationUrnPrefix", {
54
- enumerable: true,
55
- get: function () { return LogtoClient.organizationUrnPrefix; }
56
- });
57
- exports.LogtoProvider = provider.LogtoProvider;
58
- exports.useHandleSignInCallback = index.useHandleSignInCallback;
59
- exports.useLogto = index.useLogto;
package/lib/provider.cjs DELETED
@@ -1,45 +0,0 @@
1
- 'use strict';
2
-
3
- var jsxRuntime = require('react/jsx-runtime');
4
- var LogtoClient = require('@logto/browser');
5
- var react = require('react');
6
- var context = require('./context.cjs');
7
-
8
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
-
10
- var LogtoClient__default = /*#__PURE__*/_interopDefault(LogtoClient);
11
-
12
- const LogtoProvider = ({ config, LogtoClientClass = LogtoClient__default.default, children, unstable_enableCache = false, }) => {
13
- const [loadingCount, setLoadingCount] = react.useState(1);
14
- const memorizedLogtoClient = react.useMemo(() => ({ logtoClient: new LogtoClientClass(config, unstable_enableCache) }), [LogtoClientClass, config, unstable_enableCache]);
15
- const [isAuthenticated, setIsAuthenticated] = react.useState(false);
16
- const [error, setError] = react.useState();
17
- const isLoading = react.useMemo(() => loadingCount > 0, [loadingCount]);
18
- const setIsLoading = react.useCallback((state) => {
19
- if (state) {
20
- setLoadingCount((count) => count + 1);
21
- }
22
- else {
23
- setLoadingCount((count) => Math.max(0, count - 1));
24
- }
25
- }, [setLoadingCount]);
26
- react.useEffect(() => {
27
- (async () => {
28
- const isAuthenticated = await memorizedLogtoClient.logtoClient.isAuthenticated();
29
- setIsAuthenticated(isAuthenticated);
30
- setLoadingCount((count) => Math.max(0, count - 1));
31
- })();
32
- }, [memorizedLogtoClient]);
33
- const memorizedContextValue = react.useMemo(() => ({
34
- ...memorizedLogtoClient,
35
- isAuthenticated,
36
- setIsAuthenticated,
37
- isLoading,
38
- setIsLoading,
39
- error,
40
- setError,
41
- }), [memorizedLogtoClient, isAuthenticated, isLoading, setIsLoading, error]);
42
- return jsxRuntime.jsx(context.LogtoContext.Provider, { value: memorizedContextValue, children: children });
43
- };
44
-
45
- exports.LogtoProvider = LogtoProvider;