From 637b63e73d1d4ffb156a4033458c6b89b2902e21 Mon Sep 17 00:00:00 2001 From: Charles Zhao Date: Mon, 23 May 2022 19:05:17 +0800 Subject: [PATCH] docs: integrate Vue SDK --- .../docs/tutorial/integrate-sdk/README.md | 2 +- .../docs/docs/tutorial/integrate-sdk/vue.mdx | 168 ++++++++++++++++++ .../integrate-sdk/components/AppNote.tsx | 16 ++ .../integrate-sdk/components/SignInNote.tsx | 16 ++ .../current/tutorial/integrate-sdk/vue.mdx | 167 +++++++++++++++++ 5 files changed, 368 insertions(+), 1 deletion(-) create mode 100644 packages/docs/docs/tutorial/integrate-sdk/vue.mdx create mode 100644 packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/components/AppNote.tsx create mode 100644 packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/components/SignInNote.tsx create mode 100644 packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/vue.mdx diff --git a/packages/docs/docs/tutorial/integrate-sdk/README.md b/packages/docs/docs/tutorial/integrate-sdk/README.md index 5820f817b..49d3d3be2 100644 --- a/packages/docs/docs/tutorial/integrate-sdk/README.md +++ b/packages/docs/docs/tutorial/integrate-sdk/README.md @@ -8,7 +8,7 @@ If the list doesn't cover your desired platform/framework, please file a feature - Vanilla JS - [React](./react) -- Vue +- [Vue](./vue) ## Native diff --git a/packages/docs/docs/tutorial/integrate-sdk/vue.mdx b/packages/docs/docs/tutorial/integrate-sdk/vue.mdx new file mode 100644 index 000000000..5e5a41459 --- /dev/null +++ b/packages/docs/docs/tutorial/integrate-sdk/vue.mdx @@ -0,0 +1,168 @@ +--- +sidebar_label: Vue +--- + +import Admonition from '@theme/Admonition'; +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + +import AppNote from './components/AppNote'; +import SignInNote from './components/SignInNote'; + +# Integrate `@logto/vue` + + + +## Import SDK + + + + +```bash +npm i @logto/vue +``` + + + + +```bash +yarn add @logto/vue +``` + + + + +```bash +pnpm add @logto/vue +``` + + + + +{/* This should be CDN URL */} +```html + + + +``` + +Then we need to link the callback component with the route. Let's say the path is `/callback` and we are using `vue-router`: + +```ts +const router = createRouter({ + routes: [ + { + path: "/callback", + name: "callback", + component: CallbackView, + }, + ] +}); +``` + +### Make a Sign In Button + + + +```ts +import { useLogto } from "@logto/vue"; + +const { signIn } = useLogto(); +const onClickSignIn = () => signIn(redirectUrl); +``` + +```html + +``` + +### Retrieve Authentication Status + +```ts +import { useLogto } from '@logto/vue'; + +const { isAuthenticated } = useLogto(); +``` + +```html +
+ +
+
+ +
+``` + +## Sign Out + +Calling `.signOut()` will clear all the Logto data in memory and LocalStorage, if there is any. + +To make the user come back to your application after signing out, +it's necessary to add `http://localhost:1234` as one of the Post Sign Out URIs and use the URL as the parameter when calling `.signOut()`. + +```ts +import { useLogto } from "@logto/vue"; + +const { signOut } = useLogto(); +const onClickSignOut = () => signOut('http://localhost:1234'); +``` + +```html + +``` + +## 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) diff --git a/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/components/AppNote.tsx b/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/components/AppNote.tsx new file mode 100644 index 000000000..9ff1dc648 --- /dev/null +++ b/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/components/AppNote.tsx @@ -0,0 +1,16 @@ +import React, { ReactNode } from 'react' +import Admonition from '@theme/Admonition'; + +type Props = { + type: ReactNode; +}; + +const AppNote = ({ type }: Props) => { + return ( + + 本篇教程默认你已经在管理界面中成功创建了一个 {type} 应用。如果你还没有完成这一步操作,请参阅这篇教程,创建应用之后再继续。 + + ); +}; + +export default AppNote diff --git a/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/components/SignInNote.tsx b/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/components/SignInNote.tsx new file mode 100644 index 000000000..ad10fb012 --- /dev/null +++ b/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/components/SignInNote.tsx @@ -0,0 +1,16 @@ +import React, { ReactNode } from 'react'; +import Admonition from '@theme/Admonition'; + +type Props = { + calling: ReactNode; +}; + +const SignInNote = ({ calling }: Props) => { + return ( + + 在调用 {calling} 之前,请首先确保已经在管理界面中正确 配置 Redirect URI + + ); +}; + +export default SignInNote; diff --git a/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/vue.mdx b/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/vue.mdx new file mode 100644 index 000000000..8a0a3df26 --- /dev/null +++ b/packages/docs/i18n/zh-cn/docusaurus-plugin-content-docs/current/tutorial/integrate-sdk/vue.mdx @@ -0,0 +1,167 @@ +--- +sidebar_label: Vue +--- + +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + +import AppNote from './components/AppNote'; +import SignInNote from './components/SignInNote'; + +# 集成 `@logto/vue` + + + +## 安装 SDK + + + + +```bash +npm i @logto/vue +``` + + + + +```bash +yarn add @logto/vue +``` + + + + +```bash +pnpm add @logto/vue +``` + + + + +{/* This should be CDN URL */} +```html + + + +``` + +然后我们可以在路由表中添加这个回调路由。假设我们的路由地址定义为 `/callback`,且使用的路由工具为 `vue-router`: + +```ts +const router = createRouter({ + ... + routes: [ + { + path: "/callback", + name: "callback", + component: CallbackView, + }, + ] +}); +``` + +### 创建一个登录按钮 + + + +```ts +import { useLogto } from "@logto/vue"; + +const { signIn } = useLogto(); +const onClickSignIn = () => signIn(redirectUrl); +``` + +```html + +``` + +### 判断当前登录状态 + +```ts +import { useLogto } from '@logto/vue'; + +const { isAuthenticated } = useLogto(); +``` + +```html +
+ +
+
+ +
+``` + +## 登出 + +调用 `.signOut()` 方法会清除所有在缓存或者 localStorage 中的 Logto 数据(如果有)。 + +为了确保用户登出后能够跳转回你的应用,我们需要首先在管理界面中将 `http://localhost:1234` 添加到允许登出后跳转的地址列表(Post Sign Out URIs)中。 + +```ts +import { useLogto } from "@logto/vue"; + +const { signOut } = useLogto(); +const onClickSignOut = () => signOut('http://localhost:1234'); +``` + +```html + +``` + +## 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)