@logto/react 1.0.0-alpha.3 → 1.0.0-beta.10
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/README.md +8 -96
- package/lib/index.d.ts +6 -5
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +35 -10
- package/lib/index.js.map +1 -1
- package/lib/module.js +36 -13
- package/lib/module.js.map +1 -1
- package/package.json +16 -16
package/README.md
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
[](https://github.com/logto-io/js/actions/workflows/main.yml)
|
|
4
4
|
[](https://app.codecov.io/gh/logto-io/js?branch=master)
|
|
5
5
|
|
|
6
|
-
The Logto React SDK written in TypeScript. Check out our [integration guide](https://docs.logto.io/integrate-
|
|
6
|
+
The Logto React SDK written in TypeScript. Check out our [integration guide](https://docs.logto.io/docs/recipes/integrate-logto/react) or [docs](https://docs.logto.io/sdk/JavaScript/react/) for more information.
|
|
7
7
|
|
|
8
|
-
We also provide [集成指南](https://docs.logto.io/zh-cn/integrate-
|
|
8
|
+
We also provide [集成指南](https://docs.logto.io/zh-cn/docs/recipes/integrate-logto/react/) and [文档](https://docs.logto.io/zh-cn/sdk/JavaScript/react/) in Simplified Chinese.
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
@@ -24,109 +24,21 @@ yarn add @logto/react
|
|
|
24
24
|
### Using pnpm
|
|
25
25
|
|
|
26
26
|
```bash
|
|
27
|
-
pnpm
|
|
27
|
+
pnpm add @logto/react
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
## Get sample
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
<script src="https://logto.io/js/logto-sdk-react/0.1.0/logto-sdk-react.production.js" />
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
## Get Started
|
|
37
|
-
|
|
38
|
-
A sample project with the following code snippets can be found at [React Sample](https://github.com/logto-io/js/tree/master/packages/react-sample)
|
|
32
|
+
A sample project can be found at [React Sample](https://github.com/logto-io/js/tree/master/packages/react-sample)
|
|
39
33
|
|
|
40
|
-
Check out the source code and try it
|
|
34
|
+
Check out the source code and try it with ease.
|
|
41
35
|
|
|
42
|
-
```bash
|
|
43
|
-
pnpm i && pnpm start
|
|
44
36
|
```
|
|
45
|
-
|
|
46
|
-
### Initiate LogtoClient
|
|
47
|
-
|
|
48
|
-
```tsx
|
|
49
|
-
import { LogtoProvider, LogtoConfig } from '@logto/react';
|
|
50
|
-
|
|
51
|
-
const App = () => {
|
|
52
|
-
const config: LogtoConfig = {
|
|
53
|
-
clientId: 'foo',
|
|
54
|
-
endpoint: 'https://your-endpoint-domain.com'
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
return (
|
|
58
|
-
<BrowserRouter>
|
|
59
|
-
<LogtoProvider config={config}>
|
|
60
|
-
<Routes>
|
|
61
|
-
<Route path="/" element={<Home />} />
|
|
62
|
-
<Route path="/callback" element={<Callback />} />
|
|
63
|
-
<Route
|
|
64
|
-
path="/protected-resource"
|
|
65
|
-
element={
|
|
66
|
-
<RequireAuth>
|
|
67
|
-
<ProtectedResource />
|
|
68
|
-
</RequireAuth>
|
|
69
|
-
}
|
|
70
|
-
/>
|
|
71
|
-
</Routes>
|
|
72
|
-
</LogtoProvider>
|
|
73
|
-
</BrowserRouter>
|
|
74
|
-
);
|
|
75
|
-
};
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
### Setup your sign-in
|
|
79
|
-
|
|
80
|
-
```tsx
|
|
81
|
-
import { useLogto } from '@logto/react';
|
|
82
|
-
|
|
83
|
-
const SignInButton = () => {
|
|
84
|
-
const { signIn } = useLogto();
|
|
85
|
-
const redirectUrl = window.location.origin + '/callback';
|
|
86
|
-
|
|
87
|
-
return <button onClick={() => signIn(redirectUrl)}>Sign In</button>;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export default SignInButton;
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
### Retrieve Auth Status
|
|
94
|
-
|
|
95
|
-
```tsx
|
|
96
|
-
import { useLogto } from '@logto/react';
|
|
97
|
-
|
|
98
|
-
const App = () => {
|
|
99
|
-
const { isAuthenticated, signIn } = useLogto();
|
|
100
|
-
|
|
101
|
-
if !(isAuthenticated) {
|
|
102
|
-
return <SignInButton />
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return <>
|
|
106
|
-
<AppContent />
|
|
107
|
-
<SignOutButton />
|
|
108
|
-
</>
|
|
109
|
-
};
|
|
110
|
-
```
|
|
111
|
-
|
|
112
|
-
### Sign out
|
|
113
|
-
|
|
114
|
-
```tsx
|
|
115
|
-
import React from 'react';
|
|
116
|
-
import { useLogto } from '@logto/react';
|
|
117
|
-
|
|
118
|
-
const SignOutButton = () => {
|
|
119
|
-
const { signOut } = useLogto();
|
|
120
|
-
const postLogoutRedirectUri = window.location.origin;
|
|
121
|
-
|
|
122
|
-
return <button onClick={() => signOut(postLogoutRedirectUri)}>Sign out</button>;
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
export default SignOutButton;
|
|
37
|
+
pnpm i && pnpm start
|
|
126
38
|
```
|
|
127
39
|
|
|
128
40
|
## Resources
|
|
129
41
|
|
|
130
42
|
[](https://logto.io/)
|
|
131
|
-
[](https://docs.logto.io/
|
|
43
|
+
[](https://docs.logto.io/sdk/JavaScript/react/)
|
|
132
44
|
[](https://discord.gg/UEPaF3j5e6)
|
package/lib/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import LogtoClient, { LogtoConfig, IdTokenClaims } from "@logto/browser";
|
|
1
|
+
import LogtoClient, { LogtoConfig, IdTokenClaims, UserInfoResponse } from "@logto/browser";
|
|
2
2
|
import { ReactNode } from "react";
|
|
3
3
|
export type LogtoContextProps = {
|
|
4
4
|
logtoClient?: LogtoClient;
|
|
@@ -18,18 +18,19 @@ type Logto = {
|
|
|
18
18
|
isAuthenticated: boolean;
|
|
19
19
|
isLoading: boolean;
|
|
20
20
|
error?: Error;
|
|
21
|
+
fetchUserInfo: () => Promise<UserInfoResponse | undefined>;
|
|
21
22
|
getAccessToken: (resource?: string) => Promise<string | undefined>;
|
|
22
|
-
getIdTokenClaims: () => IdTokenClaims | undefined
|
|
23
|
+
getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;
|
|
23
24
|
signIn: (redirectUri: string) => Promise<void>;
|
|
24
25
|
signOut: (postLogoutRedirectUri?: string) => Promise<void>;
|
|
25
26
|
};
|
|
26
|
-
export const useHandleSignInCallback: (callback?: (
|
|
27
|
+
export const useHandleSignInCallback: (callback?: () => void) => {
|
|
27
28
|
isLoading: boolean;
|
|
28
29
|
isAuthenticated: boolean;
|
|
29
30
|
error: Error | undefined;
|
|
30
31
|
};
|
|
31
32
|
export const useLogto: () => Logto;
|
|
32
|
-
export type { LogtoConfig, IdTokenClaims, LogtoErrorCode, LogtoClientErrorCode, } from '@logto/browser';
|
|
33
|
-
export { LogtoError, LogtoClientError, OidcError, Prompt } from '@logto/browser';
|
|
33
|
+
export type { LogtoConfig, IdTokenClaims, UserInfoResponse, LogtoErrorCode, LogtoClientErrorCode, } from '@logto/browser';
|
|
34
|
+
export { LogtoError, LogtoClientError, OidcError, Prompt, ReservedScope, UserScope, } from '@logto/browser';
|
|
34
35
|
|
|
35
36
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,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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,GACrB,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} 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
CHANGED
|
@@ -29,6 +29,8 @@ $parcel$export(module.exports, "LogtoError", () => $22652c0d956faacc$re_export$L
|
|
|
29
29
|
$parcel$export(module.exports, "LogtoClientError", () => $22652c0d956faacc$re_export$LogtoClientError);
|
|
30
30
|
$parcel$export(module.exports, "OidcError", () => $22652c0d956faacc$re_export$OidcError);
|
|
31
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);
|
|
32
34
|
$parcel$export(module.exports, "useLogto", () => $ccb423956ca75d68$export$44fc9df4d2a1789a);
|
|
33
35
|
$parcel$export(module.exports, "useHandleSignInCallback", () => $ccb423956ca75d68$export$84e88c4b3c082374);
|
|
34
36
|
|
|
@@ -54,14 +56,23 @@ const $52a461056f85891e$export$e5bf247804b97da7 = /*#__PURE__*/ (0, $lYn3l$react
|
|
|
54
56
|
|
|
55
57
|
|
|
56
58
|
const $00c2be589188321f$export$bfa587c2c3245948 = ({ config: config , children: children })=>{
|
|
57
|
-
const [loadingCount, setLoadingCount] = (0, $lYn3l$react.useState)(
|
|
59
|
+
const [loadingCount, setLoadingCount] = (0, $lYn3l$react.useState)(1);
|
|
58
60
|
const memorizedLogtoClient = (0, $lYn3l$react.useMemo)(()=>({
|
|
59
61
|
logtoClient: new (0, ($parcel$interopDefault($lYn3l$logtobrowser)))(config)
|
|
60
62
|
}), [
|
|
61
63
|
config
|
|
62
64
|
]);
|
|
63
|
-
const [isAuthenticated, setIsAuthenticated] = (0, $lYn3l$react.useState)(
|
|
65
|
+
const [isAuthenticated, setIsAuthenticated] = (0, $lYn3l$react.useState)(false);
|
|
64
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
|
+
]);
|
|
65
76
|
const memorizedContextValue = (0, $lYn3l$react.useMemo)(()=>({
|
|
66
77
|
...memorizedLogtoClient,
|
|
67
78
|
isAuthenticated: isAuthenticated,
|
|
@@ -113,7 +124,7 @@ const $ccb423956ca75d68$var$useErrorHandler = ()=>{
|
|
|
113
124
|
};
|
|
114
125
|
};
|
|
115
126
|
const $ccb423956ca75d68$export$84e88c4b3c082374 = (callback)=>{
|
|
116
|
-
const { logtoClient: logtoClient , isAuthenticated: isAuthenticated , error:
|
|
127
|
+
const { logtoClient: logtoClient , isAuthenticated: isAuthenticated , error: error , setIsAuthenticated: setIsAuthenticated } = (0, $lYn3l$react.useContext)((0, $52a461056f85891e$export$e5bf247804b97da7));
|
|
117
128
|
const { isLoading: isLoading , setLoadingState: setLoadingState } = $ccb423956ca75d68$var$useLoadingState();
|
|
118
129
|
const { handleError: handleError } = $ccb423956ca75d68$var$useErrorHandler();
|
|
119
130
|
const callbackRef = (0, $lYn3l$react.useRef)();
|
|
@@ -152,11 +163,11 @@ const $ccb423956ca75d68$export$84e88c4b3c082374 = (callback)=>{
|
|
|
152
163
|
return {
|
|
153
164
|
isLoading: isLoading,
|
|
154
165
|
isAuthenticated: isAuthenticated,
|
|
155
|
-
error:
|
|
166
|
+
error: error
|
|
156
167
|
};
|
|
157
168
|
};
|
|
158
169
|
const $ccb423956ca75d68$export$44fc9df4d2a1789a = ()=>{
|
|
159
|
-
const { logtoClient: logtoClient , loadingCount: loadingCount , isAuthenticated: isAuthenticated , error:
|
|
170
|
+
const { logtoClient: logtoClient , loadingCount: loadingCount , isAuthenticated: isAuthenticated , error: error } = (0, $lYn3l$react.useContext)((0, $52a461056f85891e$export$e5bf247804b97da7));
|
|
160
171
|
const { setLoadingState: setLoadingState } = $ccb423956ca75d68$var$useLoadingState();
|
|
161
172
|
const { handleError: handleError } = $ccb423956ca75d68$var$useErrorHandler();
|
|
162
173
|
const isLoading = loadingCount > 0;
|
|
@@ -167,8 +178,6 @@ const $ccb423956ca75d68$export$44fc9df4d2a1789a = ()=>{
|
|
|
167
178
|
await logtoClient.signIn(redirectUri);
|
|
168
179
|
} catch (error) {
|
|
169
180
|
handleError(error, "Unexpected error occurred while signing in.");
|
|
170
|
-
} finally{
|
|
171
|
-
setLoadingState(false);
|
|
172
181
|
}
|
|
173
182
|
}, [
|
|
174
183
|
logtoClient,
|
|
@@ -193,6 +202,21 @@ const $ccb423956ca75d68$export$44fc9df4d2a1789a = ()=>{
|
|
|
193
202
|
setLoadingState,
|
|
194
203
|
handleError
|
|
195
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
|
+
]);
|
|
196
220
|
const getAccessToken = (0, $lYn3l$react.useCallback)(async (resource)=>{
|
|
197
221
|
if (!logtoClient) return (0, $52a461056f85891e$export$838ead842aa548e7)();
|
|
198
222
|
try {
|
|
@@ -208,10 +232,10 @@ const $ccb423956ca75d68$export$44fc9df4d2a1789a = ()=>{
|
|
|
208
232
|
setLoadingState,
|
|
209
233
|
handleError
|
|
210
234
|
]);
|
|
211
|
-
const getIdTokenClaims = (0, $lYn3l$react.useCallback)(()=>{
|
|
235
|
+
const getIdTokenClaims = (0, $lYn3l$react.useCallback)(async ()=>{
|
|
212
236
|
if (!logtoClient) return (0, $52a461056f85891e$export$838ead842aa548e7)();
|
|
213
237
|
try {
|
|
214
|
-
return logtoClient.getIdTokenClaims();
|
|
238
|
+
return await logtoClient.getIdTokenClaims();
|
|
215
239
|
} catch {
|
|
216
240
|
// Do nothing if any exception occurs. Caller will get undefined value.
|
|
217
241
|
}
|
|
@@ -222,9 +246,10 @@ const $ccb423956ca75d68$export$44fc9df4d2a1789a = ()=>{
|
|
|
222
246
|
return {
|
|
223
247
|
isAuthenticated: isAuthenticated,
|
|
224
248
|
isLoading: isLoading,
|
|
225
|
-
error:
|
|
249
|
+
error: error,
|
|
226
250
|
signIn: signIn,
|
|
227
251
|
signOut: signOut,
|
|
252
|
+
fetchUserInfo: fetchUserInfo,
|
|
228
253
|
getAccessToken: getAccessToken,
|
|
229
254
|
getIdTokenClaims: getIdTokenClaims
|
|
230
255
|
};
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;ACAA;;;ACAA;AAaO,MAAM,yCAAiB,GAAG,IAAa;IAC5C,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;CACjE,AAAC;AAEK,MAAM,yCAAY,iBAAG,CAAA,GAAA,0BAAa,CAAA,CAAoB;IAC3D,WAAW,EAAE,SAAS;IACtB,eAAe,EAAE,KAAK;IACtB,YAAY,EAAE,CAAC;IACf,KAAK,EAAE,SAAS;IAChB,kBAAkB,EAAE,yCAAiB;IACrC,eAAe,EAAE,yCAAiB;IAClC,QAAQ,EAAE,yCAAiB;CAC5B,CAAC,AAAC;;;ADfI,MAAM,yCAAa,GAAG,CAAC,UAAE,MAAM,CAAA,YAAE,QAAQ,CAAA,EAAsB,GAAK;IACzE,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,CAAA,GAAA,qBAAQ,CAAA,CAAC,CAAC,CAAC,AAAC;IACpD,MAAM,oBAAoB,GAAG,CAAA,GAAA,oBAAO,CAAA,CAAC,IAAO,CAAA;YAAE,WAAW,EAAE,IAAI,CAAA,GAAA,6CAAW,CAAA,CAAC,MAAM,CAAC;SAAE,CAAA,AAAC,EAAE;QAAC,MAAM;KAAC,CAAC,AAAC;IACjG,MAAM,CAAC,eAAe,EAAE,kBAAkB,CAAC,GAAG,CAAA,GAAA,qBAAQ,CAAA,CACpD,oBAAoB,CAAC,WAAW,CAAC,eAAe,CACjD,AAAC;IACF,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,CAAA,GAAA,qBAAQ,CAAA,EAAS,AAAC;IAC5C,MAAM,qBA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,IAAI;YACF,OAAO,WAAW,CAAC,gBAAgB,EAAE,CAAC;SACvC,CAAC,OAAM;QACN,uEAAuE;SACxE;KACF,EAAE;QAAC,WAAW;KAAC,CAAC,AAAC;IAElB,IAAI,CAAC,WAAW,EACd,OAAO,CAAA,GAAA,yCAAiB,CAAA,EAAE,CAAC;IAG7B,OAAO;yBACL,eAAe;mBACf,SAAS;QACT,KAAK,EAAL,MAAK;gBACL,MAAM;iBACN,OAAO;wBACP,cAAc;0BACd,gBAAgB;KACjB,CAAC;CACH,AAAC;;","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 LogtoErrorCode,\n LogtoClientErrorCode,\n} from '@logto/browser';\n\nexport { LogtoError, LogtoClientError, OidcError, Prompt } from '@logto/browser';\n\nexport * from './provider';\n\nexport { useLogto, useHandleSignInCallback } from './hooks';\n","import LogtoClient, { LogtoConfig } from '@logto/browser';\nimport React, { ReactNode, 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(0);\n const memorizedLogtoClient = useMemo(() => ({ logtoClient: new LogtoClient(config) }), [config]);\n const [isAuthenticated, setIsAuthenticated] = useState(\n memorizedLogtoClient.logtoClient.isAuthenticated\n );\n const [error, setError] = useState<Error>();\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 } 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 getAccessToken: (resource?: string) => Promise<string | undefined>;\n getIdTokenClaims: () => IdTokenClaims | undefined;\n signIn: (redirectUri: string) => 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) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n await logtoClient.signIn(redirectUri);\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while signing in.');\n } finally {\n setLoadingState(false);\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 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(() => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n return 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 getAccessToken,\n getIdTokenClaims,\n };\n};\n\nexport { useLogto, useHandleSignInCallback };\n"],"names":[],"version":3,"file":"index.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;ACAA;;;ACAA;AAaO,MAAM,yCAAiB,GAAG,IAAa;IAC5C,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;CACjE,AAAC;AAEK,MAAM,yCAAY,iBAAG,CAAA,GAAA,0BAAa,CAAA,CAAoB;IAC3D,WAAW,EAAE,SAAS;IACtB,eAAe,EAAE,KAAK;IACtB,YAAY,EAAE,CAAC;IACf,KAAK,EAAE,SAAS;IAChB,kBAAkB,EAAE,yCAAiB;IACrC,eAAe,EAAE,yCAAiB;IAClC,QAAQ,EAAE,yCAAiB;CAC5B,CAAC,AAAC;;;ADfI,MAAM,yCAAa,GAAG,CAAC,UAAE,MAAM,CAAA,YAAE,QAAQ,CAAA,EAAsB,GAAK;IACzE,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,CAAA,GAAA,qBAAQ,CAAA,CAAC,CAAC,CAAC,AAAC;IACpD,MAAM,oBAAoB,GAAG,CAAA,GAAA,oBAAO,CAAA,CAAC,IAAO,CAAA;YAAE,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AAC;IAG7B,OAAO;yBACL,eAAe;mBACf,SAAS;eACT,KAAK;gBACL,MAAM;iBACN,OAAO;uBACP,aAAa;wBACb,cAAc;0BACd,gBAAgB;KACjB,CAAC;CACH,AAAC;;","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} 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, 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) => 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) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n await logtoClient.signIn(redirectUri);\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.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
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} from "@logto/browser";
|
|
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
2
|
import {jsx as $19DtJ$jsx} from "react/jsx-runtime";
|
|
3
|
-
import {useState as $19DtJ$useState, useMemo as $19DtJ$useMemo, createContext as $19DtJ$createContext, useContext as $19DtJ$useContext, useCallback as $19DtJ$useCallback, useRef as $19DtJ$useRef
|
|
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
4
|
|
|
5
5
|
function $parcel$export(e, n, v, s) {
|
|
6
6
|
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
@@ -28,14 +28,23 @@ const $e48cc35865e6f365$export$e5bf247804b97da7 = /*#__PURE__*/ (0, $19DtJ$creat
|
|
|
28
28
|
|
|
29
29
|
|
|
30
30
|
const $8097f9b4a6c197ce$export$bfa587c2c3245948 = ({ config: config , children: children })=>{
|
|
31
|
-
const [loadingCount, setLoadingCount] = (0, $19DtJ$useState)(
|
|
31
|
+
const [loadingCount, setLoadingCount] = (0, $19DtJ$useState)(1);
|
|
32
32
|
const memorizedLogtoClient = (0, $19DtJ$useMemo)(()=>({
|
|
33
33
|
logtoClient: new (0, $19DtJ$logtobrowser)(config)
|
|
34
34
|
}), [
|
|
35
35
|
config
|
|
36
36
|
]);
|
|
37
|
-
const [isAuthenticated, setIsAuthenticated] = (0, $19DtJ$useState)(
|
|
37
|
+
const [isAuthenticated, setIsAuthenticated] = (0, $19DtJ$useState)(false);
|
|
38
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
|
+
]);
|
|
39
48
|
const memorizedContextValue = (0, $19DtJ$useMemo)(()=>({
|
|
40
49
|
...memorizedLogtoClient,
|
|
41
50
|
isAuthenticated: isAuthenticated,
|
|
@@ -87,7 +96,7 @@ const $9cc626600a847be2$var$useErrorHandler = ()=>{
|
|
|
87
96
|
};
|
|
88
97
|
};
|
|
89
98
|
const $9cc626600a847be2$export$84e88c4b3c082374 = (callback)=>{
|
|
90
|
-
const { logtoClient: logtoClient , isAuthenticated: isAuthenticated , error:
|
|
99
|
+
const { logtoClient: logtoClient , isAuthenticated: isAuthenticated , error: error , setIsAuthenticated: setIsAuthenticated } = (0, $19DtJ$useContext)((0, $e48cc35865e6f365$export$e5bf247804b97da7));
|
|
91
100
|
const { isLoading: isLoading , setLoadingState: setLoadingState } = $9cc626600a847be2$var$useLoadingState();
|
|
92
101
|
const { handleError: handleError } = $9cc626600a847be2$var$useErrorHandler();
|
|
93
102
|
const callbackRef = (0, $19DtJ$useRef)();
|
|
@@ -126,11 +135,11 @@ const $9cc626600a847be2$export$84e88c4b3c082374 = (callback)=>{
|
|
|
126
135
|
return {
|
|
127
136
|
isLoading: isLoading,
|
|
128
137
|
isAuthenticated: isAuthenticated,
|
|
129
|
-
error:
|
|
138
|
+
error: error
|
|
130
139
|
};
|
|
131
140
|
};
|
|
132
141
|
const $9cc626600a847be2$export$44fc9df4d2a1789a = ()=>{
|
|
133
|
-
const { logtoClient: logtoClient , loadingCount: loadingCount , isAuthenticated: isAuthenticated , error:
|
|
142
|
+
const { logtoClient: logtoClient , loadingCount: loadingCount , isAuthenticated: isAuthenticated , error: error } = (0, $19DtJ$useContext)((0, $e48cc35865e6f365$export$e5bf247804b97da7));
|
|
134
143
|
const { setLoadingState: setLoadingState } = $9cc626600a847be2$var$useLoadingState();
|
|
135
144
|
const { handleError: handleError } = $9cc626600a847be2$var$useErrorHandler();
|
|
136
145
|
const isLoading = loadingCount > 0;
|
|
@@ -141,8 +150,6 @@ const $9cc626600a847be2$export$44fc9df4d2a1789a = ()=>{
|
|
|
141
150
|
await logtoClient.signIn(redirectUri);
|
|
142
151
|
} catch (error) {
|
|
143
152
|
handleError(error, "Unexpected error occurred while signing in.");
|
|
144
|
-
} finally{
|
|
145
|
-
setLoadingState(false);
|
|
146
153
|
}
|
|
147
154
|
}, [
|
|
148
155
|
logtoClient,
|
|
@@ -167,6 +174,21 @@ const $9cc626600a847be2$export$44fc9df4d2a1789a = ()=>{
|
|
|
167
174
|
setLoadingState,
|
|
168
175
|
handleError
|
|
169
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
|
+
]);
|
|
170
192
|
const getAccessToken = (0, $19DtJ$useCallback)(async (resource)=>{
|
|
171
193
|
if (!logtoClient) return (0, $e48cc35865e6f365$export$838ead842aa548e7)();
|
|
172
194
|
try {
|
|
@@ -182,10 +204,10 @@ const $9cc626600a847be2$export$44fc9df4d2a1789a = ()=>{
|
|
|
182
204
|
setLoadingState,
|
|
183
205
|
handleError
|
|
184
206
|
]);
|
|
185
|
-
const getIdTokenClaims = (0, $19DtJ$useCallback)(()=>{
|
|
207
|
+
const getIdTokenClaims = (0, $19DtJ$useCallback)(async ()=>{
|
|
186
208
|
if (!logtoClient) return (0, $e48cc35865e6f365$export$838ead842aa548e7)();
|
|
187
209
|
try {
|
|
188
|
-
return logtoClient.getIdTokenClaims();
|
|
210
|
+
return await logtoClient.getIdTokenClaims();
|
|
189
211
|
} catch {
|
|
190
212
|
// Do nothing if any exception occurs. Caller will get undefined value.
|
|
191
213
|
}
|
|
@@ -196,9 +218,10 @@ const $9cc626600a847be2$export$44fc9df4d2a1789a = ()=>{
|
|
|
196
218
|
return {
|
|
197
219
|
isAuthenticated: isAuthenticated,
|
|
198
220
|
isLoading: isLoading,
|
|
199
|
-
error:
|
|
221
|
+
error: error,
|
|
200
222
|
signIn: signIn,
|
|
201
223
|
signOut: signOut,
|
|
224
|
+
fetchUserInfo: fetchUserInfo,
|
|
202
225
|
getAccessToken: getAccessToken,
|
|
203
226
|
getIdTokenClaims: getIdTokenClaims
|
|
204
227
|
};
|
|
@@ -207,5 +230,5 @@ const $9cc626600a847be2$export$44fc9df4d2a1789a = ()=>{
|
|
|
207
230
|
|
|
208
231
|
|
|
209
232
|
|
|
210
|
-
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, $9cc626600a847be2$export$44fc9df4d2a1789a as useLogto, $9cc626600a847be2$export$84e88c4b3c082374 as useHandleSignInCallback, $8097f9b4a6c197ce$export$bfa587c2c3245948 as LogtoProvider};
|
|
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};
|
|
211
234
|
//# sourceMappingURL=module.js.map
|
package/lib/module.js.map
CHAN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7B,OAAO;yBACL,eAAe;mBACf,SAAS;QACT,KAAK,EAAL,MAAK;gBACL,MAAM;iBACN,OAAO;wBACP,cAAc;0BACd,gBAAgB;KACjB,CAAC;CACH,AAAC;;","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 LogtoErrorCode,\n LogtoClientErrorCode,\n} from '@logto/browser';\n\nexport { LogtoError, LogtoClientError, OidcError, Prompt } from '@logto/browser';\n\nexport * from './provider';\n\nexport { useLogto, useHandleSignInCallback } from './hooks';\n","import LogtoClient, { LogtoConfig } from '@logto/browser';\nimport React, { ReactNode, 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(0);\n const memorizedLogtoClient = useMemo(() => ({ logtoClient: new LogtoClient(config) }), [config]);\n const [isAuthenticated, setIsAuthenticated] = useState(\n memorizedLogtoClient.logtoClient.isAuthenticated\n );\n const [error, setError] = useState<Error>();\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 } 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 getAccessToken: (resource?: string) => Promise<string | undefined>;\n getIdTokenClaims: () => IdTokenClaims | undefined;\n signIn: (redirectUri: string) => 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) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n await logtoClient.signIn(redirectUri);\n } catch (error: unknown) {\n handleError(error, 'Unexpected error occurred while signing in.');\n } finally {\n setLoadingState(false);\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 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(() => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n return 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 getAccessToken,\n getIdTokenClaims,\n };\n};\n\nexport { useLogto, useHandleSignInCallback };\n"],"names":[],"version":3,"file":"module.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;AAAA;;;;ACAA;;;ACAA;AAaO,MAAM,yCAAiB,GAAG,IAAa;IAC5C,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;CACjE,AAAC;AAEK,MAAM,yCAAY,iBAAG,CAAA,GAAA,oBAAa,CAAA,CAAoB;IAC3D,WAAW,EAAE,SAAS;IACtB,eAAe,EAAE,KAAK;IACtB,YAAY,EAAE,CAAC;IACf,KAAK,EAAE,SAAS;IAChB,kBAAkB,EAAE,yCAAiB;IACrC,eAAe,EAAE,yCAAiB;IAClC,QAAQ,EAAE,yCAAiB;CAC5B,CAAC,AAAC;;;ADfI,MAAM,yCAAa,GAAG,CAAC,UAAE,MAAM,CAAA,YAAE,QAAQ,CAAA,EAAsB,GAAK;IACzE,MAAM,CAAC,YAAY,EAAE,eAAe,CAAC,GAAG,CAAA,GAAA,eAAQ,CAAA,CAAC,CAAC,CAAC,AAAC;IACpD,MAAM,oBAAoB,GAAG,CAAA,GAAA,cAAO,CAAA,CAAC,IAAO,CAAA;YAAE,WAAW,EAAE,IAAI,CAAA,GAAA,mBAAW,CAAA,CAAC,MAAM,CAAC;SAAE,CAAA,AAAC,EAAE;QAAC,MAAM;KAAC,CAAC,AAAC;IACjG,MAAM,CAAC,eAAe,EAAE,kBAAkB,CAAC,GAAG,CAAA,GAAA,eAAQ,CAAA,CAAC,KAAK,CAAC,AAAC;IAC9D,MAAM,CAAC,KAAK,E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EAAW;YACvB,WAAW,CAAC,KAAK,EAAE,qDAAqD,CAAC,CAAC;SAC3E,QAAS;YACR,eAAe,CAAC,KAAK,CAAC,CAAC;SACxB;KACF,EAAE;QAAC,WAAW;QAAE,eAAe;QAAE,WAAW;KAAC,CAAC,AAAC;IAEhD,MAAM,cAAc,GAAG,CAAA,GAAA,kBAAW,CAAA,CAChC,OAAO,QAAiB,GAAK;QAC3B,IAAI,CAAC,WAAW,EACd,OAAO,CAAA,GAAA,yCAAiB,CAAA,EAAE,CAAC;QAG7B,IAAI;YACF,eAAe,CAAC,IAAI,CAAC,CAAC;YAEtB,OAAO,MAAM,WAAW,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;SACnD,CAAC,OAAO,KAAK,EAAW;YACvB,WAAW,CAAC,KAAK,EAAE,uDAAuD,CAAC,CAAC;SAC7E,QAAS;YACR,eAAe,CAAC,KAAK,CAAC,CAAC;SACxB;KACF,EACD;QAAC,WAAW;QAAE,eAAe;QAAE,WAAW;KAAC,CAC5C,AAAC;IAEF,MAAM,gBAAgB,GAAG,CAAA,GAAA,kBAAW,CAAA,CAAC,UAAY;QAC/C,IAAI,CAAC,WAAW,EACd,OAAO,CAAA,GAAA,yCAAiB,CAAA,EAAE,CAAC;QAG7B,IAAI;YACF,OAAO,MAAM,WAAW,CAAC,gBAAgB,EAAE,CAAC;SAC7C,CAAC,OAAM;QACN,uEAAuE;SACxE;KACF,EAAE;QAAC,WAAW;KAAC,CAAC,AAAC;IAElB,IAAI,CAAC,WAAW,EACd,OAAO,CAAA,GAAA,yCAAiB,CAAA,EAAE,CAAC;IAG7B,OAAO;yBACL,eAAe;mBACf,SAAS;eACT,KAAK;gBACL,MAAM;iBACN,OAAO;uBACP,aAAa;wBACb,cAAc;0BACd,gBAAgB;KACjB,CAAC;CACH,AAAC;;","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} 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, 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) => 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) => {\n if (!logtoClient) {\n return throwContextError();\n }\n\n try {\n setLoadingState(true);\n\n await logtoClient.signIn(redirectUri);\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.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@logto/react",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-beta.10",
|
|
4
4
|
"source": "./src/index.ts",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"exports": {
|
|
@@ -29,31 +29,31 @@
|
|
|
29
29
|
"prepack": "pnpm test"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@logto/browser": "^1.0.0-
|
|
33
|
-
"@silverhand/essentials": "^1.1
|
|
32
|
+
"@logto/browser": "^1.0.0-beta.10",
|
|
33
|
+
"@silverhand/essentials": "^1.2.1"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@jest/types": "^27.5.1",
|
|
37
|
-
"@parcel/core": "^2.
|
|
38
|
-
"@parcel/packager-ts": "^2.
|
|
39
|
-
"@parcel/transformer-typescript-types": "^2.
|
|
40
|
-
"@silverhand/eslint-config": "^0.
|
|
41
|
-
"@silverhand/eslint-config-react": "^0.
|
|
42
|
-
"@silverhand/ts-config": "^0.
|
|
43
|
-
"@silverhand/ts-config-react": "^0.
|
|
37
|
+
"@parcel/core": "^2.7.0",
|
|
38
|
+
"@parcel/packager-ts": "^2.7.0",
|
|
39
|
+
"@parcel/transformer-typescript-types": "^2.7.0",
|
|
40
|
+
"@silverhand/eslint-config": "^1.0.0",
|
|
41
|
+
"@silverhand/eslint-config-react": "^1.0.0",
|
|
42
|
+
"@silverhand/ts-config": "^1.0.0",
|
|
43
|
+
"@silverhand/ts-config-react": "^1.0.0",
|
|
44
44
|
"@testing-library/react-hooks": "^8.0.0",
|
|
45
45
|
"@types/jest": "^27.4.1",
|
|
46
46
|
"@types/react": "^17.0.39",
|
|
47
|
-
"eslint": "^8.
|
|
47
|
+
"eslint": "^8.23.0",
|
|
48
48
|
"jest": "^27.5.1",
|
|
49
49
|
"lint-staged": "^13.0.0",
|
|
50
|
-
"parcel": "^2.
|
|
50
|
+
"parcel": "^2.7.0",
|
|
51
51
|
"postcss": "^8.4.6",
|
|
52
|
-
"prettier": "^2.
|
|
52
|
+
"prettier": "^2.7.1",
|
|
53
53
|
"react": "^17.0.2",
|
|
54
|
-
"stylelint": "^14.
|
|
54
|
+
"stylelint": "^14.11.0",
|
|
55
55
|
"ts-jest": "^27.0.4",
|
|
56
|
-
"typescript": "
|
|
56
|
+
"typescript": "4.7.4"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"react": ">=16.8.0"
|
|
@@ -65,5 +65,5 @@
|
|
|
65
65
|
"publishConfig": {
|
|
66
66
|
"access": "public"
|
|
67
67
|
},
|
|
68
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "c79c3759b7f15c44bf5b476dcebd82e9c96e9dd8"
|
|
69
69
|
}
|