@logto/react 1.1.1 → 2.0.0
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.cjs +19 -0
- package/lib/context.d.ts +13 -0
- package/lib/context.js +16 -0
- package/lib/hooks/index.cjs +162 -0
- package/lib/hooks/index.d.ts +18 -0
- package/lib/hooks/index.js +159 -0
- package/lib/hooks/index.test.d.ts +1 -0
- package/lib/index.cjs +35 -0
- package/lib/index.d.ts +3 -34
- package/lib/index.js +3 -262
- package/lib/provider.cjs +36 -0
- package/lib/provider.d.ts +7 -0
- package/lib/provider.js +30 -0
- package/package.json +32 -36
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/module.d.mts +0 -36
- package/lib/module.mjs +0 -234
- package/lib/module.mjs.map +0 -1
package/lib/context.cjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
const LogtoContext = react.createContext({
|
|
9
|
+
logtoClient: undefined,
|
|
10
|
+
isAuthenticated: false,
|
|
11
|
+
loadingCount: 0,
|
|
12
|
+
error: undefined,
|
|
13
|
+
setIsAuthenticated: throwContextError,
|
|
14
|
+
setLoadingCount: throwContextError,
|
|
15
|
+
setError: throwContextError,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
exports.LogtoContext = LogtoContext;
|
|
19
|
+
exports.throwContextError = throwContextError;
|
package/lib/context.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type LogtoClient from '@logto/browser';
|
|
3
|
+
export type LogtoContextProps = {
|
|
4
|
+
logtoClient?: LogtoClient;
|
|
5
|
+
isAuthenticated: boolean;
|
|
6
|
+
loadingCount: number;
|
|
7
|
+
error?: Error;
|
|
8
|
+
setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;
|
|
9
|
+
setLoadingCount: React.Dispatch<React.SetStateAction<number>>;
|
|
10
|
+
setError: React.Dispatch<React.SetStateAction<Error | undefined>>;
|
|
11
|
+
};
|
|
12
|
+
export declare const throwContextError: () => never;
|
|
13
|
+
export declare const LogtoContext: import("react").Context<LogtoContextProps>;
|
package/lib/context.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createContext } from 'react';
|
|
2
|
+
|
|
3
|
+
const throwContextError = () => {
|
|
4
|
+
throw new Error('Must be used inside <LogtoProvider> context.');
|
|
5
|
+
};
|
|
6
|
+
const LogtoContext = createContext({
|
|
7
|
+
logtoClient: undefined,
|
|
8
|
+
isAuthenticated: false,
|
|
9
|
+
loadingCount: 0,
|
|
10
|
+
error: undefined,
|
|
11
|
+
setIsAuthenticated: throwContextError,
|
|
12
|
+
setLoadingCount: throwContextError,
|
|
13
|
+
setError: throwContextError,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export { LogtoContext, throwContextError };
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
var context = require('../context.cjs');
|
|
5
|
+
|
|
6
|
+
const useLoadingState = () => {
|
|
7
|
+
const { loadingCount, setLoadingCount } = react.useContext(context.LogtoContext);
|
|
8
|
+
const isLoading = loadingCount > 0;
|
|
9
|
+
const setLoadingState = react.useCallback((state) => {
|
|
10
|
+
if (state) {
|
|
11
|
+
setLoadingCount((count) => count + 1);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
setLoadingCount((count) => Math.max(0, count - 1));
|
|
15
|
+
}
|
|
16
|
+
}, [setLoadingCount]);
|
|
17
|
+
return { isLoading, setLoadingState };
|
|
18
|
+
};
|
|
19
|
+
const useErrorHandler = () => {
|
|
20
|
+
const { setError } = react.useContext(context.LogtoContext);
|
|
21
|
+
const handleError = react.useCallback((error, fallbackErrorMessage) => {
|
|
22
|
+
if (error instanceof Error) {
|
|
23
|
+
setError(error);
|
|
24
|
+
}
|
|
25
|
+
else if (fallbackErrorMessage) {
|
|
26
|
+
setError(new Error(fallbackErrorMessage));
|
|
27
|
+
}
|
|
28
|
+
console.error(error);
|
|
29
|
+
}, [setError]);
|
|
30
|
+
return { handleError };
|
|
31
|
+
};
|
|
32
|
+
const useHandleSignInCallback = (callback) => {
|
|
33
|
+
const { logtoClient, isAuthenticated, error, setIsAuthenticated } = react.useContext(context.LogtoContext);
|
|
34
|
+
const { isLoading, setLoadingState } = useLoadingState();
|
|
35
|
+
const { handleError } = useErrorHandler();
|
|
36
|
+
const callbackRef = react.useRef();
|
|
37
|
+
react.useEffect(() => {
|
|
38
|
+
// eslint-disable-next-line @silverhand/fp/no-mutation
|
|
39
|
+
callbackRef.current = callback; // Update ref to the latest callback.
|
|
40
|
+
}, [callback]);
|
|
41
|
+
const handleSignInCallback = react.useCallback(async (callbackUri) => {
|
|
42
|
+
if (!logtoClient) {
|
|
43
|
+
return context.throwContextError();
|
|
44
|
+
}
|
|
45
|
+
try {
|
|
46
|
+
setLoadingState(true);
|
|
47
|
+
await logtoClient.handleSignInCallback(callbackUri);
|
|
48
|
+
setIsAuthenticated(true);
|
|
49
|
+
callbackRef.current?.();
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
handleError(error, 'Unexpected error occurred while handling sign in callback.');
|
|
53
|
+
}
|
|
54
|
+
finally {
|
|
55
|
+
setLoadingState(false);
|
|
56
|
+
}
|
|
57
|
+
}, [logtoClient, setLoadingState, setIsAuthenticated, handleError]);
|
|
58
|
+
react.useEffect(() => {
|
|
59
|
+
const currentPageUrl = window.location.href;
|
|
60
|
+
if (!isAuthenticated && logtoClient?.isSignInRedirected(currentPageUrl) && !isLoading) {
|
|
61
|
+
void handleSignInCallback(currentPageUrl);
|
|
62
|
+
}
|
|
63
|
+
}, [handleSignInCallback, isAuthenticated, isLoading, logtoClient]);
|
|
64
|
+
return {
|
|
65
|
+
isLoading,
|
|
66
|
+
isAuthenticated,
|
|
67
|
+
error,
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
const useLogto = () => {
|
|
71
|
+
const { logtoClient, loadingCount, isAuthenticated, error } = react.useContext(context.LogtoContext);
|
|
72
|
+
const { setLoadingState } = useLoadingState();
|
|
73
|
+
const { handleError } = useErrorHandler();
|
|
74
|
+
const isLoading = loadingCount > 0;
|
|
75
|
+
const signIn = react.useCallback(async (redirectUri, interactionMode) => {
|
|
76
|
+
if (!logtoClient) {
|
|
77
|
+
return context.throwContextError();
|
|
78
|
+
}
|
|
79
|
+
try {
|
|
80
|
+
setLoadingState(true);
|
|
81
|
+
await logtoClient.signIn(redirectUri, interactionMode);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
handleError(error, 'Unexpected error occurred while signing in.');
|
|
85
|
+
}
|
|
86
|
+
}, [logtoClient, setLoadingState, handleError]);
|
|
87
|
+
const signOut = react.useCallback(async (postLogoutRedirectUri) => {
|
|
88
|
+
if (!logtoClient) {
|
|
89
|
+
return context.throwContextError();
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
setLoadingState(true);
|
|
93
|
+
await logtoClient.signOut(postLogoutRedirectUri);
|
|
94
|
+
// We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately
|
|
95
|
+
// even before navigating to the oidc end session endpoint, which might cause rendering problems.
|
|
96
|
+
// Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
handleError(error, 'Unexpected error occurred while signing out.');
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
setLoadingState(false);
|
|
103
|
+
}
|
|
104
|
+
}, [logtoClient, setLoadingState, handleError]);
|
|
105
|
+
const fetchUserInfo = react.useCallback(async () => {
|
|
106
|
+
if (!logtoClient) {
|
|
107
|
+
return context.throwContextError();
|
|
108
|
+
}
|
|
109
|
+
try {
|
|
110
|
+
setLoadingState(true);
|
|
111
|
+
return await logtoClient.fetchUserInfo();
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
handleError(error, 'Unexpected error occurred while fetching user info.');
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
setLoadingState(false);
|
|
118
|
+
}
|
|
119
|
+
}, [logtoClient, setLoadingState, handleError]);
|
|
120
|
+
const getAccessToken = react.useCallback(async (resource) => {
|
|
121
|
+
if (!logtoClient) {
|
|
122
|
+
return context.throwContextError();
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
setLoadingState(true);
|
|
126
|
+
return await logtoClient.getAccessToken(resource);
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
handleError(error, 'Unexpected error occurred while getting access token.');
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
setLoadingState(false);
|
|
133
|
+
}
|
|
134
|
+
}, [logtoClient, setLoadingState, handleError]);
|
|
135
|
+
const getIdTokenClaims = react.useCallback(async () => {
|
|
136
|
+
if (!logtoClient) {
|
|
137
|
+
return context.throwContextError();
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
return await logtoClient.getIdTokenClaims();
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
// Do nothing if any exception occurs. Caller will get undefined value.
|
|
144
|
+
}
|
|
145
|
+
}, [logtoClient]);
|
|
146
|
+
if (!logtoClient) {
|
|
147
|
+
return context.throwContextError();
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
isAuthenticated,
|
|
151
|
+
isLoading,
|
|
152
|
+
error,
|
|
153
|
+
signIn,
|
|
154
|
+
signOut,
|
|
155
|
+
fetchUserInfo,
|
|
156
|
+
getAccessToken,
|
|
157
|
+
getIdTokenClaims,
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
exports.useHandleSignInCallback = useHandleSignInCallback;
|
|
162
|
+
exports.useLogto = useLogto;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type IdTokenClaims, type InteractionMode, type UserInfoResponse } from '@logto/browser';
|
|
2
|
+
type Logto = {
|
|
3
|
+
isAuthenticated: boolean;
|
|
4
|
+
isLoading: boolean;
|
|
5
|
+
error?: Error;
|
|
6
|
+
fetchUserInfo: () => Promise<UserInfoResponse | undefined>;
|
|
7
|
+
getAccessToken: (resource?: string) => Promise<string | undefined>;
|
|
8
|
+
getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;
|
|
9
|
+
signIn: (redirectUri: string, interactionMode?: InteractionMode) => Promise<void>;
|
|
10
|
+
signOut: (postLogoutRedirectUri?: string) => Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
declare const useHandleSignInCallback: (callback?: () => void) => {
|
|
13
|
+
isLoading: boolean;
|
|
14
|
+
isAuthenticated: boolean;
|
|
15
|
+
error: Error | undefined;
|
|
16
|
+
};
|
|
17
|
+
declare const useLogto: () => Logto;
|
|
18
|
+
export { useLogto, useHandleSignInCallback };
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { useContext, useRef, useEffect, useCallback } from 'react';
|
|
2
|
+
import { LogtoContext, throwContextError } from '../context.js';
|
|
3
|
+
|
|
4
|
+
const useLoadingState = () => {
|
|
5
|
+
const { loadingCount, setLoadingCount } = useContext(LogtoContext);
|
|
6
|
+
const isLoading = loadingCount > 0;
|
|
7
|
+
const setLoadingState = useCallback((state) => {
|
|
8
|
+
if (state) {
|
|
9
|
+
setLoadingCount((count) => count + 1);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
setLoadingCount((count) => Math.max(0, count - 1));
|
|
13
|
+
}
|
|
14
|
+
}, [setLoadingCount]);
|
|
15
|
+
return { isLoading, setLoadingState };
|
|
16
|
+
};
|
|
17
|
+
const useErrorHandler = () => {
|
|
18
|
+
const { setError } = useContext(LogtoContext);
|
|
19
|
+
const handleError = useCallback((error, fallbackErrorMessage) => {
|
|
20
|
+
if (error instanceof Error) {
|
|
21
|
+
setError(error);
|
|
22
|
+
}
|
|
23
|
+
else if (fallbackErrorMessage) {
|
|
24
|
+
setError(new Error(fallbackErrorMessage));
|
|
25
|
+
}
|
|
26
|
+
console.error(error);
|
|
27
|
+
}, [setError]);
|
|
28
|
+
return { handleError };
|
|
29
|
+
};
|
|
30
|
+
const useHandleSignInCallback = (callback) => {
|
|
31
|
+
const { logtoClient, isAuthenticated, error, setIsAuthenticated } = useContext(LogtoContext);
|
|
32
|
+
const { isLoading, setLoadingState } = useLoadingState();
|
|
33
|
+
const { handleError } = useErrorHandler();
|
|
34
|
+
const callbackRef = useRef();
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
// eslint-disable-next-line @silverhand/fp/no-mutation
|
|
37
|
+
callbackRef.current = callback; // Update ref to the latest callback.
|
|
38
|
+
}, [callback]);
|
|
39
|
+
const handleSignInCallback = useCallback(async (callbackUri) => {
|
|
40
|
+
if (!logtoClient) {
|
|
41
|
+
return throwContextError();
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
setLoadingState(true);
|
|
45
|
+
await logtoClient.handleSignInCallback(callbackUri);
|
|
46
|
+
setIsAuthenticated(true);
|
|
47
|
+
callbackRef.current?.();
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
handleError(error, 'Unexpected error occurred while handling sign in callback.');
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
setLoadingState(false);
|
|
54
|
+
}
|
|
55
|
+
}, [logtoClient, setLoadingState, setIsAuthenticated, handleError]);
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
const currentPageUrl = window.location.href;
|
|
58
|
+
if (!isAuthenticated && logtoClient?.isSignInRedirected(currentPageUrl) && !isLoading) {
|
|
59
|
+
void handleSignInCallback(currentPageUrl);
|
|
60
|
+
}
|
|
61
|
+
}, [handleSignInCallback, isAuthenticated, isLoading, logtoClient]);
|
|
62
|
+
return {
|
|
63
|
+
isLoading,
|
|
64
|
+
isAuthenticated,
|
|
65
|
+
error,
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
const useLogto = () => {
|
|
69
|
+
const { logtoClient, loadingCount, isAuthenticated, error } = useContext(LogtoContext);
|
|
70
|
+
const { setLoadingState } = useLoadingState();
|
|
71
|
+
const { handleError } = useErrorHandler();
|
|
72
|
+
const isLoading = loadingCount > 0;
|
|
73
|
+
const signIn = useCallback(async (redirectUri, interactionMode) => {
|
|
74
|
+
if (!logtoClient) {
|
|
75
|
+
return throwContextError();
|
|
76
|
+
}
|
|
77
|
+
try {
|
|
78
|
+
setLoadingState(true);
|
|
79
|
+
await logtoClient.signIn(redirectUri, interactionMode);
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
handleError(error, 'Unexpected error occurred while signing in.');
|
|
83
|
+
}
|
|
84
|
+
}, [logtoClient, setLoadingState, handleError]);
|
|
85
|
+
const signOut = useCallback(async (postLogoutRedirectUri) => {
|
|
86
|
+
if (!logtoClient) {
|
|
87
|
+
return throwContextError();
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
setLoadingState(true);
|
|
91
|
+
await logtoClient.signOut(postLogoutRedirectUri);
|
|
92
|
+
// We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately
|
|
93
|
+
// even before navigating to the oidc end session endpoint, which might cause rendering problems.
|
|
94
|
+
// Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
|
|
95
|
+
}
|
|
96
|
+
catch (error) {
|
|
97
|
+
handleError(error, 'Unexpected error occurred while signing out.');
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
setLoadingState(false);
|
|
101
|
+
}
|
|
102
|
+
}, [logtoClient, setLoadingState, handleError]);
|
|
103
|
+
const fetchUserInfo = useCallback(async () => {
|
|
104
|
+
if (!logtoClient) {
|
|
105
|
+
return throwContextError();
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
setLoadingState(true);
|
|
109
|
+
return await logtoClient.fetchUserInfo();
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
handleError(error, 'Unexpected error occurred while fetching user info.');
|
|
113
|
+
}
|
|
114
|
+
finally {
|
|
115
|
+
setLoadingState(false);
|
|
116
|
+
}
|
|
117
|
+
}, [logtoClient, setLoadingState, handleError]);
|
|
118
|
+
const getAccessToken = useCallback(async (resource) => {
|
|
119
|
+
if (!logtoClient) {
|
|
120
|
+
return throwContextError();
|
|
121
|
+
}
|
|
122
|
+
try {
|
|
123
|
+
setLoadingState(true);
|
|
124
|
+
return await logtoClient.getAccessToken(resource);
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
handleError(error, 'Unexpected error occurred while getting access token.');
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
setLoadingState(false);
|
|
131
|
+
}
|
|
132
|
+
}, [logtoClient, setLoadingState, handleError]);
|
|
133
|
+
const getIdTokenClaims = useCallback(async () => {
|
|
134
|
+
if (!logtoClient) {
|
|
135
|
+
return throwContextError();
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
return await logtoClient.getIdTokenClaims();
|
|
139
|
+
}
|
|
140
|
+
catch {
|
|
141
|
+
// Do nothing if any exception occurs. Caller will get undefined value.
|
|
142
|
+
}
|
|
143
|
+
}, [logtoClient]);
|
|
144
|
+
if (!logtoClient) {
|
|
145
|
+
return throwContextError();
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
isAuthenticated,
|
|
149
|
+
isLoading,
|
|
150
|
+
error,
|
|
151
|
+
signIn,
|
|
152
|
+
signOut,
|
|
153
|
+
fetchUserInfo,
|
|
154
|
+
getAccessToken,
|
|
155
|
+
getIdTokenClaims,
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export { useHandleSignInCallback, useLogto };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/lib/index.cjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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, 'OidcError', {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: function () { return LogtoClient.OidcError; }
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(exports, 'Prompt', {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function () { return LogtoClient.Prompt; }
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(exports, 'ReservedScope', {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () { return LogtoClient.ReservedScope; }
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(exports, 'UserScope', {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
get: function () { return LogtoClient.UserScope; }
|
|
32
|
+
});
|
|
33
|
+
exports.LogtoProvider = provider.LogtoProvider;
|
|
34
|
+
exports.useHandleSignInCallback = index.useHandleSignInCallback;
|
|
35
|
+
exports.useLogto = index.useLogto;
|
package/lib/index.d.ts
CHANGED
|
@@ -1,36 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
import { ReactNode } from "react";
|
|
3
|
-
export type LogtoContextProps = {
|
|
4
|
-
logtoClient?: LogtoClient;
|
|
5
|
-
isAuthenticated: boolean;
|
|
6
|
-
loadingCount: number;
|
|
7
|
-
error?: Error;
|
|
8
|
-
setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;
|
|
9
|
-
setLoadingCount: React.Dispatch<React.SetStateAction<number>>;
|
|
10
|
-
setError: React.Dispatch<React.SetStateAction<Error | undefined>>;
|
|
11
|
-
};
|
|
12
|
-
export type LogtoProviderProps = {
|
|
13
|
-
config: LogtoConfig;
|
|
14
|
-
children?: ReactNode;
|
|
15
|
-
};
|
|
16
|
-
export const LogtoProvider: ({ config, children }: LogtoProviderProps) => JSX.Element;
|
|
17
|
-
type Logto = {
|
|
18
|
-
isAuthenticated: boolean;
|
|
19
|
-
isLoading: boolean;
|
|
20
|
-
error?: Error;
|
|
21
|
-
fetchUserInfo: () => Promise<UserInfoResponse | undefined>;
|
|
22
|
-
getAccessToken: (resource?: string) => Promise<string | undefined>;
|
|
23
|
-
getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;
|
|
24
|
-
signIn: (redirectUri: string, interactionMode?: InteractionMode) => Promise<void>;
|
|
25
|
-
signOut: (postLogoutRedirectUri?: string) => Promise<void>;
|
|
26
|
-
};
|
|
27
|
-
export const useHandleSignInCallback: (callback?: () => void) => {
|
|
28
|
-
isLoading: boolean;
|
|
29
|
-
isAuthenticated: boolean;
|
|
30
|
-
error: Error | undefined;
|
|
31
|
-
};
|
|
32
|
-
export const useLogto: () => Logto;
|
|
1
|
+
export type { LogtoContextProps } from './context.js';
|
|
33
2
|
export type { LogtoConfig, IdTokenClaims, UserInfoResponse, LogtoErrorCode, LogtoClientErrorCode, InteractionMode, } from '@logto/browser';
|
|
34
3
|
export { LogtoError, LogtoClientError, OidcError, Prompt, ReservedScope, UserScope, } from '@logto/browser';
|
|
35
|
-
|
|
36
|
-
|
|
4
|
+
export * from './provider.js';
|
|
5
|
+
export { useLogto, useHandleSignInCallback } from './hooks/index.js';
|
package/lib/index.js
CHANGED
|
@@ -1,262 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function $parcel$exportWildcard(dest, source) {
|
|
6
|
-
Object.keys(source).forEach(function(key) {
|
|
7
|
-
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
|
|
8
|
-
return;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
Object.defineProperty(dest, key, {
|
|
12
|
-
enumerable: true,
|
|
13
|
-
get: function get() {
|
|
14
|
-
return source[key];
|
|
15
|
-
}
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
|
|
19
|
-
return dest;
|
|
20
|
-
}
|
|
21
|
-
function $parcel$export(e, n, v, s) {
|
|
22
|
-
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
23
|
-
}
|
|
24
|
-
function $parcel$interopDefault(a) {
|
|
25
|
-
return a && a.__esModule ? a.default : a;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
$parcel$export(module.exports, "LogtoError", () => $22652c0d956faacc$re_export$LogtoError);
|
|
29
|
-
$parcel$export(module.exports, "LogtoClientError", () => $22652c0d956faacc$re_export$LogtoClientError);
|
|
30
|
-
$parcel$export(module.exports, "OidcError", () => $22652c0d956faacc$re_export$OidcError);
|
|
31
|
-
$parcel$export(module.exports, "Prompt", () => $22652c0d956faacc$re_export$Prompt);
|
|
32
|
-
$parcel$export(module.exports, "ReservedScope", () => $22652c0d956faacc$re_export$ReservedScope);
|
|
33
|
-
$parcel$export(module.exports, "UserScope", () => $22652c0d956faacc$re_export$UserScope);
|
|
34
|
-
$parcel$export(module.exports, "useLogto", () => $ccb423956ca75d68$export$44fc9df4d2a1789a);
|
|
35
|
-
$parcel$export(module.exports, "useHandleSignInCallback", () => $ccb423956ca75d68$export$84e88c4b3c082374);
|
|
36
|
-
|
|
37
|
-
var $00c2be589188321f$exports = {};
|
|
38
|
-
|
|
39
|
-
$parcel$export($00c2be589188321f$exports, "LogtoProvider", () => $00c2be589188321f$export$bfa587c2c3245948);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const $52a461056f85891e$export$838ead842aa548e7 = ()=>{
|
|
45
|
-
throw new Error("Must be used inside <LogtoProvider> context.");
|
|
46
|
-
};
|
|
47
|
-
const $52a461056f85891e$export$e5bf247804b97da7 = /*#__PURE__*/ (0, $lYn3l$react.createContext)({
|
|
48
|
-
logtoClient: undefined,
|
|
49
|
-
isAuthenticated: false,
|
|
50
|
-
loadingCount: 0,
|
|
51
|
-
error: undefined,
|
|
52
|
-
setIsAuthenticated: $52a461056f85891e$export$838ead842aa548e7,
|
|
53
|
-
setLoadingCount: $52a461056f85891e$export$838ead842aa548e7,
|
|
54
|
-
setError: $52a461056f85891e$export$838ead842aa548e7
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const $00c2be589188321f$export$bfa587c2c3245948 = ({ config: config , children: children })=>{
|
|
59
|
-
const [loadingCount, setLoadingCount] = (0, $lYn3l$react.useState)(1);
|
|
60
|
-
const memorizedLogtoClient = (0, $lYn3l$react.useMemo)(()=>({
|
|
61
|
-
logtoClient: new (0, ($parcel$interopDefault($lYn3l$logtobrowser)))(config)
|
|
62
|
-
}), [
|
|
63
|
-
config
|
|
64
|
-
]);
|
|
65
|
-
const [isAuthenticated, setIsAuthenticated] = (0, $lYn3l$react.useState)(false);
|
|
66
|
-
const [error, setError] = (0, $lYn3l$react.useState)();
|
|
67
|
-
(0, $lYn3l$react.useEffect)(()=>{
|
|
68
|
-
(async ()=>{
|
|
69
|
-
const isAuthenticated = await memorizedLogtoClient.logtoClient.isAuthenticated();
|
|
70
|
-
setIsAuthenticated(isAuthenticated);
|
|
71
|
-
setLoadingCount((count)=>Math.max(0, count - 1));
|
|
72
|
-
})();
|
|
73
|
-
}, [
|
|
74
|
-
memorizedLogtoClient
|
|
75
|
-
]);
|
|
76
|
-
const memorizedContextValue = (0, $lYn3l$react.useMemo)(()=>({
|
|
77
|
-
...memorizedLogtoClient,
|
|
78
|
-
isAuthenticated: isAuthenticated,
|
|
79
|
-
setIsAuthenticated: setIsAuthenticated,
|
|
80
|
-
loadingCount: loadingCount,
|
|
81
|
-
setLoadingCount: setLoadingCount,
|
|
82
|
-
error: error,
|
|
83
|
-
setError: setError
|
|
84
|
-
}), [
|
|
85
|
-
memorizedLogtoClient,
|
|
86
|
-
isAuthenticated,
|
|
87
|
-
loadingCount,
|
|
88
|
-
error
|
|
89
|
-
]);
|
|
90
|
-
return /*#__PURE__*/ (0, $lYn3l$reactjsxruntime.jsx)((0, $52a461056f85891e$export$e5bf247804b97da7).Provider, {
|
|
91
|
-
value: memorizedContextValue,
|
|
92
|
-
children: children
|
|
93
|
-
});
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const $ccb423956ca75d68$var$useLoadingState = ()=>{
|
|
100
|
-
const { loadingCount: loadingCount , setLoadingCount: setLoadingCount } = (0, $lYn3l$react.useContext)((0, $52a461056f85891e$export$e5bf247804b97da7));
|
|
101
|
-
const isLoading = loadingCount > 0;
|
|
102
|
-
const setLoadingState = (0, $lYn3l$react.useCallback)((state)=>{
|
|
103
|
-
if (state) setLoadingCount((count)=>count + 1);
|
|
104
|
-
else setLoadingCount((count)=>Math.max(0, count - 1));
|
|
105
|
-
}, [
|
|
106
|
-
setLoadingCount
|
|
107
|
-
]);
|
|
108
|
-
return {
|
|
109
|
-
isLoading: isLoading,
|
|
110
|
-
setLoadingState: setLoadingState
|
|
111
|
-
};
|
|
112
|
-
};
|
|
113
|
-
const $ccb423956ca75d68$var$useErrorHandler = ()=>{
|
|
114
|
-
const { setError: setError } = (0, $lYn3l$react.useContext)((0, $52a461056f85891e$export$e5bf247804b97da7));
|
|
115
|
-
const handleError = (0, $lYn3l$react.useCallback)((error, fallbackErrorMessage)=>{
|
|
116
|
-
if (error instanceof Error) setError(error);
|
|
117
|
-
else if (fallbackErrorMessage) setError(new Error(fallbackErrorMessage));
|
|
118
|
-
console.error(error);
|
|
119
|
-
}, [
|
|
120
|
-
setError
|
|
121
|
-
]);
|
|
122
|
-
return {
|
|
123
|
-
handleError: handleError
|
|
124
|
-
};
|
|
125
|
-
};
|
|
126
|
-
const $ccb423956ca75d68$export$84e88c4b3c082374 = (callback)=>{
|
|
127
|
-
const { logtoClient: logtoClient , isAuthenticated: isAuthenticated , error: error , setIsAuthenticated: setIsAuthenticated } = (0, $lYn3l$react.useContext)((0, $52a461056f85891e$export$e5bf247804b97da7));
|
|
128
|
-
const { isLoading: isLoading , setLoadingState: setLoadingState } = $ccb423956ca75d68$var$useLoadingState();
|
|
129
|
-
const { handleError: handleError } = $ccb423956ca75d68$var$useErrorHandler();
|
|
130
|
-
const callbackRef = (0, $lYn3l$react.useRef)();
|
|
131
|
-
(0, $lYn3l$react.useEffect)(()=>{
|
|
132
|
-
// eslint-disable-next-line @silverhand/fp/no-mutation
|
|
133
|
-
callbackRef.current = callback; // Update ref to the latest callback.
|
|
134
|
-
}, [
|
|
135
|
-
callback
|
|
136
|
-
]);
|
|
137
|
-
const handleSignInCallback = (0, $lYn3l$react.useCallback)(async (callbackUri)=>{
|
|
138
|
-
if (!logtoClient) return (0, $52a461056f85891e$export$838ead842aa548e7)();
|
|
139
|
-
try {
|
|
140
|
-
setLoadingState(true);
|
|
141
|
-
await logtoClient.handleSignInCallback(callbackUri);
|
|
142
|
-
setIsAuthenticated(true);
|
|
143
|
-
callbackRef.current?.();
|
|
144
|
-
} catch (error) {
|
|
145
|
-
handleError(error, "Unexpected error occurred while handling sign in callback.");
|
|
146
|
-
} finally{
|
|
147
|
-
setLoadingState(false);
|
|
148
|
-
}
|
|
149
|
-
}, [
|
|
150
|
-
logtoClient,
|
|
151
|
-
setLoadingState,
|
|
152
|
-
setIsAuthenticated,
|
|
153
|
-
handleError
|
|
154
|
-
]);
|
|
155
|
-
(0, $lYn3l$react.useEffect)(()=>{
|
|
156
|
-
const currentPageUrl = window.location.href;
|
|
157
|
-
if (!isAuthenticated && logtoClient?.isSignInRedirected(currentPageUrl)) handleSignInCallback(currentPageUrl);
|
|
158
|
-
}, [
|
|
159
|
-
handleSignInCallback,
|
|
160
|
-
isAuthenticated,
|
|
161
|
-
logtoClient
|
|
162
|
-
]);
|
|
163
|
-
return {
|
|
164
|
-
isLoading: isLoading,
|
|
165
|
-
isAuthenticated: isAuthenticated,
|
|
166
|
-
error: error
|
|
167
|
-
};
|
|
168
|
-
};
|
|
169
|
-
const $ccb423956ca75d68$export$44fc9df4d2a1789a = ()=>{
|
|
170
|
-
const { logtoClient: logtoClient , loadingCount: loadingCount , isAuthenticated: isAuthenticated , error: error } = (0, $lYn3l$react.useContext)((0, $52a461056f85891e$export$e5bf247804b97da7));
|
|
171
|
-
const { setLoadingState: setLoadingState } = $ccb423956ca75d68$var$useLoadingState();
|
|
172
|
-
const { handleError: handleError } = $ccb423956ca75d68$var$useErrorHandler();
|
|
173
|
-
const isLoading = loadingCount > 0;
|
|
174
|
-
const signIn = (0, $lYn3l$react.useCallback)(async (redirectUri, interactionMode)=>{
|
|
175
|
-
if (!logtoClient) return (0, $52a461056f85891e$export$838ead842aa548e7)();
|
|
176
|
-
try {
|
|
177
|
-
setLoadingState(true);
|
|
178
|
-
await logtoClient.signIn(redirectUri, interactionMode);
|
|
179
|
-
} catch (error) {
|
|
180
|
-
handleError(error, "Unexpected error occurred while signing in.");
|
|
181
|
-
}
|
|
182
|
-
}, [
|
|
183
|
-
logtoClient,
|
|
184
|
-
setLoadingState,
|
|
185
|
-
handleError
|
|
186
|
-
]);
|
|
187
|
-
const signOut = (0, $lYn3l$react.useCallback)(async (postLogoutRedirectUri)=>{
|
|
188
|
-
if (!logtoClient) return (0, $52a461056f85891e$export$838ead842aa548e7)();
|
|
189
|
-
try {
|
|
190
|
-
setLoadingState(true);
|
|
191
|
-
await logtoClient.signOut(postLogoutRedirectUri);
|
|
192
|
-
// We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately
|
|
193
|
-
// even before navigating to the oidc end session endpoint, which might cause rendering problems.
|
|
194
|
-
// Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
|
|
195
|
-
} catch (error) {
|
|
196
|
-
handleError(error, "Unexpected error occurred while signing out.");
|
|
197
|
-
} finally{
|
|
198
|
-
setLoadingState(false);
|
|
199
|
-
}
|
|
200
|
-
}, [
|
|
201
|
-
logtoClient,
|
|
202
|
-
setLoadingState,
|
|
203
|
-
handleError
|
|
204
|
-
]);
|
|
205
|
-
const fetchUserInfo = (0, $lYn3l$react.useCallback)(async ()=>{
|
|
206
|
-
if (!logtoClient) return (0, $52a461056f85891e$export$838ead842aa548e7)();
|
|
207
|
-
try {
|
|
208
|
-
setLoadingState(true);
|
|
209
|
-
return await logtoClient.fetchUserInfo();
|
|
210
|
-
} catch (error) {
|
|
211
|
-
handleError(error, "Unexpected error occurred while fetching user info.");
|
|
212
|
-
} finally{
|
|
213
|
-
setLoadingState(false);
|
|
214
|
-
}
|
|
215
|
-
}, [
|
|
216
|
-
logtoClient,
|
|
217
|
-
setLoadingState,
|
|
218
|
-
handleError
|
|
219
|
-
]);
|
|
220
|
-
const getAccessToken = (0, $lYn3l$react.useCallback)(async (resource)=>{
|
|
221
|
-
if (!logtoClient) return (0, $52a461056f85891e$export$838ead842aa548e7)();
|
|
222
|
-
try {
|
|
223
|
-
setLoadingState(true);
|
|
224
|
-
return await logtoClient.getAccessToken(resource);
|
|
225
|
-
} catch (error) {
|
|
226
|
-
handleError(error, "Unexpected error occurred while getting access token.");
|
|
227
|
-
} finally{
|
|
228
|
-
setLoadingState(false);
|
|
229
|
-
}
|
|
230
|
-
}, [
|
|
231
|
-
logtoClient,
|
|
232
|
-
setLoadingState,
|
|
233
|
-
handleError
|
|
234
|
-
]);
|
|
235
|
-
const getIdTokenClaims = (0, $lYn3l$react.useCallback)(async ()=>{
|
|
236
|
-
if (!logtoClient) return (0, $52a461056f85891e$export$838ead842aa548e7)();
|
|
237
|
-
try {
|
|
238
|
-
return await logtoClient.getIdTokenClaims();
|
|
239
|
-
} catch {
|
|
240
|
-
// Do nothing if any exception occurs. Caller will get undefined value.
|
|
241
|
-
}
|
|
242
|
-
}, [
|
|
243
|
-
logtoClient
|
|
244
|
-
]);
|
|
245
|
-
if (!logtoClient) return (0, $52a461056f85891e$export$838ead842aa548e7)();
|
|
246
|
-
return {
|
|
247
|
-
isAuthenticated: isAuthenticated,
|
|
248
|
-
isLoading: isLoading,
|
|
249
|
-
error: error,
|
|
250
|
-
signIn: signIn,
|
|
251
|
-
signOut: signOut,
|
|
252
|
-
fetchUserInfo: fetchUserInfo,
|
|
253
|
-
getAccessToken: getAccessToken,
|
|
254
|
-
getIdTokenClaims: getIdTokenClaims
|
|
255
|
-
};
|
|
256
|
-
};
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
$parcel$exportWildcard(module.exports, $00c2be589188321f$exports);
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
export { LogtoClientError, LogtoError, OidcError, Prompt, ReservedScope, UserScope } from '@logto/browser';
|
|
2
|
+
export { LogtoProvider } from './provider.js';
|
|
3
|
+
export { useHandleSignInCallback, useLogto } from './hooks/index.js';
|
package/lib/provider.cjs
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
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, children }) => {
|
|
13
|
+
const [loadingCount, setLoadingCount] = react.useState(1);
|
|
14
|
+
const memorizedLogtoClient = react.useMemo(() => ({ logtoClient: new LogtoClient__default.default(config) }), [config]);
|
|
15
|
+
const [isAuthenticated, setIsAuthenticated] = react.useState(false);
|
|
16
|
+
const [error, setError] = react.useState();
|
|
17
|
+
react.useEffect(() => {
|
|
18
|
+
(async () => {
|
|
19
|
+
const isAuthenticated = await memorizedLogtoClient.logtoClient.isAuthenticated();
|
|
20
|
+
setIsAuthenticated(isAuthenticated);
|
|
21
|
+
setLoadingCount((count) => Math.max(0, count - 1));
|
|
22
|
+
})();
|
|
23
|
+
}, [memorizedLogtoClient]);
|
|
24
|
+
const memorizedContextValue = react.useMemo(() => ({
|
|
25
|
+
...memorizedLogtoClient,
|
|
26
|
+
isAuthenticated,
|
|
27
|
+
setIsAuthenticated,
|
|
28
|
+
loadingCount,
|
|
29
|
+
setLoadingCount,
|
|
30
|
+
error,
|
|
31
|
+
setError,
|
|
32
|
+
}), [memorizedLogtoClient, isAuthenticated, loadingCount, error]);
|
|
33
|
+
return jsxRuntime.jsx(context.LogtoContext.Provider, { value: memorizedContextValue, children: children });
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.LogtoProvider = LogtoProvider;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type LogtoConfig } from '@logto/browser';
|
|
2
|
+
import { type ReactNode } from 'react';
|
|
3
|
+
export type LogtoProviderProps = {
|
|
4
|
+
config: LogtoConfig;
|
|
5
|
+
children?: ReactNode;
|
|
6
|
+
};
|
|
7
|
+
export declare const LogtoProvider: ({ config, children }: LogtoProviderProps) => JSX.Element;
|
package/lib/provider.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import LogtoClient from '@logto/browser';
|
|
3
|
+
import { useState, useMemo, useEffect } from 'react';
|
|
4
|
+
import { LogtoContext } from './context.js';
|
|
5
|
+
|
|
6
|
+
const LogtoProvider = ({ config, children }) => {
|
|
7
|
+
const [loadingCount, setLoadingCount] = useState(1);
|
|
8
|
+
const memorizedLogtoClient = useMemo(() => ({ logtoClient: new LogtoClient(config) }), [config]);
|
|
9
|
+
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
10
|
+
const [error, setError] = useState();
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
(async () => {
|
|
13
|
+
const isAuthenticated = await memorizedLogtoClient.logtoClient.isAuthenticated();
|
|
14
|
+
setIsAuthenticated(isAuthenticated);
|
|
15
|
+
setLoadingCount((count) => Math.max(0, count - 1));
|
|
16
|
+
})();
|
|
17
|
+
}, [memorizedLogtoClient]);
|
|
18
|
+
const memorizedContextValue = useMemo(() => ({
|
|
19
|
+
...memorizedLogtoClient,
|
|
20
|
+
isAuthenticated,
|
|
21
|
+
setIsAuthenticated,
|
|
22
|
+
loadingCount,
|
|
23
|
+
setLoadingCount,
|
|
24
|
+
error,
|
|
25
|
+
setError,
|
|
26
|
+
}), [memorizedLogtoClient, isAuthenticated, loadingCount, error]);
|
|
27
|
+
return jsx(LogtoContext.Provider, { value: memorizedContextValue, children: children });
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export { LogtoProvider };
|
package/package.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/react",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"main": "./lib/index.
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./lib/index.cjs",
|
|
6
|
+
"module": "./lib/index.js",
|
|
7
|
+
"types": "./lib/index.d.ts",
|
|
6
8
|
"exports": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
+
"types": "./lib/index.d.ts",
|
|
10
|
+
"require": "./lib/index.cjs",
|
|
11
|
+
"import": "./lib/index.js",
|
|
12
|
+
"default": "./lib/index.js"
|
|
9
13
|
},
|
|
10
|
-
"module": "./lib/module.mjs",
|
|
11
|
-
"types": "./lib/index.d.ts",
|
|
12
14
|
"files": [
|
|
13
15
|
"lib"
|
|
14
16
|
],
|
|
@@ -18,42 +20,28 @@
|
|
|
18
20
|
"url": "https://github.com/logto-io/js.git",
|
|
19
21
|
"directory": "packages/react"
|
|
20
22
|
},
|
|
21
|
-
"scripts": {
|
|
22
|
-
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
23
|
-
"precommit": "lint-staged",
|
|
24
|
-
"check": "tsc --noEmit",
|
|
25
|
-
"build": "rm -rf lib/ && pnpm check && parcel build && cp lib/index.d.ts lib/module.d.mts",
|
|
26
|
-
"lint": "eslint --ext .ts --ext .tsx src",
|
|
27
|
-
"test": "jest",
|
|
28
|
-
"test:coverage": "jest --silent --coverage",
|
|
29
|
-
"prepack": "pnpm test"
|
|
30
|
-
},
|
|
31
23
|
"dependencies": {
|
|
32
|
-
"@logto/browser": "^
|
|
33
|
-
"@silverhand/essentials": "^
|
|
24
|
+
"@logto/browser": "^2.0.0",
|
|
25
|
+
"@silverhand/essentials": "^2.6.2"
|
|
34
26
|
},
|
|
35
27
|
"devDependencies": {
|
|
36
|
-
"@
|
|
37
|
-
"@
|
|
38
|
-
"@
|
|
39
|
-
"@
|
|
40
|
-
"@
|
|
41
|
-
"@
|
|
42
|
-
"@silverhand/ts-config": "^1.0.0",
|
|
43
|
-
"@silverhand/ts-config-react": "^2.0.0",
|
|
28
|
+
"@silverhand/eslint-config": "^3.0.1",
|
|
29
|
+
"@silverhand/eslint-config-react": "^3.0.1",
|
|
30
|
+
"@silverhand/ts-config": "^3.0.0",
|
|
31
|
+
"@silverhand/ts-config-react": "^3.0.0",
|
|
32
|
+
"@swc/core": "^1.3.50",
|
|
33
|
+
"@swc/jest": "^0.2.24",
|
|
44
34
|
"@testing-library/react-hooks": "^8.0.0",
|
|
45
|
-
"@types/jest": "^
|
|
35
|
+
"@types/jest": "^29.5.0",
|
|
46
36
|
"@types/react": "^17.0.39",
|
|
47
|
-
"eslint": "^8.
|
|
48
|
-
"jest": "^
|
|
37
|
+
"eslint": "^8.38.0",
|
|
38
|
+
"jest": "^29.5.0",
|
|
49
39
|
"lint-staged": "^13.0.0",
|
|
50
|
-
"parcel": "^2.8.3",
|
|
51
40
|
"postcss": "^8.4.6",
|
|
52
|
-
"prettier": "^2.7
|
|
41
|
+
"prettier": "^2.8.7",
|
|
53
42
|
"react": "^17.0.2",
|
|
54
43
|
"stylelint": "^15.0.0",
|
|
55
|
-
"
|
|
56
|
-
"typescript": "4.9.5"
|
|
44
|
+
"typescript": "^5.0.0"
|
|
57
45
|
},
|
|
58
46
|
"peerDependencies": {
|
|
59
47
|
"react": ">=16.8.0"
|
|
@@ -65,5 +53,13 @@
|
|
|
65
53
|
"publishConfig": {
|
|
66
54
|
"access": "public"
|
|
67
55
|
},
|
|
68
|
-
"
|
|
69
|
-
|
|
56
|
+
"scripts": {
|
|
57
|
+
"dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
|
|
58
|
+
"precommit": "lint-staged",
|
|
59
|
+
"check": "tsc --noEmit",
|
|
60
|
+
"build": "rm -rf lib/ && tsc -p tsconfig.build.json --noEmit && rollup -c",
|
|
61
|
+
"lint": "eslint --ext .ts --ext .tsx src",
|
|
62
|
+
"test": "jest",
|
|
63
|
+
"test:coverage": "jest --silent --coverage"
|
|
64
|
+
}
|
|
65
|
+
}
|
package/lib/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;AAGA,gCAAgC;IAC9B,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,eAAe,EAAE,OAAO,CAAC;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,kBAAkB,EAAE,MAAM,QAAQ,CAAC,MAAM,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC;IAClE,eAAe,EAAE,MAAM,QAAQ,CAAC,MAAM,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9D,QAAQ,EAAE,MAAM,QAAQ,CAAC,MAAM,cAAc,CAAC,KAAK,GAAG,SAAS,CAAC,CAAC,CAAC;CACnE,CAAC;ACNF,iCAAiC;IAC/B,MAAM,EAAE,WAAW,CAAC;IACpB,QAAQ,CAAC,EAAE,SAAS,CAAC;CACtB,CAAC;AAEF,OAAO,MAAM,sCAAuC,kBAAkB,gBA6BrE,CAAC;AClCF,aAAa;IACX,eAAe,EAAE,OAAO,CAAC;IACzB,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,aAAa,EAAE,MAAM,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAAC;IAC3D,cAAc,EAAE,CAAC,QAAQ,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IACnE,gBAAgB,EAAE,MAAM,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC,CAAC;IAC3D,MAAM,EAAE,CAAC,WAAW,EAAE,MAAM,EAAE,eAAe,CAAC,EAAE,eAAe,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAClF,OAAO,EAAE,CAAC,qBAAqB,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5D,CAAC;AAsCF,OAAA,MAAM,qCAAsC,MAAM,IAAI;;;;CA4CrD,CAAC;AAEF,OAAA,MAAM,gBAAe,KA4GpB,CAAC;AC5MF,YAAY,EACV,WAAW,EACX,aAAa,EACb,gBAAgB,EAChB,cAAc,EACd,oBAAoB,EACpB,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,SAAS,EACT,MAAM,EACN,aAAa,EACb,SAAS,GACV,MAAM,gBAAgB,CAAC","sources":["packages/react/src/src/context.tsx","packages/react/src/src/provider.tsx","packages/react/src/src/hooks/index.ts","packages/react/src/src/index.ts","packages/react/src/index.ts"],"sourcesContent":[null,null,null,null,"export type { LogtoContextProps } from './context';\n\nexport type {\n LogtoConfig,\n IdTokenClaims,\n UserInfoResponse,\n LogtoErrorCode,\n LogtoClientErrorCode,\n InteractionMode,\n} from '@logto/browser';\n\nexport {\n LogtoError,\n LogtoClientError,\n OidcError,\n Prompt,\n ReservedScope,\n UserScope,\n} from '@logto/browser';\n\nexport * from './provider';\n\nexport { useLogto, useHandleSignInCallback } from './hooks';\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/lib/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;ACAA;;;ACAA;AAaO,MAAM,4CAAoB,IAAa;IAC5C,MAAM,IAAI,MAAM,gDAAgD;AAClE;AAEO,MAAM,0DAAe,CAAA,GAAA,0BAAY,EAAqB;IAC3D,aAAa;IACb,iBAAiB,KAAK;IACtB,cAAc;IACd,OAAO;IACP,oBAAoB;IACpB,iBAAiB;IACjB,UAAU;AACZ;;;ADfO,MAAM,4CAAgB,CAAC,UAAE,OAAM,YAAE,SAAQ,EAAsB,GAAK;IACzE,MAAM,CAAC,cAAc,gBAAgB,GAAG,CAAA,GAAA,qBAAQ,AAAD,EAAE;IACjD,MAAM,uBAAuB,CAAA,GAAA,oBAAO,AAAD,EAAE,IAAO,CAAA;YAAE,aAAa,IAAI,CAAA,GAAA,6CAAU,EAAE;QAAQ,CAAA,GAAI;QAAC;KAAO;IAC/F,MAAM,CAAC,iBAAiB,mBAAmB,GAAG,CAAA,GAAA,qBAAO,EAAE,KAAK;IAC5D,MAAM,CAAC,OAAO,SAAS,GAAG,CAAA,GAAA,qBAAQ,AAAD;IAEjC,CAAA,GAAA,sBAAS,AAAD,EAAE,IAAM;QACb,CAAA,UAAY;YACX,MAAM,kBAAkB,MAAM,qBAAqB,WAAW,CAAC,eAAe;YAE9E,mBAAmB;YACnB,gBAAgB,CAAC,QAAU,KAAK,GAAG,CAAC,GAAG,QAAQ;QACjD,CAAA;IACF,GAAG;QAAC;KAAqB;IAEzB,MAAM,wBAAwB,CAAA,GAAA,oBAAO,AAAD,EAClC,IAAO,CAAA;YACL,GAAG,oBAAoB;6BACvB;gCACA;0BACA;6BACA;mBACA;sBACA;QACF,CAAA,GACA;QAAC;QAAsB;QAAiB;QAAc;KAA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GzB,IAAI;YACF,gBAAgB,IAAI;YAEpB,MAAM,YAAY,OAAO,CAAC;QAE1B,yGAAyG;QACzG,iGAAiG;QACjG,uGAAuG;QACzG,EAAE,OAAO,OAAgB;YACvB,YAAY,OAAO;QACrB,SAAU;YACR,gBAAgB,KAAK;QACvB;IACF,GACA;QAAC;QAAa;QAAiB;KAAY;IAG7C,MAAM,gBAAgB,CAAA,GAAA,wBAAU,EAAE,UAAY;QAC5C,IAAI,CAAC,aACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;QAGzB,IAAI;YACF,gBAAgB,IAAI;YAEpB,OAAO,MAAM,YAAY,aAAa;QACxC,EAAE,OAAO,OAAgB;YACvB,YAAY,OAAO;QACrB,SAAU;YACR,gBAAgB,KAAK;QACvB;IACF,GAAG;QAAC;QAAa;QAAiB;KAAY;IAE9C,MAAM,iBAAiB,CAAA,GAAA,wBAAW,AAAD,EAC/B,OAAO,WAAsB;QAC3B,IAAI,CAAC,aACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;QAGzB,IAAI;YACF,gBAAgB,IAAI;YAEpB,OAAO,MAAM,YAAY,cAAc,CAAC;QAC1C,EAAE,OAAO,OAAgB;YACvB,YAAY,OAAO;QACrB,SAAU;YACR,gBAAgB,KAAK;QACvB;IACF,GACA;QAAC;QAAa;QAAiB;KAAY;IAG7C,MAAM,mBAAmB,CAAA,GAAA,wBAAU,EAAE,UAAY;QAC/C,IAAI,CAAC,aACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;QAGzB,IAAI;YACF,OAAO,MAAM,YAAY,gBAAgB;QAC3C,EAAE,OAAM;QACN,uEAAuE;QACzE;IACF,GAAG;QAAC;KAAY;IAEhB,IAAI,CAAC,aACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;IAGzB,OAAO;yBACL;mBACA;eACA;gBACA;iBACA;uBACA;wBACA;0BACA;IACF;AACF;;","sources":["packages/react/src/index.ts","packages/react/src/provider.tsx","packages/react/src/context.tsx","packages/react/src/hooks/index.ts"],"sourcesContent":["export type { LogtoContextProps } from './context';\n\nexport type {\n LogtoConfig,\n IdTokenClaims,\n UserInfoResponse,\n LogtoErrorCode,\n LogtoClientErrorCode,\n InteractionMode,\n} from '@logto/browser';\n\nexport {\n LogtoError,\n LogtoClientError,\n OidcError,\n Prompt,\n ReservedScope,\n UserScope,\n} from '@logto/browser';\n\nexport * from './provider';\n\nexport { useLogto, useHandleSignInCallback } from './hooks';\n","import LogtoClient, { LogtoConfig } from '@logto/browser';\nimport { ReactNode, useEffect, useMemo, useState } from 'react';\n\nimport { LogtoContext } from './context';\n\nexport type LogtoProviderProps = {\n config: LogtoConfig;\n children?: ReactNode;\n};\n\nexport const LogtoProvider = ({ config, children }: LogtoProviderProps) => {\n const [loadingCount, setLoadingCount] = useState(1);\n const memorizedLogtoClient = useMemo(() => ({ logtoClient: new LogtoClient(config) }), [config]);\n const [isAuthenticated, setIsAuthenticated] = useState(false);\n const [error, setError] = useState<Error>();\n\n useEffect(() => {\n (async () => {\n const isAuthenticated = await memorizedLogtoClient.logtoClient.isAuthenticated();\n\n setIsAuthenticated(isAuthenticated);\n setLoadingCount((count) => Math.max(0, count - 1));\n })();\n }, [memorizedLogtoClient]);\n\n const memorizedContextValue = useMemo(\n () => ({\n ...memorizedLogtoClient,\n isAuthenticated,\n setIsAuthenticated,\n loadingCount,\n setLoadingCount,\n error,\n setError,\n }),\n [memorizedLogtoClient, isAuthenticated, loadingCount, error]\n );\n\n return <LogtoContext.Provider value={memorizedContextValue}>{children}</LogtoContext.Provider>;\n};\n","import LogtoClient from '@logto/browser';\nimport { createContext } from 'react';\n\nexport type LogtoContextProps = {\n logtoClient?: LogtoClient;\n isAuthenticated: boolean;\n loadingCount: number;\n error?: Error;\n setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;\n setLoadingCount: React.Dispatch<React.SetStateAction<number>>;\n setError: React.Dispatch<React.SetStateAction<Error | undefined>>;\n};\n\nexport const throwContextError = (): never => {\n throw new Error('Must be used inside <LogtoProvider> context.');\n};\n\nexport const LogtoContext = createContext<LogtoContextProps>({\n logtoClient: undefined,\n isAuthenticated: false,\n loadingCount: 0,\n error: undefined,\n setIsAuthenticated: throwContextError,\n setLoadingCount: throwContextError,\n setError: throwContextError,\n});\n","import { IdTokenClaims, InteractionMode, UserInfoResponse } from '@logto/browser';\nimport { useCallback, useContext, useEffect, useRef } from 'react';\n\nimport { LogtoContext, throwContextError } from '../context';\n\ntype Logto = {\n isAuthenticated: boolean;\n isLoading: boolean;\n error?: Error;\n fetchUserInfo: () => Promise<UserInfoResponse | undefined>;\n getAccessToken: (resource?: string) => Promise<string | undefined>;\n getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;\n signIn: (redirectUri: string, interactionMode?: InteractionMode) => Promise<void>;\n signOut: (postLogoutRedirectUri?: string) => Promise<void>;\n};\n\nconst useLoadingState = () => {\n const { loadingCount, setLoadingCount } = useContext(LogtoContext);\n const isLoading = loadingCount > 0;\n\n const setLoadingState = useCallback(\n (state: boolean) => {\n if (state) {\n setLoadingCount((count) => count + 1);\n } else {\n setLoadingCount((count) => Math.max(0, count - 1));\n }\n },\n [setLoadingCount]\n );\n\n return { isLoading, setLoadingState };\n};\n\nconst useErrorHandler = () => {\n const { setError } = useContext(LogtoContext);\n\n const handleError = useCallback(\n (error: unknown, fallbackErrorMessage?: string) => {\n if (error instanceof Error) {\n setError(error);\n } else if (fallbackErrorMessage) {\n setError(new Error(fallbackErrorMessage));\n }\n console.error(error);\n },\n [setError]\n );\n\n return { handleError };\n};\n\nconst useHandleSignInCallback = (callback?: () => void) => {\n const { logtoClient, isAuthenticated, error, setIsAuthenticated } = useContext(LogtoContext);\n const { isLoading, setLoadingState } = useLoadingState();\n const { handleError } = useErrorHandler();\n const callbackRef = useRef<() => void>();\n\n useEffect(() => {\n // eslint-disable-next-line @silverhand/fp/no-mutation\n callbackRef.current = callback; // Update ref to the latest callback.\n }, [callback]);\n\n const handleSignInCallback = useCallback(\n async (callbackUri: string) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n await logtoClient.handleSignInCallback(callbackUri);\n setIsAuthenticated(true);\n callbackRef.current?.();\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while handling sign in callback.');\n } finally {\n setLoadingState(false);\n }\n },\n [logtoClient, setLoadingState, setIsAuthenticated, handleError]\n );\n\n useEffect(() => {\n const currentPageUrl = window.location.href;\n\n if (!isAuthenticated && logtoClient?.isSignInRedirected(currentPageUrl)) {\n void handleSignInCallback(currentPageUrl);\n }\n }, [handleSignInCallback, isAuthenticated, logtoClient]);\n\n return {\n isLoading,\n isAuthenticated,\n error,\n };\n};\n\nconst useLogto = (): Logto => {\n const { logtoClient, loadingCount, isAuthenticated, error } = useContext(LogtoContext);\n const { setLoadingState } = useLoadingState();\n const { handleError } = useErrorHandler();\n\n const isLoading = loadingCount > 0;\n\n const signIn = useCallback(\n async (redirectUri: string, interactionMode?: InteractionMode) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n await logtoClient.signIn(redirectUri, interactionMode);\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while signing in.');\n }\n },\n [logtoClient, setLoadingState, handleError]\n );\n\n const signOut = useCallback(\n async (postLogoutRedirectUri?: string) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n await logtoClient.signOut(postLogoutRedirectUri);\n\n // We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately\n // even before navigating to the oidc end session endpoint, which might cause rendering problems.\n // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while signing out.');\n } finally {\n setLoadingState(false);\n }\n },\n [logtoClient, setLoadingState, handleError]\n );\n\n const fetchUserInfo = useCallback(async () => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n return await logtoClient.fetchUserInfo();\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while fetching user info.');\n } finally {\n setLoadingState(false);\n }\n }, [logtoClient, setLoadingState, handleError]);\n\n const getAccessToken = useCallback(\n async (resource?: string) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n return await logtoClient.getAccessToken(resource);\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while getting access token.');\n } finally {\n setLoadingState(false);\n }\n },\n [logtoClient, setLoadingState, handleError]\n );\n\n const getIdTokenClaims = useCallback(async () => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n return await logtoClient.getIdTokenClaims();\n } catch {\n // Do nothing if any exception occurs. Caller will get undefined value.\n }\n }, [logtoClient]);\n\n if (!logtoClient) {\n return throwContextError();\n }\n\n return {\n isAuthenticated,\n isLoading,\n error,\n signIn,\n signOut,\n fetchUserInfo,\n getAccessToken,\n getIdTokenClaims,\n };\n};\n\nexport { useLogto, useHandleSignInCallback };\n"],"names":[],"version":3,"file":"index.js.map"}
|
package/lib/module.d.mts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import LogtoClient, { LogtoConfig, IdTokenClaims, InteractionMode, UserInfoResponse } from "@logto/browser";
|
|
2
|
-
import { ReactNode } from "react";
|
|
3
|
-
export type LogtoContextProps = {
|
|
4
|
-
logtoClient?: LogtoClient;
|
|
5
|
-
isAuthenticated: boolean;
|
|
6
|
-
loadingCount: number;
|
|
7
|
-
error?: Error;
|
|
8
|
-
setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;
|
|
9
|
-
setLoadingCount: React.Dispatch<React.SetStateAction<number>>;
|
|
10
|
-
setError: React.Dispatch<React.SetStateAction<Error | undefined>>;
|
|
11
|
-
};
|
|
12
|
-
export type LogtoProviderProps = {
|
|
13
|
-
config: LogtoConfig;
|
|
14
|
-
children?: ReactNode;
|
|
15
|
-
};
|
|
16
|
-
export const LogtoProvider: ({ config, children }: LogtoProviderProps) => JSX.Element;
|
|
17
|
-
type Logto = {
|
|
18
|
-
isAuthenticated: boolean;
|
|
19
|
-
isLoading: boolean;
|
|
20
|
-
error?: Error;
|
|
21
|
-
fetchUserInfo: () => Promise<UserInfoResponse | undefined>;
|
|
22
|
-
getAccessToken: (resource?: string) => Promise<string | undefined>;
|
|
23
|
-
getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;
|
|
24
|
-
signIn: (redirectUri: string, interactionMode?: InteractionMode) => Promise<void>;
|
|
25
|
-
signOut: (postLogoutRedirectUri?: string) => Promise<void>;
|
|
26
|
-
};
|
|
27
|
-
export const useHandleSignInCallback: (callback?: () => void) => {
|
|
28
|
-
isLoading: boolean;
|
|
29
|
-
isAuthenticated: boolean;
|
|
30
|
-
error: Error | undefined;
|
|
31
|
-
};
|
|
32
|
-
export const useLogto: () => Logto;
|
|
33
|
-
export type { LogtoConfig, IdTokenClaims, UserInfoResponse, LogtoErrorCode, LogtoClientErrorCode, InteractionMode, } from '@logto/browser';
|
|
34
|
-
export { LogtoError, LogtoClientError, OidcError, Prompt, ReservedScope, UserScope, } from '@logto/browser';
|
|
35
|
-
|
|
36
|
-
//# sourceMappingURL=index.d.ts.map
|
package/lib/module.mjs
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import $19DtJ$logtobrowser, {LogtoError as $c7d9d85b511cb05a$re_export$LogtoError, LogtoClientError as $c7d9d85b511cb05a$re_export$LogtoClientError, OidcError as $c7d9d85b511cb05a$re_export$OidcError, Prompt as $c7d9d85b511cb05a$re_export$Prompt, ReservedScope as $c7d9d85b511cb05a$re_export$ReservedScope, UserScope as $c7d9d85b511cb05a$re_export$UserScope} from "@logto/browser";
|
|
2
|
-
import {jsx as $19DtJ$jsx} from "react/jsx-runtime";
|
|
3
|
-
import {useState as $19DtJ$useState, useMemo as $19DtJ$useMemo, useEffect as $19DtJ$useEffect, createContext as $19DtJ$createContext, useContext as $19DtJ$useContext, useCallback as $19DtJ$useCallback, useRef as $19DtJ$useRef} from "react";
|
|
4
|
-
|
|
5
|
-
function $parcel$export(e, n, v, s) {
|
|
6
|
-
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
var $8097f9b4a6c197ce$exports = {};
|
|
10
|
-
|
|
11
|
-
$parcel$export($8097f9b4a6c197ce$exports, "LogtoProvider", () => $8097f9b4a6c197ce$export$bfa587c2c3245948);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const $e48cc35865e6f365$export$838ead842aa548e7 = ()=>{
|
|
17
|
-
throw new Error("Must be used inside <LogtoProvider> context.");
|
|
18
|
-
};
|
|
19
|
-
const $e48cc35865e6f365$export$e5bf247804b97da7 = /*#__PURE__*/ (0, $19DtJ$createContext)({
|
|
20
|
-
logtoClient: undefined,
|
|
21
|
-
isAuthenticated: false,
|
|
22
|
-
loadingCount: 0,
|
|
23
|
-
error: undefined,
|
|
24
|
-
setIsAuthenticated: $e48cc35865e6f365$export$838ead842aa548e7,
|
|
25
|
-
setLoadingCount: $e48cc35865e6f365$export$838ead842aa548e7,
|
|
26
|
-
setError: $e48cc35865e6f365$export$838ead842aa548e7
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const $8097f9b4a6c197ce$export$bfa587c2c3245948 = ({ config: config , children: children })=>{
|
|
31
|
-
const [loadingCount, setLoadingCount] = (0, $19DtJ$useState)(1);
|
|
32
|
-
const memorizedLogtoClient = (0, $19DtJ$useMemo)(()=>({
|
|
33
|
-
logtoClient: new (0, $19DtJ$logtobrowser)(config)
|
|
34
|
-
}), [
|
|
35
|
-
config
|
|
36
|
-
]);
|
|
37
|
-
const [isAuthenticated, setIsAuthenticated] = (0, $19DtJ$useState)(false);
|
|
38
|
-
const [error, setError] = (0, $19DtJ$useState)();
|
|
39
|
-
(0, $19DtJ$useEffect)(()=>{
|
|
40
|
-
(async ()=>{
|
|
41
|
-
const isAuthenticated = await memorizedLogtoClient.logtoClient.isAuthenticated();
|
|
42
|
-
setIsAuthenticated(isAuthenticated);
|
|
43
|
-
setLoadingCount((count)=>Math.max(0, count - 1));
|
|
44
|
-
})();
|
|
45
|
-
}, [
|
|
46
|
-
memorizedLogtoClient
|
|
47
|
-
]);
|
|
48
|
-
const memorizedContextValue = (0, $19DtJ$useMemo)(()=>({
|
|
49
|
-
...memorizedLogtoClient,
|
|
50
|
-
isAuthenticated: isAuthenticated,
|
|
51
|
-
setIsAuthenticated: setIsAuthenticated,
|
|
52
|
-
loadingCount: loadingCount,
|
|
53
|
-
setLoadingCount: setLoadingCount,
|
|
54
|
-
error: error,
|
|
55
|
-
setError: setError
|
|
56
|
-
}), [
|
|
57
|
-
memorizedLogtoClient,
|
|
58
|
-
isAuthenticated,
|
|
59
|
-
loadingCount,
|
|
60
|
-
error
|
|
61
|
-
]);
|
|
62
|
-
return /*#__PURE__*/ (0, $19DtJ$jsx)((0, $e48cc35865e6f365$export$e5bf247804b97da7).Provider, {
|
|
63
|
-
value: memorizedContextValue,
|
|
64
|
-
children: children
|
|
65
|
-
});
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const $9cc626600a847be2$var$useLoadingState = ()=>{
|
|
72
|
-
const { loadingCount: loadingCount , setLoadingCount: setLoadingCount } = (0, $19DtJ$useContext)((0, $e48cc35865e6f365$export$e5bf247804b97da7));
|
|
73
|
-
const isLoading = loadingCount > 0;
|
|
74
|
-
const setLoadingState = (0, $19DtJ$useCallback)((state)=>{
|
|
75
|
-
if (state) setLoadingCount((count)=>count + 1);
|
|
76
|
-
else setLoadingCount((count)=>Math.max(0, count - 1));
|
|
77
|
-
}, [
|
|
78
|
-
setLoadingCount
|
|
79
|
-
]);
|
|
80
|
-
return {
|
|
81
|
-
isLoading: isLoading,
|
|
82
|
-
setLoadingState: setLoadingState
|
|
83
|
-
};
|
|
84
|
-
};
|
|
85
|
-
const $9cc626600a847be2$var$useErrorHandler = ()=>{
|
|
86
|
-
const { setError: setError } = (0, $19DtJ$useContext)((0, $e48cc35865e6f365$export$e5bf247804b97da7));
|
|
87
|
-
const handleError = (0, $19DtJ$useCallback)((error, fallbackErrorMessage)=>{
|
|
88
|
-
if (error instanceof Error) setError(error);
|
|
89
|
-
else if (fallbackErrorMessage) setError(new Error(fallbackErrorMessage));
|
|
90
|
-
console.error(error);
|
|
91
|
-
}, [
|
|
92
|
-
setError
|
|
93
|
-
]);
|
|
94
|
-
return {
|
|
95
|
-
handleError: handleError
|
|
96
|
-
};
|
|
97
|
-
};
|
|
98
|
-
const $9cc626600a847be2$export$84e88c4b3c082374 = (callback)=>{
|
|
99
|
-
const { logtoClient: logtoClient , isAuthenticated: isAuthenticated , error: error , setIsAuthenticated: setIsAuthenticated } = (0, $19DtJ$useContext)((0, $e48cc35865e6f365$export$e5bf247804b97da7));
|
|
100
|
-
const { isLoading: isLoading , setLoadingState: setLoadingState } = $9cc626600a847be2$var$useLoadingState();
|
|
101
|
-
const { handleError: handleError } = $9cc626600a847be2$var$useErrorHandler();
|
|
102
|
-
const callbackRef = (0, $19DtJ$useRef)();
|
|
103
|
-
(0, $19DtJ$useEffect)(()=>{
|
|
104
|
-
// eslint-disable-next-line @silverhand/fp/no-mutation
|
|
105
|
-
callbackRef.current = callback; // Update ref to the latest callback.
|
|
106
|
-
}, [
|
|
107
|
-
callback
|
|
108
|
-
]);
|
|
109
|
-
const handleSignInCallback = (0, $19DtJ$useCallback)(async (callbackUri)=>{
|
|
110
|
-
if (!logtoClient) return (0, $e48cc35865e6f365$export$838ead842aa548e7)();
|
|
111
|
-
try {
|
|
112
|
-
setLoadingState(true);
|
|
113
|
-
await logtoClient.handleSignInCallback(callbackUri);
|
|
114
|
-
setIsAuthenticated(true);
|
|
115
|
-
callbackRef.current?.();
|
|
116
|
-
} catch (error) {
|
|
117
|
-
handleError(error, "Unexpected error occurred while handling sign in callback.");
|
|
118
|
-
} finally{
|
|
119
|
-
setLoadingState(false);
|
|
120
|
-
}
|
|
121
|
-
}, [
|
|
122
|
-
logtoClient,
|
|
123
|
-
setLoadingState,
|
|
124
|
-
setIsAuthenticated,
|
|
125
|
-
handleError
|
|
126
|
-
]);
|
|
127
|
-
(0, $19DtJ$useEffect)(()=>{
|
|
128
|
-
const currentPageUrl = window.location.href;
|
|
129
|
-
if (!isAuthenticated && logtoClient?.isSignInRedirected(currentPageUrl)) handleSignInCallback(currentPageUrl);
|
|
130
|
-
}, [
|
|
131
|
-
handleSignInCallback,
|
|
132
|
-
isAuthenticated,
|
|
133
|
-
logtoClient
|
|
134
|
-
]);
|
|
135
|
-
return {
|
|
136
|
-
isLoading: isLoading,
|
|
137
|
-
isAuthenticated: isAuthenticated,
|
|
138
|
-
error: error
|
|
139
|
-
};
|
|
140
|
-
};
|
|
141
|
-
const $9cc626600a847be2$export$44fc9df4d2a1789a = ()=>{
|
|
142
|
-
const { logtoClient: logtoClient , loadingCount: loadingCount , isAuthenticated: isAuthenticated , error: error } = (0, $19DtJ$useContext)((0, $e48cc35865e6f365$export$e5bf247804b97da7));
|
|
143
|
-
const { setLoadingState: setLoadingState } = $9cc626600a847be2$var$useLoadingState();
|
|
144
|
-
const { handleError: handleError } = $9cc626600a847be2$var$useErrorHandler();
|
|
145
|
-
const isLoading = loadingCount > 0;
|
|
146
|
-
const signIn = (0, $19DtJ$useCallback)(async (redirectUri, interactionMode)=>{
|
|
147
|
-
if (!logtoClient) return (0, $e48cc35865e6f365$export$838ead842aa548e7)();
|
|
148
|
-
try {
|
|
149
|
-
setLoadingState(true);
|
|
150
|
-
await logtoClient.signIn(redirectUri, interactionMode);
|
|
151
|
-
} catch (error) {
|
|
152
|
-
handleError(error, "Unexpected error occurred while signing in.");
|
|
153
|
-
}
|
|
154
|
-
}, [
|
|
155
|
-
logtoClient,
|
|
156
|
-
setLoadingState,
|
|
157
|
-
handleError
|
|
158
|
-
]);
|
|
159
|
-
const signOut = (0, $19DtJ$useCallback)(async (postLogoutRedirectUri)=>{
|
|
160
|
-
if (!logtoClient) return (0, $e48cc35865e6f365$export$838ead842aa548e7)();
|
|
161
|
-
try {
|
|
162
|
-
setLoadingState(true);
|
|
163
|
-
await logtoClient.signOut(postLogoutRedirectUri);
|
|
164
|
-
// We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately
|
|
165
|
-
// even before navigating to the oidc end session endpoint, which might cause rendering problems.
|
|
166
|
-
// Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
|
|
167
|
-
} catch (error) {
|
|
168
|
-
handleError(error, "Unexpected error occurred while signing out.");
|
|
169
|
-
} finally{
|
|
170
|
-
setLoadingState(false);
|
|
171
|
-
}
|
|
172
|
-
}, [
|
|
173
|
-
logtoClient,
|
|
174
|
-
setLoadingState,
|
|
175
|
-
handleError
|
|
176
|
-
]);
|
|
177
|
-
const fetchUserInfo = (0, $19DtJ$useCallback)(async ()=>{
|
|
178
|
-
if (!logtoClient) return (0, $e48cc35865e6f365$export$838ead842aa548e7)();
|
|
179
|
-
try {
|
|
180
|
-
setLoadingState(true);
|
|
181
|
-
return await logtoClient.fetchUserInfo();
|
|
182
|
-
} catch (error) {
|
|
183
|
-
handleError(error, "Unexpected error occurred while fetching user info.");
|
|
184
|
-
} finally{
|
|
185
|
-
setLoadingState(false);
|
|
186
|
-
}
|
|
187
|
-
}, [
|
|
188
|
-
logtoClient,
|
|
189
|
-
setLoadingState,
|
|
190
|
-
handleError
|
|
191
|
-
]);
|
|
192
|
-
const getAccessToken = (0, $19DtJ$useCallback)(async (resource)=>{
|
|
193
|
-
if (!logtoClient) return (0, $e48cc35865e6f365$export$838ead842aa548e7)();
|
|
194
|
-
try {
|
|
195
|
-
setLoadingState(true);
|
|
196
|
-
return await logtoClient.getAccessToken(resource);
|
|
197
|
-
} catch (error) {
|
|
198
|
-
handleError(error, "Unexpected error occurred while getting access token.");
|
|
199
|
-
} finally{
|
|
200
|
-
setLoadingState(false);
|
|
201
|
-
}
|
|
202
|
-
}, [
|
|
203
|
-
logtoClient,
|
|
204
|
-
setLoadingState,
|
|
205
|
-
handleError
|
|
206
|
-
]);
|
|
207
|
-
const getIdTokenClaims = (0, $19DtJ$useCallback)(async ()=>{
|
|
208
|
-
if (!logtoClient) return (0, $e48cc35865e6f365$export$838ead842aa548e7)();
|
|
209
|
-
try {
|
|
210
|
-
return await logtoClient.getIdTokenClaims();
|
|
211
|
-
} catch {
|
|
212
|
-
// Do nothing if any exception occurs. Caller will get undefined value.
|
|
213
|
-
}
|
|
214
|
-
}, [
|
|
215
|
-
logtoClient
|
|
216
|
-
]);
|
|
217
|
-
if (!logtoClient) return (0, $e48cc35865e6f365$export$838ead842aa548e7)();
|
|
218
|
-
return {
|
|
219
|
-
isAuthenticated: isAuthenticated,
|
|
220
|
-
isLoading: isLoading,
|
|
221
|
-
error: error,
|
|
222
|
-
signIn: signIn,
|
|
223
|
-
signOut: signOut,
|
|
224
|
-
fetchUserInfo: fetchUserInfo,
|
|
225
|
-
getAccessToken: getAccessToken,
|
|
226
|
-
getIdTokenClaims: getIdTokenClaims
|
|
227
|
-
};
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
export {$c7d9d85b511cb05a$re_export$LogtoError as LogtoError, $c7d9d85b511cb05a$re_export$LogtoClientError as LogtoClientError, $c7d9d85b511cb05a$re_export$OidcError as OidcError, $c7d9d85b511cb05a$re_export$Prompt as Prompt, $c7d9d85b511cb05a$re_export$ReservedScope as ReservedScope, $c7d9d85b511cb05a$re_export$UserScope as UserScope, $9cc626600a847be2$export$44fc9df4d2a1789a as useLogto, $9cc626600a847be2$export$84e88c4b3c082374 as useHandleSignInCallback, $8097f9b4a6c197ce$export$bfa587c2c3245948 as LogtoProvider};
|
|
234
|
-
//# sourceMappingURL=module.mjs.map
|
package/lib/module.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;;;;;AAAA;;;;ACAA;;;ACAA;AAaO,MAAM,4CAAoB,IAAa;IAC5C,MAAM,IAAI,MAAM,gDA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5C,IAAI,CAAC,aACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;QAGzB,IAAI;YACF,gBAAgB,IAAI;YAEpB,OAAO,MAAM,YAAY,aAAa;QACxC,EAAE,OAAO,OAAgB;YACvB,YAAY,OAAO;QACrB,SAAU;YACR,gBAAgB,KAAK;QACvB;IACF,GAAG;QAAC;QAAa;QAAiB;KAAY;IAE9C,MAAM,iBAAiB,CAAA,GAAA,kBAAW,AAAD,EAC/B,OAAO,WAAsB;QAC3B,IAAI,CAAC,aACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;QAGzB,IAAI;YACF,gBAAgB,IAAI;YAEpB,OAAO,MAAM,YAAY,cAAc,CAAC;QAC1C,EAAE,OAAO,OAAgB;YACvB,YAAY,OAAO;QACrB,SAAU;YACR,gBAAgB,KAAK;QACvB;IACF,GACA;QAAC;QAAa;QAAiB;KAAY;IAG7C,MAAM,mBAAmB,CAAA,GAAA,kBAAU,EAAE,UAAY;QAC/C,IAAI,CAAC,aACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;QAGzB,IAAI;YACF,OAAO,MAAM,YAAY,gBAAgB;QAC3C,EAAE,OAAM;QACN,uEAAuE;QACzE;IACF,GAAG;QAAC;KAAY;IAEhB,IAAI,CAAC,aACH,OAAO,CAAA,GAAA,yCAAiB,AAAD;IAGzB,OAAO;yBACL;mBACA;eACA;gBACA;iBACA;uBACA;wBACA;0BACA;IACF;AACF;;","sources":["packages/react/src/index.ts","packages/react/src/provider.tsx","packages/react/src/context.tsx","packages/react/src/hooks/index.ts"],"sourcesContent":["export type { LogtoContextProps } from './context';\n\nexport type {\n LogtoConfig,\n IdTokenClaims,\n UserInfoResponse,\n LogtoErrorCode,\n LogtoClientErrorCode,\n InteractionMode,\n} from '@logto/browser';\n\nexport {\n LogtoError,\n LogtoClientError,\n OidcError,\n Prompt,\n ReservedScope,\n UserScope,\n} from '@logto/browser';\n\nexport * from './provider';\n\nexport { useLogto, useHandleSignInCallback } from './hooks';\n","import LogtoClient, { LogtoConfig } from '@logto/browser';\nimport { ReactNode, useEffect, useMemo, useState } from 'react';\n\nimport { LogtoContext } from './context';\n\nexport type LogtoProviderProps = {\n config: LogtoConfig;\n children?: ReactNode;\n};\n\nexport const LogtoProvider = ({ config, children }: LogtoProviderProps) => {\n const [loadingCount, setLoadingCount] = useState(1);\n const memorizedLogtoClient = useMemo(() => ({ logtoClient: new LogtoClient(config) }), [config]);\n const [isAuthenticated, setIsAuthenticated] = useState(false);\n const [error, setError] = useState<Error>();\n\n useEffect(() => {\n (async () => {\n const isAuthenticated = await memorizedLogtoClient.logtoClient.isAuthenticated();\n\n setIsAuthenticated(isAuthenticated);\n setLoadingCount((count) => Math.max(0, count - 1));\n })();\n }, [memorizedLogtoClient]);\n\n const memorizedContextValue = useMemo(\n () => ({\n ...memorizedLogtoClient,\n isAuthenticated,\n setIsAuthenticated,\n loadingCount,\n setLoadingCount,\n error,\n setError,\n }),\n [memorizedLogtoClient, isAuthenticated, loadingCount, error]\n );\n\n return <LogtoContext.Provider value={memorizedContextValue}>{children}</LogtoContext.Provider>;\n};\n","import LogtoClient from '@logto/browser';\nimport { createContext } from 'react';\n\nexport type LogtoContextProps = {\n logtoClient?: LogtoClient;\n isAuthenticated: boolean;\n loadingCount: number;\n error?: Error;\n setIsAuthenticated: React.Dispatch<React.SetStateAction<boolean>>;\n setLoadingCount: React.Dispatch<React.SetStateAction<number>>;\n setError: React.Dispatch<React.SetStateAction<Error | undefined>>;\n};\n\nexport const throwContextError = (): never => {\n throw new Error('Must be used inside <LogtoProvider> context.');\n};\n\nexport const LogtoContext = createContext<LogtoContextProps>({\n logtoClient: undefined,\n isAuthenticated: false,\n loadingCount: 0,\n error: undefined,\n setIsAuthenticated: throwContextError,\n setLoadingCount: throwContextError,\n setError: throwContextError,\n});\n","import { IdTokenClaims, InteractionMode, UserInfoResponse } from '@logto/browser';\nimport { useCallback, useContext, useEffect, useRef } from 'react';\n\nimport { LogtoContext, throwContextError } from '../context';\n\ntype Logto = {\n isAuthenticated: boolean;\n isLoading: boolean;\n error?: Error;\n fetchUserInfo: () => Promise<UserInfoResponse | undefined>;\n getAccessToken: (resource?: string) => Promise<string | undefined>;\n getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;\n signIn: (redirectUri: string, interactionMode?: InteractionMode) => Promise<void>;\n signOut: (postLogoutRedirectUri?: string) => Promise<void>;\n};\n\nconst useLoadingState = () => {\n const { loadingCount, setLoadingCount } = useContext(LogtoContext);\n const isLoading = loadingCount > 0;\n\n const setLoadingState = useCallback(\n (state: boolean) => {\n if (state) {\n setLoadingCount((count) => count + 1);\n } else {\n setLoadingCount((count) => Math.max(0, count - 1));\n }\n },\n [setLoadingCount]\n );\n\n return { isLoading, setLoadingState };\n};\n\nconst useErrorHandler = () => {\n const { setError } = useContext(LogtoContext);\n\n const handleError = useCallback(\n (error: unknown, fallbackErrorMessage?: string) => {\n if (error instanceof Error) {\n setError(error);\n } else if (fallbackErrorMessage) {\n setError(new Error(fallbackErrorMessage));\n }\n console.error(error);\n },\n [setError]\n );\n\n return { handleError };\n};\n\nconst useHandleSignInCallback = (callback?: () => void) => {\n const { logtoClient, isAuthenticated, error, setIsAuthenticated } = useContext(LogtoContext);\n const { isLoading, setLoadingState } = useLoadingState();\n const { handleError } = useErrorHandler();\n const callbackRef = useRef<() => void>();\n\n useEffect(() => {\n // eslint-disable-next-line @silverhand/fp/no-mutation\n callbackRef.current = callback; // Update ref to the latest callback.\n }, [callback]);\n\n const handleSignInCallback = useCallback(\n async (callbackUri: string) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n await logtoClient.handleSignInCallback(callbackUri);\n setIsAuthenticated(true);\n callbackRef.current?.();\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while handling sign in callback.');\n } finally {\n setLoadingState(false);\n }\n },\n [logtoClient, setLoadingState, setIsAuthenticated, handleError]\n );\n\n useEffect(() => {\n const currentPageUrl = window.location.href;\n\n if (!isAuthenticated && logtoClient?.isSignInRedirected(currentPageUrl)) {\n void handleSignInCallback(currentPageUrl);\n }\n }, [handleSignInCallback, isAuthenticated, logtoClient]);\n\n return {\n isLoading,\n isAuthenticated,\n error,\n };\n};\n\nconst useLogto = (): Logto => {\n const { logtoClient, loadingCount, isAuthenticated, error } = useContext(LogtoContext);\n const { setLoadingState } = useLoadingState();\n const { handleError } = useErrorHandler();\n\n const isLoading = loadingCount > 0;\n\n const signIn = useCallback(\n async (redirectUri: string, interactionMode?: InteractionMode) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n await logtoClient.signIn(redirectUri, interactionMode);\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while signing in.');\n }\n },\n [logtoClient, setLoadingState, handleError]\n );\n\n const signOut = useCallback(\n async (postLogoutRedirectUri?: string) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n await logtoClient.signOut(postLogoutRedirectUri);\n\n // We deliberately do NOT set isAuthenticated to false here, because the app state may change immediately\n // even before navigating to the oidc end session endpoint, which might cause rendering problems.\n // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while signing out.');\n } finally {\n setLoadingState(false);\n }\n },\n [logtoClient, setLoadingState, handleError]\n );\n\n const fetchUserInfo = useCallback(async () => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n return await logtoClient.fetchUserInfo();\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while fetching user info.');\n } finally {\n setLoadingState(false);\n }\n }, [logtoClient, setLoadingState, handleError]);\n\n const getAccessToken = useCallback(\n async (resource?: string) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n return await logtoClient.getAccessToken(resource);\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while getting access token.');\n } finally {\n setLoadingState(false);\n }\n },\n [logtoClient, setLoadingState, handleError]\n );\n\n const getIdTokenClaims = useCallback(async () => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n return await logtoClient.getIdTokenClaims();\n } catch {\n // Do nothing if any exception occurs. Caller will get undefined value.\n }\n }, [logtoClient]);\n\n if (!logtoClient) {\n return throwContextError();\n }\n\n return {\n isAuthenticated,\n isLoading,\n error,\n signIn,\n signOut,\n fetchUserInfo,\n getAccessToken,\n getIdTokenClaims,\n };\n};\n\nexport { useLogto, useHandleSignInCallback };\n"],"names":[],"version":3,"file":"module.mjs.map"}
|