mirror of
https://github.com/logto-io/logto.git
synced 2024-12-30 20:33:54 -05:00
refactor(console): separate mdx usages from docs
This commit is contained in:
parent
631a8f0f49
commit
c1b6c32d24
11 changed files with 659 additions and 19 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -17,9 +17,6 @@ node_modules
|
||||||
/packages/*/lib
|
/packages/*/lib
|
||||||
/packages/*/dist
|
/packages/*/dist
|
||||||
|
|
||||||
# docs copied to admin console
|
|
||||||
/packages/console/**/*.mdx
|
|
||||||
|
|
||||||
# logs
|
# logs
|
||||||
logs
|
logs
|
||||||
*.log*
|
*.log*
|
||||||
|
|
|
@ -8,11 +8,10 @@
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"preinstall": "npx only-allow pnpm",
|
"preinstall": "npx only-allow pnpm",
|
||||||
"precommit": "lint-staged",
|
"precommit": "lint-staged",
|
||||||
"copyfiles": "copyfiles -u 2 \"../docs/**/*.mdx\" src/assets",
|
"start": "parcel src/index.html",
|
||||||
"start": "pnpm copyfiles && parcel src/index.html",
|
"dev": "PORT=5002 parcel src/index.html --public-url /console --no-cache --hmr-port 6002",
|
||||||
"dev": "pnpm copyfiles && PORT=5002 parcel src/index.html --public-url /console --no-cache --hmr-port 6002",
|
|
||||||
"check": "tsc --noEmit",
|
"check": "tsc --noEmit",
|
||||||
"build": "pnpm check && rm -rf dist && pnpm copyfiles && parcel build src/index.html --no-autoinstall --no-cache --public-url /console",
|
"build": "pnpm check && rm -rf dist && parcel build src/index.html --no-autoinstall --no-cache --public-url /console",
|
||||||
"lint": "eslint --ext .ts --ext .tsx src",
|
"lint": "eslint --ext .ts --ext .tsx src",
|
||||||
"lint:report": "pnpm lint --format json --output-file report.json",
|
"lint:report": "pnpm lint --format json --output-file report.json",
|
||||||
"stylelint": "stylelint \"src/**/*.scss\""
|
"stylelint": "stylelint \"src/**/*.scss\""
|
||||||
|
@ -39,7 +38,6 @@
|
||||||
"@types/react-dom": "^17.0.9",
|
"@types/react-dom": "^17.0.9",
|
||||||
"@types/react-modal": "^3.13.1",
|
"@types/react-modal": "^3.13.1",
|
||||||
"classnames": "^2.3.1",
|
"classnames": "^2.3.1",
|
||||||
"copyfiles": "^2.4.1",
|
|
||||||
"csstype": "^3.0.11",
|
"csstype": "^3.0.11",
|
||||||
"dayjs": "^1.10.5",
|
"dayjs": "^1.10.5",
|
||||||
"dnd-core": "^16.0.0",
|
"dnd-core": "^16.0.0",
|
||||||
|
@ -74,8 +72,7 @@
|
||||||
},
|
},
|
||||||
"alias": {
|
"alias": {
|
||||||
"@/*": "./src/$1",
|
"@/*": "./src/$1",
|
||||||
"@theme/*": "./src/mdx-components/$1",
|
"@mdx/components/*": "./src/mdx-components/$1"
|
||||||
"@components/*": "./src/pages/Guide/components/$1"
|
|
||||||
},
|
},
|
||||||
"eslintConfig": {
|
"eslintConfig": {
|
||||||
"extends": "@silverhand/react"
|
"extends": "@silverhand/react"
|
||||||
|
|
|
@ -0,0 +1,273 @@
|
||||||
|
import Step from '@mdx/components/Step';
|
||||||
|
import Tabs from '@mdx/components/Tabs';
|
||||||
|
import TabItem from '@mdx/components/TabItem';
|
||||||
|
|
||||||
|
<Step
|
||||||
|
title="Install SDK"
|
||||||
|
subtitle="Install Logto Android SDK with Gradle"
|
||||||
|
index={0}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(1)}
|
||||||
|
>
|
||||||
|
|
||||||
|
### Prerequisite
|
||||||
|
* Minimal Android SDK: Level 24 (TBD)
|
||||||
|
|
||||||
|
<Tabs>
|
||||||
|
|
||||||
|
<TabItem value="kotlin" label="Kotlin">
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
dependencies {
|
||||||
|
implementation("io.logto.sdk:android:1.0.0")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
<TabItem value="groovy" label="Groovy">
|
||||||
|
|
||||||
|
```groovy
|
||||||
|
dependencies {
|
||||||
|
implementation 'io.logto.sdk:android:1.0.0'
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
</Step>
|
||||||
|
|
||||||
|
<Step
|
||||||
|
title="Configure"
|
||||||
|
subtitle="Configure your application and LogtoClient"
|
||||||
|
index={1}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(2)}
|
||||||
|
>
|
||||||
|
|
||||||
|
### Configure Redirect URI
|
||||||
|
|
||||||
|
The `Redirect URI` indicates the endpoint that the application used to receive authorization results, which is implemented by the Logto Android SDK internally.
|
||||||
|
|
||||||
|
### Configure Redirect URI in Admin Console
|
||||||
|
|
||||||
|
Add the following value to Redirect URIs on the application settings page.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$(LOGTO_REDIRECT_SCHEME)://$(YOUR_APP_PACKAGE)/callback
|
||||||
|
```
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
- `LOGTO_REDIRECT_SCHEME` should be a custom scheme in the reverse domain format.
|
||||||
|
- Replace `$(LOGTO_REDIRECT_SCHEME)` with your own settings.
|
||||||
|
|
||||||
|
e.g. `io.logto.android://io.logto.sample/callback`
|
||||||
|
|
||||||
|
### Configure Logto Android SDK
|
||||||
|
|
||||||
|
<Tabs>
|
||||||
|
|
||||||
|
<TabItem value="kotlin" label="Kotlin">
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
import io.logto.sdk.android.LogtoClient
|
||||||
|
import io.logto.sdk.android.type.LogtoConfig
|
||||||
|
|
||||||
|
class MainActivity : AppCompatActivity() {
|
||||||
|
val logtoConfig = LogtoConfig(
|
||||||
|
endpoint = "<your-logto-endpoint>",
|
||||||
|
appId = "<your-app-id>",
|
||||||
|
scopes = null,
|
||||||
|
resources = null,
|
||||||
|
usingPersistStorage = true,
|
||||||
|
)
|
||||||
|
|
||||||
|
val logtoClient = LogtoClient(logtoConfig, application)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
<TabItem value="java" label="Java">
|
||||||
|
|
||||||
|
```java
|
||||||
|
import io.logto.sdk.android.LogtoClient;
|
||||||
|
import io.logto.sdk.android.type.LogtoConfig;
|
||||||
|
|
||||||
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
private LogtoClient logtoClient;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
|
LogtoConfig logtoConfig = new LogtoConfig(
|
||||||
|
"<your-logto-endpoint>",
|
||||||
|
"<your-app-id>",
|
||||||
|
null,
|
||||||
|
null,
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
logtoClient = new LogtoClient(logtoConfig, getApplication());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
- `<your-logto-endpoint>` is the logto service running in your machine. If you start logto service in `http://localhost:300` then `<your-logto-endpoint` will be `http://localhost:300`
|
||||||
|
- `<your-app-id>` is the Application ID of your application, which is created in the Admin Console.
|
||||||
|
|
||||||
|
</Step>
|
||||||
|
|
||||||
|
<Step
|
||||||
|
title="Sign In"
|
||||||
|
subtitle="Sin In to your application by Logto and do some extra works"
|
||||||
|
index={2}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(3)}
|
||||||
|
>
|
||||||
|
|
||||||
|
### Perform a signing-in action
|
||||||
|
|
||||||
|
<Tabs>
|
||||||
|
|
||||||
|
<TabItem value="kotlin" label="Kotlin">
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
logtoClient.signInWithBrowser(
|
||||||
|
this,
|
||||||
|
"io.logto.android://io.logto.sample/callback",
|
||||||
|
) { logtoException: LogtoException? ->
|
||||||
|
// custom logic
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
<TabItem value="java" label="Java">
|
||||||
|
|
||||||
|
```java
|
||||||
|
logtoClient.signInWithBrowser(
|
||||||
|
this,
|
||||||
|
"io.logto.android://io.logto.sample/callback",
|
||||||
|
logtoException -> {
|
||||||
|
// custom logic
|
||||||
|
}
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
### Implement your business logic after signing-in.
|
||||||
|
|
||||||
|
<Tabs>
|
||||||
|
|
||||||
|
<TabItem value="kotlin" label="Kotlin">
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
logtoClient.getAccessToken { logtoException: LogtoException?, result: AccessToken? ->
|
||||||
|
// custom logic
|
||||||
|
}
|
||||||
|
|
||||||
|
logtoClient.getIdTokenClaims { logtoException: LogtoException?, result: IdTokenCliams? ->
|
||||||
|
// custom logic
|
||||||
|
}
|
||||||
|
|
||||||
|
logtoClient.fetchUserInfo { logtoException: LogtoException?, userInfoResponse: UserInfoResponse? ->
|
||||||
|
// custom logic
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
<TabItem value="java" label="Java">
|
||||||
|
|
||||||
|
```java
|
||||||
|
logtoClient.getAccessToken((logtoException, accessToken) -> {
|
||||||
|
// custom logic
|
||||||
|
});
|
||||||
|
|
||||||
|
logtoClient.getIdTokenClaims((logtoException, idTokenClaims) -> {
|
||||||
|
// custom logic
|
||||||
|
});
|
||||||
|
|
||||||
|
logtoClient.fetchUserInfo((logtoException, userInfoResponse) -> {
|
||||||
|
// custom logic
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
</Step>
|
||||||
|
|
||||||
|
<Step
|
||||||
|
title="Sign Out"
|
||||||
|
index={3}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(4)}
|
||||||
|
>
|
||||||
|
|
||||||
|
### Perform a signing-out action
|
||||||
|
|
||||||
|
<Tabs>
|
||||||
|
|
||||||
|
<TabItem value="kotlin" label="Kotlin">
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
logtoClient.signOut { logtoException: LogtoException? ->
|
||||||
|
// custom logic
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
<TabItem value="java" label="Java">
|
||||||
|
|
||||||
|
```java
|
||||||
|
logtoClient.signOut(logtoException -> {
|
||||||
|
// custom logic
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
|
||||||
|
</Tabs>
|
||||||
|
|
||||||
|
Notes:
|
||||||
|
|
||||||
|
- `signOut` will always clear local credentials even if errors occurred.
|
||||||
|
|
||||||
|
</Step>
|
||||||
|
|
||||||
|
<Step
|
||||||
|
title="Further Readings"
|
||||||
|
subtitle="3 steps"
|
||||||
|
index={4}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
buttonText="general.done"
|
||||||
|
buttonHtmlType="submit"
|
||||||
|
>
|
||||||
|
|
||||||
|
- Fetch User Info
|
||||||
|
- Configure Social Sign-In
|
||||||
|
- Access Protected API Resources
|
||||||
|
|
||||||
|
</Step>
|
|
@ -0,0 +1,190 @@
|
||||||
|
import MultiTextInputField from '@mdx/components/MultiTextInputField';
|
||||||
|
import Step from '@mdx/components/Step';
|
||||||
|
import Tabs from '@mdx/components/Tabs';
|
||||||
|
import TabItem from '@mdx/components/TabItem';
|
||||||
|
|
||||||
|
<Step
|
||||||
|
title="Install SDK"
|
||||||
|
subtitle="Please select your favorite package manager"
|
||||||
|
index={0}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(1)}
|
||||||
|
>
|
||||||
|
<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>
|
||||||
|
<TabItem value="script" label="script">
|
||||||
|
|
||||||
|
```html
|
||||||
|
<script src="https://logto.io/js/logto-sdk-react/0.1.0/logto-sdk-react.production.js" />
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="git" label="Git">
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://github.com/logto-io/js.git
|
||||||
|
pnpm build
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
</Step>
|
||||||
|
<Step
|
||||||
|
title="Initiate LogtoClient"
|
||||||
|
subtitle="1 step"
|
||||||
|
index={1}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(2)}
|
||||||
|
>
|
||||||
|
|
||||||
|
Add the following code to your main html file. You may need client ID and authorization domain.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
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>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
</Step>
|
||||||
|
<Step
|
||||||
|
title="Sign In"
|
||||||
|
subtitle="2 steps"
|
||||||
|
index={2}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(3)}
|
||||||
|
>
|
||||||
|
|
||||||
|
### Setup your login
|
||||||
|
|
||||||
|
The Logto React SDK provides you tools and hooks to quickly implement your own authorization flow. First, let’s enter your redirect URI
|
||||||
|
|
||||||
|
<MultiTextInputField name="redirectUris" title="Redirect URI" onError={() => props.onError(2)} />
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import React from "react";
|
||||||
|
import { useLogto } from '@logto/react';
|
||||||
|
|
||||||
|
const App = () => {
|
||||||
|
const { isAuthenticated, signIn } = useLogto();
|
||||||
|
|
||||||
|
if !(isAuthenticated) {
|
||||||
|
return <SignInButton />
|
||||||
|
}
|
||||||
|
|
||||||
|
return <>
|
||||||
|
<AppContent />
|
||||||
|
<SignOutButton />
|
||||||
|
</>
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
</Step>
|
||||||
|
<Step
|
||||||
|
title="Sign Out"
|
||||||
|
subtitle="1 step"
|
||||||
|
index={3}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(4)}
|
||||||
|
>
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
<MultiTextInputField name="postLogoutRedirectUris" title="Post sign out redirect URI" onError={() => props.onError(3)} />
|
||||||
|
|
||||||
|
Add the following code to your web app
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
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;
|
||||||
|
```
|
||||||
|
|
||||||
|
</Step>
|
||||||
|
<Step
|
||||||
|
title="Further Readings"
|
||||||
|
subtitle="3 steps"
|
||||||
|
index={4}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
buttonText="general.done"
|
||||||
|
buttonHtmlType="submit"
|
||||||
|
>
|
||||||
|
|
||||||
|
- [SDK Documentation](https://link-url-here.org)
|
||||||
|
- [OIDC Documentation](https://link-url-here.org)
|
||||||
|
- [Calling API to fetch accessToken](https://link-url-here.org)
|
||||||
|
|
||||||
|
</Step>
|
|
@ -0,0 +1,190 @@
|
||||||
|
import MultiTextInputField from '@mdx/components/MultiTextInputField';
|
||||||
|
import Step from '@mdx/components/Step';
|
||||||
|
import Tabs from '@mdx/components/Tabs';
|
||||||
|
import TabItem from '@mdx/components/TabItem';
|
||||||
|
|
||||||
|
<Step
|
||||||
|
title="安装 SDK"
|
||||||
|
subtitle="选择您熟悉的安装方式"
|
||||||
|
index={0}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(1)}
|
||||||
|
>
|
||||||
|
<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>
|
||||||
|
<TabItem value="script" label="script">
|
||||||
|
|
||||||
|
```html
|
||||||
|
<script src="https://logto.io/js/logto-sdk-react/0.1.0/logto-sdk-react.production.js" />
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
<TabItem value="git" label="Git">
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://github.com/logto-io/js.git
|
||||||
|
pnpm build
|
||||||
|
```
|
||||||
|
|
||||||
|
</TabItem>
|
||||||
|
</Tabs>
|
||||||
|
</Step>
|
||||||
|
<Step
|
||||||
|
title="Initiate LogtoClient"
|
||||||
|
subtitle="1 step"
|
||||||
|
index={1}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(2)}
|
||||||
|
>
|
||||||
|
|
||||||
|
在项目的 html 文件中,加入如下代码(需提前准备好 client ID 以及 authorization domain)
|
||||||
|
Add the following code to your main html file. You may need client ID and authorization domain.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
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>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
</Step>
|
||||||
|
<Step
|
||||||
|
title="Sign In"
|
||||||
|
subtitle="2 steps"
|
||||||
|
index={2}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(3)}
|
||||||
|
>
|
||||||
|
|
||||||
|
### Setup your login
|
||||||
|
|
||||||
|
The Logto React SDK provides you tools and hooks to quickly implement your own authorization flow. First, let’s enter your redirect URI
|
||||||
|
|
||||||
|
<MultiTextInputField name="redirectUris" title="Redirect URI" onError={() => props.onError(2)} />
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import React from "react";
|
||||||
|
import { useLogto } from '@logto/react';
|
||||||
|
|
||||||
|
const App = () => {
|
||||||
|
const { isAuthenticated, signIn } = useLogto();
|
||||||
|
|
||||||
|
if !(isAuthenticated) {
|
||||||
|
return <SignInButton />
|
||||||
|
}
|
||||||
|
|
||||||
|
return <>
|
||||||
|
<AppContent />
|
||||||
|
<SignOutButton />
|
||||||
|
</>
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
</Step>
|
||||||
|
<Step
|
||||||
|
title="Sign Out"
|
||||||
|
subtitle="1 step"
|
||||||
|
index={3}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
onNext={() => props.onNext(4)}
|
||||||
|
>
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
<MultiTextInputField name="postLogoutRedirectUris" title="Post sign out redirect URI" onError={() => props.onError(3)} />
|
||||||
|
|
||||||
|
Add the following code to your web app
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
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;
|
||||||
|
```
|
||||||
|
|
||||||
|
</Step>
|
||||||
|
<Step
|
||||||
|
title="Further Readings"
|
||||||
|
subtitle="3 steps"
|
||||||
|
index={4}
|
||||||
|
activeIndex={props.activeStepIndex}
|
||||||
|
invalidIndex={props.invalidStepIndex}
|
||||||
|
buttonHtmlType="submit"
|
||||||
|
>
|
||||||
|
|
||||||
|
- [SDK Documentation](https://link-url-here.org)
|
||||||
|
- [OIDC Documentation](https://link-url-here.org)
|
||||||
|
- [Calling API to fetch accessToken](https://link-url-here.org)
|
||||||
|
|
||||||
|
</Step>
|
|
@ -15,8 +15,8 @@ import IconButton from '@/components/IconButton';
|
||||||
import Spacer from '@/components/Spacer';
|
import Spacer from '@/components/Spacer';
|
||||||
import useApi from '@/hooks/use-api';
|
import useApi from '@/hooks/use-api';
|
||||||
import Close from '@/icons/Close';
|
import Close from '@/icons/Close';
|
||||||
|
import Step from '@/mdx-components/Step';
|
||||||
import SenderTester from '@/pages/ConnectorDetails/components/SenderTester';
|
import SenderTester from '@/pages/ConnectorDetails/components/SenderTester';
|
||||||
import Step from '@/pages/Guide/components/Step';
|
|
||||||
import * as modalStyles from '@/scss/modal.module.scss';
|
import * as modalStyles from '@/scss/modal.module.scss';
|
||||||
import { GuideForm } from '@/types/guide';
|
import { GuideForm } from '@/types/guide';
|
||||||
|
|
||||||
|
|
|
@ -27,12 +27,7 @@ import * as styles from './index.module.scss';
|
||||||
|
|
||||||
const Guides: Record<string, LazyExoticComponent<(props: MDXProps) => JSX.Element>> = {
|
const Guides: Record<string, LazyExoticComponent<(props: MDXProps) => JSX.Element>> = {
|
||||||
react: lazy(async () => import('@/assets/docs/tutorial/integrate-sdk/react.mdx')),
|
react: lazy(async () => import('@/assets/docs/tutorial/integrate-sdk/react.mdx')),
|
||||||
'react_zh-cn': lazy(
|
'react_zh-cn': lazy(async () => import('@/assets/docs/tutorial/integrate-sdk/react_zh-cn.mdx')),
|
||||||
async () =>
|
|
||||||
import(
|
|
||||||
'@/assets/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/react.mdx'
|
|
||||||
)
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
type Props = PropsWithChildren<{
|
type Props = PropsWithChildren<{
|
||||||
|
|
|
@ -456,7 +456,6 @@ importers:
|
||||||
'@types/react-dom': ^17.0.9
|
'@types/react-dom': ^17.0.9
|
||||||
'@types/react-modal': ^3.13.1
|
'@types/react-modal': ^3.13.1
|
||||||
classnames: ^2.3.1
|
classnames: ^2.3.1
|
||||||
copyfiles: ^2.4.1
|
|
||||||
csstype: ^3.0.11
|
csstype: ^3.0.11
|
||||||
dayjs: ^1.10.5
|
dayjs: ^1.10.5
|
||||||
dnd-core: ^16.0.0
|
dnd-core: ^16.0.0
|
||||||
|
@ -510,7 +509,6 @@ importers:
|
||||||
'@types/react-dom': 17.0.11
|
'@types/react-dom': 17.0.11
|
||||||
'@types/react-modal': 3.13.1
|
'@types/react-modal': 3.13.1
|
||||||
classnames: 2.3.1
|
classnames: 2.3.1
|
||||||
copyfiles: 2.4.1
|
|
||||||
csstype: 3.0.11
|
csstype: 3.0.11
|
||||||
dayjs: 1.10.7
|
dayjs: 1.10.7
|
||||||
dnd-core: 16.0.0
|
dnd-core: 16.0.0
|
||||||
|
|
Loading…
Reference in a new issue