@logto/react 0.1.14 → 0.1.17

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 ADDED
@@ -0,0 +1,132 @@
1
+ # Logto React SDK
2
+ [![Version](https://img.shields.io/npm/v/@logto/react)](https://www.npmjs.com/package/@logto/react)
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
+ [![Codecov](https://img.shields.io/codecov/c/github/logto-io/js)](https://app.codecov.io/gh/logto-io/js?branch=master)
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.
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.
9
+
10
+ ## Installation
11
+
12
+ ### Using npm
13
+
14
+ ```bash
15
+ npm install @logto/react
16
+ ```
17
+
18
+ ### Using yarn
19
+
20
+ ```bash
21
+ yarn add @logto/react
22
+ ```
23
+
24
+ ### Using pnpm
25
+
26
+ ```bash
27
+ pnpm install @logto/react
28
+ ```
29
+
30
+ ### Using CDN
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)
39
+
40
+ Check out the source code and try it yourself. (We use [pnpm](https://pnpm.io/) for package management)
41
+
42
+ ```bash
43
+ pnpm i && pnpm start
44
+ ```
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;
126
+ ```
127
+
128
+ ## Resources
129
+
130
+ [![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/)
132
+ [![Discord](https://img.shields.io/discord/965845662535147551?logo=discord&logoColor=ffffff&color=7389D8&cacheSeconds=600)](https://discord.gg/UEPaF3j5e6)
@@ -9,7 +9,7 @@ declare type Logto = {
9
9
  signIn: (redirectUri: string) => Promise<void>;
10
10
  signOut: (postLogoutRedirectUri: string) => Promise<void>;
11
11
  };
12
- declare const useHandleSignInCallback: (returnToPageUrl?: string) => {
12
+ declare const useHandleSignInCallback: (callback?: (() => void) | undefined) => {
13
13
  isLoading: boolean;
14
14
  isAuthenticated: boolean;
15
15
  error: Error | undefined;
@@ -29,10 +29,15 @@ const useErrorHandler = () => {
29
29
  }, [setError]);
30
30
  return { handleError };
31
31
  };
32
- const useHandleSignInCallback = (returnToPageUrl = window.location.origin) => {
33
- const { logtoClient, isAuthenticated, error } = (0, react_1.useContext)(context_1.LogtoContext);
32
+ const useHandleSignInCallback = (callback) => {
33
+ const { logtoClient, isAuthenticated, error, setIsAuthenticated } = (0, react_1.useContext)(context_1.LogtoContext);
34
34
  const { isLoading, setLoadingState } = useLoadingState();
35
35
  const { handleError } = useErrorHandler();
36
+ const callbackRef = (0, react_1.useRef)();
37
+ (0, react_1.useEffect)(() => {
38
+ // eslint-disable-next-line @silverhand/fp/no-mutation
39
+ callbackRef.current = callback; // Update ref to the latest callback.
40
+ }, [callback]);
36
41
  const handleSignInCallback = (0, react_1.useCallback)(async (callbackUri) => {
37
42
  if (!logtoClient) {
38
43
  return (0, context_1.throwContextError)();
@@ -40,10 +45,8 @@ const useHandleSignInCallback = (returnToPageUrl = window.location.origin) => {
40
45
  try {
41
46
  setLoadingState(true);
42
47
  await logtoClient.handleSignInCallback(callbackUri);
43
- // We deliberately do NOT set isAuthenticated to true here, because the app state may change immediately
44
- // even before navigating to the return page URL, which might cause rendering problems.
45
- // Moreover, since the location will be redirected, the isAuthenticated state will not matter any more.
46
- window.location.assign(returnToPageUrl);
48
+ setIsAuthenticated(true);
49
+ callbackRef.current?.();
47
50
  }
48
51
  catch (error) {
49
52
  handleError(error, 'Unexpected error occurred while handling sign in callback.');
@@ -51,10 +54,11 @@ const useHandleSignInCallback = (returnToPageUrl = window.location.origin) => {
51
54
  finally {
52
55
  setLoadingState(false);
53
56
  }
54
- }, [logtoClient, returnToPageUrl, setLoadingState, handleError]);
57
+ }, [logtoClient, setLoadingState, setIsAuthenticated, handleError]);
55
58
  (0, react_1.useEffect)(() => {
56
- if (!isAuthenticated && logtoClient?.isSignInRedirected(window.location.href)) {
57
- void handleSignInCallback(window.location.href);
59
+ const currentPageUrl = window.location.href;
60
+ if (!isAuthenticated && logtoClient?.isSignInRedirected(currentPageUrl)) {
61
+ void handleSignInCallback(currentPageUrl);
58
62
  }
59
63
  }, [handleSignInCallback, isAuthenticated, logtoClient]);
60
64
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logto/react",
3
- "version": "0.1.14",
3
+ "version": "0.1.17",
4
4
  "main": "./lib/index.js",
5
5
  "exports": "./lib/index.js",
6
6
  "typings": "./lib/index.d.ts",
@@ -15,7 +15,6 @@
15
15
  },
16
16
  "scripts": {
17
17
  "dev:tsc": "tsc -p tsconfig.build.json -w --preserveWatchOutput",
18
- "preinstall": "npx only-allow pnpm",
19
18
  "precommit": "lint-staged",
20
19
  "build": "rm -rf lib/ && tsc -p tsconfig.build.json",
21
20
  "lint": "eslint --ext .ts --ext .tsx src",
@@ -24,7 +23,7 @@
24
23
  "prepack": "pnpm test"
25
24
  },
26
25
  "dependencies": {
27
- "@logto/browser": "^0.1.14",
26
+ "@logto/browser": "^0.1.17",
28
27
  "@silverhand/essentials": "^1.1.6"
29
28
  },
30
29
  "devDependencies": {
@@ -56,5 +55,5 @@
56
55
  "publishConfig": {
57
56
  "access": "public"
58
57
  },
59
- "gitHead": "5d05c6bd23dc35d7217031edccc01db2bae33b7b"
58
+ "gitHead": "57e93385829eceb3c1b2ffb92b90901aa174a59a"
60
59
  }