@logto/react 1.0.0-alpha.3 → 1.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -3,9 +3,9 @@
3
3
  [![Build Status](https://github.com/logto-io/js/actions/workflows/main.yml/badge.svg)](https://github.com/logto-io/js/actions/workflows/main.yml)
4
4
  [![Codecov](https://img.shields.io/codecov/c/github/logto-io/js)](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-sdk/react) or [docs](https://docs.logto.io/sdk/react) for more information.
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-sdk/react) and [文档](https://docs.logto.io/zh-cn/sdk/react) in Simplified Chinese.
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 install @logto/react
27
+ pnpm add @logto/react
28
28
  ```
29
29
 
30
- ### Using CDN
30
+ ## Get sample
31
31
 
32
- ```bash
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 yourself. (We use [pnpm](https://pnpm.io/) for package management)
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
  [![Website](https://img.shields.io/badge/website-logto.io-8262F8.svg)](https://logto.io/)
131
- [![Docs](https://img.shields.io/badge/docs-logto.io-green.svg)](https://docs.logto.io/docs/sdk/swift/)
43
+ [![Docs](https://img.shields.io/badge/docs-logto.io-green.svg)](https://docs.logto.io/sdk/JavaScript/react/)
132
44
  [![Discord](https://img.shields.io/discord/965845662535147551?logo=discord&logoColor=ffffff&color=7389D8&cacheSeconds=600)](https://discord.gg/UEPaF3j5e6)
package/lib/index.d.ts CHANGED
@@ -19,7 +19,7 @@ type Logto = {
19
19
  isLoading: boolean;
20
20
  error?: Error;
21
21
  getAccessToken: (resource?: string) => Promise<string | undefined>;
22
- getIdTokenClaims: () => IdTokenClaims | undefined;
22
+ getIdTokenClaims: () => Promise<IdTokenClaims | undefined>;
23
23
  signIn: (redirectUri: string) => Promise<void>;
24
24
  signOut: (postLogoutRedirectUri?: string) => Promise<void>;
25
25
  };
@@ -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,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,gBAqBrE,CAAC;AC1BF,aAAa;IACX,eAAe,EAAE,OAAO,CAAC;IACzB,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,cAAc,EAAE,CAAC,QAAQ,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC;IACnE,gBAAgB,EAAE,MAAM,aAAa,GAAG,SAAS,CAAC;IAClD,MAAM,EAAE,CAAC,WAAW,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C,OAAO,EAAE,CAAC,qBAAqB,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5D,CAAC;AAsCF,OAAA,MAAM,4CAA4C,IAAI;;;;CA4CrD,CAAC;AAEF,OAAA,MAAM,gBAAe,KA6FpB,CAAC;AC5LF,YAAY,EACV,WAAW,EACX,aAAa,EACb,cAAc,EACd,oBAAoB,GACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,EAAE,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 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"],"names":[],"version":3,"file":"index.d.ts.map"}
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,gBA4BrE,CAAC;ACjCF,aAAa;IACX,eAAe,EAAE,OAAO,CAAC;IACzB,SAAS,EAAE,OAAO,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC;IACd,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,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C,OAAO,EAAE,CAAC,qBAAqB,CAAC,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CAC5D,CAAC;AAsCF,OAAA,MAAM,4CAA4C,IAAI;;;;CA4CrD,CAAC;AAEF,OAAA,MAAM,gBAAe,KA6FpB,CAAC;AC5LF,YAAY,EACV,WAAW,EACX,aAAa,EACb,cAAc,EACd,oBAAoB,GACrB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,UAAU,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,EAAE,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 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"],"names":[],"version":3,"file":"index.d.ts.map"}
package/lib/index.js CHANGED
@@ -60,11 +60,19 @@ const $00c2be589188321f$export$bfa587c2c3245948 = ({ config: config , children:
60
60
  }), [
61
61
  config
62
62
  ]);
63
- const [isAuthenticated, setIsAuthenticated] = (0, $lYn3l$react.useState)(memorizedLogtoClient.logtoClient.isAuthenticated);
63
+ const [isAuthenticated1, setIsAuthenticated] = (0, $lYn3l$react.useState)(false);
64
64
  const [error, setError] = (0, $lYn3l$react.useState)();
65
+ (0, $lYn3l$react.useEffect)(()=>{
66
+ (async ()=>{
67
+ const isAuthenticated = await memorizedLogtoClient.logtoClient.isAuthenticated();
68
+ setIsAuthenticated(isAuthenticated);
69
+ })();
70
+ }, [
71
+ memorizedLogtoClient
72
+ ]);
65
73
  const memorizedContextValue = (0, $lYn3l$react.useMemo)(()=>({
66
74
  ...memorizedLogtoClient,
67
- isAuthenticated: isAuthenticated,
75
+ isAuthenticated: isAuthenticated1,
68
76
  setIsAuthenticated: setIsAuthenticated,
69
77
  loadingCount: loadingCount,
70
78
  setLoadingCount: setLoadingCount,
@@ -72,7 +80,7 @@ const $00c2be589188321f$export$bfa587c2c3245948 = ({ config: config , children:
72
80
  setError: setError
73
81
  }), [
74
82
  memorizedLogtoClient,
75
- isAuthenticated,
83
+ isAuthenticated1,
76
84
  loadingCount,
77
85
  error
78
86
  ]);
@@ -208,10 +216,10 @@ const $ccb423956ca75d68$export$44fc9df4d2a1789a = ()=>{
208
216
  setLoadingState,
209
217
  handleError
210
218
  ]);
211
- const getIdTokenClaims = (0, $lYn3l$react.useCallback)(()=>{
219
+ const getIdTokenClaims = (0, $lYn3l$react.useCallback)(async ()=>{
212
220
  if (!logtoClient) return (0, $52a461056f85891e$export$838ead842aa548e7)();
213
221
  try {
214
- return logtoClient.getIdTokenClaims();
222
+ return await logtoClient.getIdTokenClaims();
215
223
  } catch {
216
224
  // Do nothing if any exception occurs. Caller will get undefined value.
217
225
  }
package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AA,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,WAAW,EAAE,IAAI,CAAA,GAAA,6CAAW,CAAA,CAAC,MAAM,CAAC;SAAE,CAAA,AAAC,EAAE;QAAC,MAAM;KAAC,CAAC,AAAC;IACjG,MAAM,CAAC,gBAAe,EAAE,kBAAkB,CAAC,GAAG,CAAA,GA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EtB,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,wBAAW,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;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 { 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(0);\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 })();\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 } 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: () => 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 } 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(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 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
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";
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, useEffect as $19DtJ$useEffect} from "react";
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});
@@ -34,11 +34,19 @@ const $8097f9b4a6c197ce$export$bfa587c2c3245948 = ({ config: config , children:
34
34
  }), [
35
35
  config
36
36
  ]);
37
- const [isAuthenticated, setIsAuthenticated] = (0, $19DtJ$useState)(memorizedLogtoClient.logtoClient.isAuthenticated);
37
+ const [isAuthenticated1, 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
+ })();
44
+ }, [
45
+ memorizedLogtoClient
46
+ ]);
39
47
  const memorizedContextValue = (0, $19DtJ$useMemo)(()=>({
40
48
  ...memorizedLogtoClient,
41
- isAuthenticated: isAuthenticated,
49
+ isAuthenticated: isAuthenticated1,
42
50
  setIsAuthenticated: setIsAuthenticated,
43
51
  loadingCount: loadingCount,
44
52
  setLoadingCount: setLoadingCount,
@@ -46,7 +54,7 @@ const $8097f9b4a6c197ce$export$bfa587c2c3245948 = ({ config: config , children:
46
54
  setError: setError
47
55
  }), [
48
56
  memorizedLogtoClient,
49
- isAuthenticated,
57
+ isAuthenticated1,
50
58
  loadingCount,
51
59
  error
52
60
  ]);
@@ -182,10 +190,10 @@ const $9cc626600a847be2$export$44fc9df4d2a1789a = ()=>{
182
190
  setLoadingState,
183
191
  handleError
184
192
  ]);
185
- const getIdTokenClaims = (0, $19DtJ$useCallback)(()=>{
193
+ const getIdTokenClaims = (0, $19DtJ$useCallback)(async ()=>{
186
194
  if (!logtoClient) return (0, $e48cc35865e6f365$export$838ead842aa548e7)();
187
195
  try {
188
- return logtoClient.getIdTokenClaims();
196
+ return await logtoClient.getIdTokenClaims();
189
197
  } catch {
190
198
  // Do nothing if any exception occurs. Caller will get undefined value.
191
199
  }
package/lib/module.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,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,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;KAAC,CAC5C,AAAC;IAEF,MAAM,gBAAgB,GAAG,CAAA,GAAA,kBAAW,CAAA,CAAC,IAAM;QACzC,IAAI,CAAC,WAAW,EACd,OAAO,CAAA,GAAA,yCAAiB,CAAA,EAAE,CAAC;QAG7B,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":"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,CAA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C,KAAK,CAAC,CAAC;SACxB;KACF,EACD;QAAC,WAAW;QAAE,eAAe;QAAE,WAAW;KAAC,CAC5C,AAAC;IAEF,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;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 { 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(0);\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 })();\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 } 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: () => 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 } 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(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 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-alpha.3",
3
+ "version": "1.0.0-beta.3",
4
4
  "source": "./src/index.ts",
5
5
  "main": "./lib/index.js",
6
6
  "exports": {
@@ -29,7 +29,7 @@
29
29
  "prepack": "pnpm test"
30
30
  },
31
31
  "dependencies": {
32
- "@logto/browser": "^1.0.0-alpha.2",
32
+ "@logto/browser": "^1.0.0-beta.3",
33
33
  "@silverhand/essentials": "^1.1.6"
34
34
  },
35
35
  "devDependencies": {
@@ -37,10 +37,10 @@
37
37
  "@parcel/core": "^2.6.2",
38
38
  "@parcel/packager-ts": "^2.6.2",
39
39
  "@parcel/transformer-typescript-types": "^2.6.2",
40
- "@silverhand/eslint-config": "^0.14.0",
41
- "@silverhand/eslint-config-react": "^0.14.0",
42
- "@silverhand/ts-config": "^0.14.0",
43
- "@silverhand/ts-config-react": "^0.14.0",
40
+ "@silverhand/eslint-config": "^0.17.0",
41
+ "@silverhand/eslint-config-react": "^0.17.0",
42
+ "@silverhand/ts-config": "^0.17.0",
43
+ "@silverhand/ts-config-react": "^0.17.0",
44
44
  "@testing-library/react-hooks": "^8.0.0",
45
45
  "@types/jest": "^27.4.1",
46
46
  "@types/react": "^17.0.39",
@@ -65,5 +65,5 @@
65
65
  "publishConfig": {
66
66
  "access": "public"
67
67
  },
68
- "gitHead": "5fec66d9acf072c7d035676e0d872d3f86dab681"
68
+ "gitHead": "e227f9b92f76ea7e12c2aac3d5e5b24f2b85e7e8"
69
69
  }