diff --git a/packages/console/src/assets/docs/guides/spa-vue/README.mdx b/packages/console/src/assets/docs/guides/spa-vue/README.mdx index 154697da597..2aa12f30b71 100644 --- a/packages/console/src/assets/docs/guides/spa-vue/README.mdx +++ b/packages/console/src/assets/docs/guides/spa-vue/README.mdx @@ -5,6 +5,9 @@ import InlineNotification from '@/ds-components/InlineNotification'; import Steps from '@/mdx-components/Steps'; import Step from '@/mdx-components/Step'; +import Checkpoint from '../../fragments/_checkpoint.md'; +import RedirectUrisWeb from '../../fragments/_redirect-uris-web.mdx'; + - + ```bash -yarn add @logto/vue +pnpm add @logto/vue ``` - + ```bash -pnpm add @logto/vue +yarn add @logto/vue ``` + @@ -41,14 +45,15 @@ pnpm add @logto/vue > - We only support Vue 3 Composition API at this point. Will add support to Vue Options API and - possibly Vue 2 in future releases. + Logto Vue SDK is built with Vue 3 composition API. Therefore, only Vue 3 is supported at the moment. Contact us if you want to add support for Vue 2. Import and use `createLogto` to install Logto plugin: - + {`import { createLogto, LogtoConfig } from '@logto/vue'; +import { createApp } from 'vue'; +import App from './App.vue'; const config: LogtoConfig = { endpoint: '${props.endpoint}', @@ -63,64 +68,25 @@ app.mount("#app");`} - - - - In the following steps, we assume your app is running on http://localhost:3000. - - -### Configure Redirect URI - -First, let’s enter your redirect URI. E.g. `http://localhost:3000/callback`. - - - -### Implement a sign-in button - -We provide two composables `useHandleSignInCallback()` and `useLogto()`, which can help you easily manage the authentication flow. + -Go back to your IDE/editor, use the following code to implement the sign-in button: + -
-
-{``}
-
-
-
+
-```html - -``` + -### Handle redirect +There are still things to do after the user is redirected back to your application from Logto. Let's handle it properly. -We're almost there! In the last step, we use `http://localhost:3000/callback` as the Redirect URI, and now we need to handle it properly. +First let's create a callback page: -First let's create a callback component: +```ts title="views/CallbackView.vue" +import { useHandleSignInCallback } from '@logto/vue'; +import router from '@/router'; -```html - - +const { isLoading } = useHandleSignInCallback(() => { + // Do something when finished, e.g. redirect to home page +}); ``` ```html @@ -130,9 +96,9 @@ First let's create a callback component: ``` -Finally insert the code below to create a `/callback` route which does NOT require authentication: +Insert the code below in your `/callback` route which does NOT require authentication: -```ts +```ts title="router/index.ts" // Assuming vue-router const router = createRouter({ routes: [ @@ -147,80 +113,70 @@ const router = createRouter({ - + -Calling `.signOut()` will clear all the Logto data in memory and localStorage if they exist. +We provide two composables `useHandleSignInCallback()` and `useLogto()` which can help you easily manage the authentication flow. -After signing out, it'll be great to redirect user back to your website. Let's add `http://localhost:3000` as the Post Sign-out URI below, and use it as the parameter when calling `.signOut()`. + + {`import { useLogto } from '@logto/vue'; - +const { signIn, signOut, isAuthenticated } = useLogto(); -### Implement a sign-out button +const onClickSignIn = () => signIn('${props.redirectUris[0] ?? 'http://localhost:3000/callback'}'); +const onClickSignOut = () => signOut('${props.postLogoutRedirectUris[0] ?? 'http://localhost:3000'}'); +`} + -
-
-{``}
+
 
-
-
+ -```html - -``` + - + -In Logto SDK, generally we can use `logtoClient.isAuthenticated` to check the authentication status, if the user is signed in, the value will be `true`, otherwise, the value will be `false`. +To display the user's information, you can use the `getIdTokenClaims()` method. For example, in your Home page: -In Logto Vue SDK, the `isAuthenticated` status can be checked by using the `useLogto` composable. In the example code below, we can use it to programmatically show and hide the sign-in and sign-out buttons. Also we'll use `getIdTokenClaims` to get the ID of the currently logged-in user. +```ts title="views/HomeView.vue" +import { useLogto, type IdTokenClaims } from '@logto/vue'; +import { ref } from 'vue'; -```tsx -import { useLogto } from "@logto/vue"; -import { ref } from "vue"; - -const { isAuthenticated, getIdTokenClaims, signIn, signOut } = useLogto(); -const userId = ref(); +const { isAuthenticated, getIdTokenClaims } = useLogto(); +const user = ref(); if (isAuthenticated.value) { (async () => { const claims = await getIdTokenClaims(); - userId.value = claims.sub; + user.value = claims; })(); } ``` ```html ``` - - -Now, you can test your application: - -1. Run your application, you will see the sign-in button. -2. Click the sign-in button, the SDK will init the sign-in process and redirect you to the Logto sign-in page. -3. After you signed in, you will be redirected back to your application and see user ID and the sign-out button. -4. Click the sign-out button to sign-out. - - -