0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

feat(docs): init React SDK tutorial (#516)

This commit is contained in:
Gao Sun 2022-04-08 13:47:31 +08:00 committed by GitHub
parent a6e0d19766
commit c1ab443e41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 342 additions and 0 deletions

View file

@ -0,0 +1,3 @@
{
"label": "Integrate SDK"
}

View file

@ -0,0 +1,3 @@
{
"label": "React"
}

View file

@ -0,0 +1,43 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
## Install SDK
_Option 1: Install your SDK dependency_
<Tabs>
<TabItem value="npm" label="npm">
```bash
npm i @logto/react
```
</TabItem>
<TabItem value="yarn" label="Yarn">
```bash
yarn add @logto/react
```
</TabItem>
<TabItem value="pnpm" label="pnpm">
```bash
pnpm add @logto/react
```
</TabItem>
</Tabs>
_Option 2: Add script tag to your HTML_
```html
<script src="https://logto.io/js/logto-sdk-react/0.1.0/logto-sdk-react.production.js" />
```
_Option 3: Fork your own from GitHub_
```bash
git clone https://github.com/logto-io/js.git
pnpm build
```

View file

@ -0,0 +1,33 @@
## Initiate LogtoClient
Add the following code to your main html file. You may need client ID and authorization domain.
```tsx
import { LogtoProvider, LogtoConfig } from '@logto/react';
import React from 'react';
//...
const App = () => {
const config: LogtoConfig = { clientId: 'foo', endpoint: 'https://your-endpoint-domain.com' }
return (
<BrowserRouter>
<LogtoProvider config={config}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/callback" element={<Callback />} />
<Route
path="/protected-resource"
element={
<RequireAuth>
<ProtectedResource />
</RequireAuth>
}
/>
</Routes>
</LogtoProvider>
</BrowserRouter>
);
};
```

View file

@ -0,0 +1,45 @@
## Sign In
### Setup your login
The Logto React SDK provides you tools and hooks to quickly implement your own authorization flow. First, lets enter your redirect URI
```redirectUris
Redirect URI
```
Add the following code to your web app
```typescript
import React from "react";
import { useLogto } from '@logto/react';
const SignInButton = () => {
const { signIn } = useLogto();
const redirectUrl = window.location.origin + '/callback';
return <button onClick={() => signIn(redirectUrl)}>Sign In</button>;
};
export default SignInButton;
```
### Retrieve Auth Status
```tsx
import React from "react";
import { useLogto } from '@logto/react';
const App = () => {
const { isAuthenticated, signIn } = useLogto();
if !(isAuthenticated) {
return <SignInButton />
}
return <>
<AppContent />
<SignOutButton />
</>
};
```

View file

@ -0,0 +1,24 @@
## Sign Out
Execute signOut() methods will redirect users to the Logto sign out page. After a success sign out, all use session data and auth status will be cleared.
```postLogoutRedirectUris
Post sign out redirect URI
```
Add the following code to your web app
```tsx
import React from "react";
import { useLogto } from '@logto/react';
const SignOutButton = () => {
const { signOut } = useLogto();
return (
<button onClick={() => signOut(window.location.origin)}>Sign out</button>
);
};
export default SignOutButton;
```

View file

@ -0,0 +1,5 @@
## Further Readings
- [SDK Documentation](https://link-url-here.org)
- [OIDC Documentation](https://link-url-here.org)
- [Calling API to fetch accessToken](https://link-url-here.org)

View file

@ -0,0 +1,13 @@
import Step1 from './_step-1.mdx'
import Step2 from './_step-2.md'
import Step3 from './_step-3.md'
import Step4 from './_step-4.md'
import Step5 from './_step-5.md'
# Integrate `@logto/react`
<Step1 />
<Step2 />
<Step3 />
<Step4 />
<Step5 />

View file

@ -0,0 +1,10 @@
{
"version.label": {
"message": "下一个",
"description": "The label for version current"
},
"sidebar.tutorialSidebar.category.Integrate SDK": {
"message": "集成 SDK",
"description": "The label for category Integrate SDK in sidebar tutorialSidebar"
}
}

View file

@ -0,0 +1,43 @@
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
## 安装 Logto SDK
_方案 1: 安装 SDK 依赖包_
<Tabs>
<TabItem value="npm" label="npm">
```bash
npm i @logto/react
```
</TabItem>
<TabItem value="yarn" label="Yarn">
```bash
yarn add @logto/react
```
</TabItem>
<TabItem value="pnpm" label="pnpm">
```bash
pnpm add @logto/react
```
</TabItem>
</Tabs>
_方案 2: Add script tag to your HTML_
```html
<script src="https://logto.io/js/logto-sdk-react/0.1.0/logto-sdk-react.production.js" />
```
_方案 3: Fork your own from GitHub_
```bash
git clone https://github.com/logto-io/js.git
pnpm build
```

View file

@ -0,0 +1,33 @@
## 初始化 LogtoClient
Add the following code to your main html file. You may need client ID and authorization domain.
```tsx
import { LogtoProvider, LogtoConfig } from '@logto/react';
import React from 'react';
...
const App = () => {
const config: LogtoConfig = { clientId: 'foo', endpoint: 'https://your-endpoint-domain.com' }
return (
<BrowserRouter>
<LogtoProvider config={config}>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/callback" element={<Callback />} />
<Route
path="/protected-resource"
element={
<RequireAuth>
<ProtectedResource />
</RequireAuth>
}
/>
</Routes>
</LogtoProvider>
</BrowserRouter>
);
};
```

View file

@ -0,0 +1,45 @@
## 登录
### Setup your login
The Logto React SDK provides you tools and hooks to quickly implement your own authorization flow. First, lets enter your redirect URI
```redirectUris
Redirect URI
```
Add the following code to your web app
```tsx
import React from "react";
import { useLogto } from '@logto/react';
const SignInButton = () => {
const { signIn } = useLogto();
const redirectUrl = window.location.origin + '/callback';
return <button onClick={() => signIn(redirectUrl)}>Sign In</button>;
};
export default SignInButton;
```
### Retrieve Auth Status
```tsx
import React from "react";
import { useLogto } from '@logto/react';
const App = () => {
const { isAuthenticated, signIn } = useLogto();
if !(isAuthenticated) {
return <SignInButton />
}
return <>
<AppContent />
<SignOutButton />
</>
};
```

View file

@ -0,0 +1,24 @@
## 登出
Execute signOut() methods will redirect users to the Logto sign out page. After a success sign out, all use session data and auth status will be cleared.
```postLogoutRedirectUris
Post sign out redirect URI
```
Add the following code to your web app
```tsx
import React from "react";
import { useLogto } from '@logto/react';
const SignOutButton = () => {
const { signOut } = useLogto();
return (
<button onClick={() => signOut(window.location.origin)}>Sign out</button>
);
};
export default SignOutButton;
```

View file

@ -0,0 +1,5 @@
## Further Readings
- [SDK Documentation](https://link-url-here.org)
- [OIDC Documentation](https://link-url-here.org)
- [Calling API to fetch accessToken](https://link-url-here.org)

View file

@ -0,0 +1,13 @@
import Step1 from './_step-1.mdx'
import Step2 from './_step-2.md'
import Step3 from './_step-3.md'
import Step4 from './_step-4.md'
import Step5 from './_step-5.md'
# 集成 `@logto/react`
<Step1 />
<Step2 />
<Step3 />
<Step4 />
<Step5 />