|
| 1 | +# Vue quickstart |
| 2 | + |
| 3 | +Welcome to the Vue quickstart guide! In this document, you will learn to build a Vue app, add user sign-in, and display user profile information using {{ product_name }}. |
| 4 | + |
| 5 | +[//] STEPS_START |
| 6 | + |
| 7 | +## Configure an application in {{ product_name }} |
| 8 | + |
| 9 | +- Sign into the {{ product_name }} Console and navigate to **Applications > New Application**. |
| 10 | +- Select **Single-Page Application** and complete the wizard by providing a suitable name and an authorized redirect URL. |
| 11 | + |
| 12 | +!!! Example |
| 13 | + **Name:** `{{ product }}-vue` |
| 14 | + |
| 15 | + **Authorized redirect URL:** `http://localhost:5173` |
| 16 | + |
| 17 | +Once you finish creating the application, note down the following values from its **Guide** tab. You will need them to configure the Asgardeo Vue SDK. |
| 18 | + |
| 19 | +- **Client ID** - The unique identifier for your application. |
| 20 | +- **Base URL** - The base URL of your {{ product_name }} organization. This typically follows the format `{{content.sdkconfig.baseUrl}}` |
| 21 | + |
| 22 | +!!! Info |
| 23 | + |
| 24 | + The authorized redirect URL determines where {{ product_name }} should send users after they successfully sign in. Typically, this will be the web address where your app is hosted. For this guide, we'll use `http://localhost:5173`, as the sample app will be accessible at this URL. |
| 25 | + |
| 26 | +## Create a Vue app using Vite |
| 27 | + |
| 28 | +Create (scaffold) your new Vue app using [Vite](https://vite.dev/). |
| 29 | + |
| 30 | +=== "npm" |
| 31 | + |
| 32 | + ```bash |
| 33 | + npm create vite@latest {{ product }}-vue -- --template vue |
| 34 | + cd {{ product }}-vue |
| 35 | + npm install |
| 36 | + npm run dev |
| 37 | + ``` |
| 38 | + |
| 39 | +=== "yarn" |
| 40 | + |
| 41 | + ```bash |
| 42 | + yarn create vite@latest {{ product }}-vue -- --template vue |
| 43 | + cd {{ product }}-vue |
| 44 | + yarn install |
| 45 | + yarn dev |
| 46 | + ``` |
| 47 | + |
| 48 | +=== "pnpm" |
| 49 | + |
| 50 | + ```bash |
| 51 | + pnpm create vite@latest {{ product }}-vue -- --template vue |
| 52 | + cd {{ product }}-vue |
| 53 | + pnpm install |
| 54 | + pnpm dev |
| 55 | + ``` |
| 56 | + |
| 57 | +## Install `@asgardeo/vue` |
| 58 | + |
| 59 | +Asgardeo Vue SDK provides all the components and composables you need to integrate {{ product_name }} into your app. To get started, simply add the Asgardeo Vue SDK to the project. Make sure to stop the dev server you started in the previous step. |
| 60 | + |
| 61 | +=== "npm" |
| 62 | + |
| 63 | + ```bash |
| 64 | + npm install @asgardeo/vue |
| 65 | + ``` |
| 66 | + |
| 67 | +=== "yarn" |
| 68 | + |
| 69 | + ```bash |
| 70 | + yarn add @asgardeo/vue |
| 71 | + ``` |
| 72 | + |
| 73 | +=== "pnpm" |
| 74 | + |
| 75 | + ```bash |
| 76 | + pnpm add @asgardeo/vue |
| 77 | + ``` |
| 78 | + |
| 79 | +## Add `AsgardeoPlugin` and `<AsgardeoProvider />` to your app |
| 80 | + |
| 81 | +`AsgardeoPlugin` registers the Asgardeo Vue SDK globally with your Vue application. `<AsgardeoProvider />` wraps your app and makes authentication context available to all child components. |
| 82 | + |
| 83 | +Add the following changes to `main.js` to register the plugin. |
| 84 | + |
| 85 | +```javascript title="src/main.js" hl_lines="3 7" |
| 86 | +import { createApp } from 'vue' |
| 87 | +import './style.css' |
| 88 | +import { AsgardeoPlugin } from '@asgardeo/vue' |
| 89 | +import App from './App.vue' |
| 90 | + |
| 91 | +createApp(App) |
| 92 | + .use(AsgardeoPlugin) |
| 93 | + .mount('#app') |
| 94 | +``` |
| 95 | + |
| 96 | +Then replace the contents of `App.vue` with the following to wrap your app with `<AsgardeoProvider />`. |
| 97 | + |
| 98 | +!!! Important |
| 99 | + |
| 100 | + Replace the following placeholders with your registered organization name in {{ product_name }} and the generated `client-id` from the app you registered in {{ product_name }}. |
| 101 | + |
| 102 | + - `<your-app-client-id>` |
| 103 | + - `{{content.sdkconfig.baseUrl}}` |
| 104 | + |
| 105 | +```vue title="src/App.vue" hl_lines="2-5 7" |
| 106 | +<template> |
| 107 | + <AsgardeoProvider |
| 108 | + client-id="<your-app-client-id>" |
| 109 | + base-url="{{content.sdkconfig.baseUrl}}" |
| 110 | + > |
| 111 | + <!-- Your app content goes here --> |
| 112 | + </AsgardeoProvider> |
| 113 | +</template> |
| 114 | +``` |
| 115 | + |
| 116 | +## Add sign-in and sign-out to your app |
| 117 | + |
| 118 | +Asgardeo Vue SDK provides `SignInButton` and `SignOutButton` components to handle user sign-in and sign-out. Use these components alongside `SignedIn` and `SignedOut` to conditionally render content based on the user's sign-in state. |
| 119 | + |
| 120 | +Replace the contents of `App.vue` with the following. |
| 121 | + |
| 122 | +```vue title="src/App.vue" hl_lines="2 11-16" |
| 123 | +<script setup> |
| 124 | +import { SignedIn, SignedOut, SignInButton, SignOutButton } from '@asgardeo/vue' |
| 125 | +</script> |
| 126 | +
|
| 127 | +<template> |
| 128 | + <AsgardeoProvider |
| 129 | + client-id="<your-app-client-id>" |
| 130 | + base-url="{{content.sdkconfig.baseUrl}}" |
| 131 | + > |
| 132 | + <header> |
| 133 | + <SignedIn> |
| 134 | + <SignOutButton /> |
| 135 | + </SignedIn> |
| 136 | + <SignedOut> |
| 137 | + <SignInButton /> |
| 138 | + </SignedOut> |
| 139 | + </header> |
| 140 | + </AsgardeoProvider> |
| 141 | +</template> |
| 142 | +``` |
| 143 | + |
| 144 | +## Display the signed-in user's profile information |
| 145 | + |
| 146 | +You can use the `UserProfile` or `UserDropdown` components to display user profile information in a declarative way. |
| 147 | + |
| 148 | +- `UserProfile`: Displays and allows the user to edit their profile information. |
| 149 | +- `UserDropdown`: Provides a dropdown menu with built-in user information and sign-out functionality. |
| 150 | + |
| 151 | +```vue title="src/App.vue" hl_lines="2 12 20-22" |
| 152 | +<script setup> |
| 153 | +import { SignedIn, SignedOut, SignInButton, SignOutButton, UserDropdown, UserProfile } from '@asgardeo/vue' |
| 154 | +</script> |
| 155 | +
|
| 156 | +<template> |
| 157 | + <AsgardeoProvider |
| 158 | + client-id="<your-app-client-id>" |
| 159 | + base-url="{{content.sdkconfig.baseUrl}}" |
| 160 | + > |
| 161 | + <header> |
| 162 | + <SignedIn> |
| 163 | + <UserDropdown /> |
| 164 | + <SignOutButton /> |
| 165 | + </SignedIn> |
| 166 | + <SignedOut> |
| 167 | + <SignInButton /> |
| 168 | + </SignedOut> |
| 169 | + </header> |
| 170 | + <main> |
| 171 | + <SignedIn> |
| 172 | + <UserProfile /> |
| 173 | + </SignedIn> |
| 174 | + </main> |
| 175 | + </AsgardeoProvider> |
| 176 | +</template> |
| 177 | +``` |
| 178 | + |
| 179 | +## Run the app |
| 180 | + |
| 181 | +To run the app, use the following command: |
| 182 | + |
| 183 | +=== "npm" |
| 184 | + |
| 185 | + ```bash |
| 186 | + npm run dev |
| 187 | + ``` |
| 188 | + |
| 189 | +=== "yarn" |
| 190 | + |
| 191 | + ```bash |
| 192 | + yarn dev |
| 193 | + ``` |
| 194 | + |
| 195 | +=== "pnpm" |
| 196 | + |
| 197 | + ```bash |
| 198 | + pnpm dev |
| 199 | + ``` |
| 200 | + |
| 201 | +Visit your app's homepage at [http://localhost:5173](http://localhost:5173). |
| 202 | + |
| 203 | +!!! Important |
| 204 | + |
| 205 | + To try out sign-in and sign-out features, create a test user in {{ product_name }} by following this [guide]({{ base_path }}/guides/users/manage-users/#onboard-single-user){:target="_blank"}. |
| 206 | + |
| 207 | +[//] STEPS_END |
0 commit comments