|
| 1 | +# Asgardeo Auth Vue.js SDK Usage Example (Single Page Application) |
| 2 | + |
| 3 | +This sample is developed to demonstrate the basic usage of the Asgardeo Auth Vue.js SDK. |
| 4 | + |
| 5 | +## Getting Started |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | +- `Node.js` (version 20 or above). |
| 9 | + |
| 10 | +### Register an Application |
| 11 | +//TODO |
| 12 | + |
| 13 | +Make sure to add `http://localhost:5173` as a Redirect URL and also add it under allowed origins. |
| 14 | + |
| 15 | +### Download the Sample |
| 16 | +//TODO |
| 17 | + |
| 18 | +### Configure the Sample |
| 19 | + |
| 20 | +Update the authentication configuration in your `main.ts` file with your registered app details. |
| 21 | + |
| 22 | +**Note:** You will need to paste in the `client ID` generated for the application you registered. |
| 23 | + |
| 24 | +```typescript |
| 25 | +import "./assets/main.css"; |
| 26 | +import { createApp } from "vue"; |
| 27 | +import App from "./App.vue"; |
| 28 | +import router from "./router"; |
| 29 | +import { asgardeoPlugin } from "./auth/authprovider"; |
| 30 | + |
| 31 | +const app = createApp(App); |
| 32 | + |
| 33 | +app.use(router); |
| 34 | +app.use(asgardeoPlugin, { |
| 35 | + signInRedirectURL: "http://localhost:5173/", |
| 36 | + signOutRedirectURL: "http://localhost:5173/", |
| 37 | + clientID: "<ADD_CLIENT_ID_HERE>", |
| 38 | + baseUrl: "https://api.asgardeo.io/t/<org_name>", |
| 39 | +}); |
| 40 | + |
| 41 | +app.mount("#app"); |
| 42 | +``` |
| 43 | + |
| 44 | +### Run the Application |
| 45 | + |
| 46 | +```bash |
| 47 | +npm install && npm run dev |
| 48 | +``` |
| 49 | + |
| 50 | +The app should open at [`http://localhost:5173`](http://localhost:5173) |
| 51 | + |
| 52 | +### Change the Application's Development Server Port |
| 53 | + |
| 54 | +By default, the Vite development server runs on port `5173`. In case you wish to change this to something else, follow the steps below. |
| 55 | + |
| 56 | +1. Update the port in your Vite configuration file (`vite.config.js` or `vite.config.ts`): |
| 57 | + ```javascript |
| 58 | + export default defineConfig({ |
| 59 | + // Other config options... |
| 60 | + server: { |
| 61 | + port: YOUR_PREFERRED_PORT |
| 62 | + } |
| 63 | + }) |
| 64 | + ``` |
| 65 | + |
| 66 | +2. Update the `signInRedirectURL` & `signOutRedirectURL` in `main.ts` to match your new port. |
| 67 | + |
| 68 | +3. Go to the Asgardeo Console and navigate to the protocol tab of your application: |
| 69 | + - Update the Authorized Redirect URL. |
| 70 | + - Update the Allowed Origins. |
| 71 | + |
| 72 | +## Using the Auth Plugin |
| 73 | + |
| 74 | +The Asgardeo Auth plugin is available throughout your Vue application. Here's how to use it in your components: |
| 75 | + |
| 76 | +```vue |
| 77 | +<script setup> |
| 78 | +import { useAsgardeo } from '@asgardeo/vue'; |
| 79 | +
|
| 80 | +const { isAuthenticated, signIn, signOut, getBasicUserInfo } = useAsgardeo(); |
| 81 | +</script> |
| 82 | +
|
| 83 | +<template> |
| 84 | + <div> |
| 85 | + <button v-if="!isAuthenticated" @click="signIn">Login</button> |
| 86 | + <button v-else @click="signOut">Logout</button> |
| 87 | + </div> |
| 88 | +</template> |
| 89 | +``` |
| 90 | + |
| 91 | +//TODO |
| 92 | + |
| 93 | +## Contribute |
| 94 | + |
| 95 | +Please read [Contributing to the Code Base](../../CONTRIBUTING.md) for details on our code of conduct, and the process for submitting pull requests to us. |
| 96 | + |
| 97 | +### Reporting Issues |
| 98 | + |
| 99 | +We encourage you to report issues, improvements, and feature requests by creating [Github Issues](https://github.com/asgardeo/web-ui-sdks/issues). |
| 100 | + |
| 101 | +Important: Please be advised that security issues must be reported to [email protected], not as GitHub issues, in order to reach the proper audience. We strongly advise following the WSO2 Security Vulnerability Reporting Guidelines when reporting security issues. |
| 102 | + |
| 103 | +## License |
| 104 | + |
| 105 | +This project is licensed under the Apache License 2.0. See the [LICENSE](../../LICENSE) file for details. |
0 commit comments