Vue.js
This guide explains how to set up your Vue.js project to begin using @badisi/auth-vue.
Make sure you properly configured your 👉 Authentication Provider.
Prerequisite​
An existing Vue.js application.
Installation​
-
Install the library from the NPM repository
- npm
- yarn
npm install @badisi/auth-vue --saveyarn add @badisi/auth-vue -
Install required dependency
- npm
- yarn
npm install vite-plugin-static-copy --save-devyarn add vite-plugin-static-copy -
Modify your Vite configuration file
vite.config.tsimport { viteStaticCopy } from 'vite-plugin-static-copy';
export default defineConfig({
plugins: [
viteStaticCopy({
targets: [{
src: 'node_modules/@badisi/auth-vue/oidc/assets/*',
dest: 'oidc/callback/'
}]
})
]
});infoAssets needs to be copy over to your application because some html web pages are used and required when navigating back from the Authentication Provider.
-
Provide the library
main.tsimport './assets/main.css'
import { initAuth } from '@badisi/auth-vue';
import { createApp } from 'vue'
import app from './App.vue'
import router from './router'
initAuth({
authorityUrl: 'https://dev-fijd1e9x.us.auth0.com',
clientId: 'kRVVEnAWKMpxxpcodl0TqLXfIHgQvmmt'
}).then(authPlugin => {
const vueApp = createApp(app);
vueApp.use(authPlugin, { router });
vueApp.use(router);
vueApp.mount('#app');
}).catch(error => console.error(error));infoInitializing the library prior to your Vue.js application bootstrapping ensures that all the security checks are made upstream (if needed) and avoid the whole application to be loaded twice when using redirects.
tipFor more info on how to configure the library please have a look at the
configurationpage. -
You're done! 🚀
Basic example​
<template>
<div style="display:flex; flex-direction:column; align-items:start; gap:16px">
<button @click="$authService.login()">Login</button>
<button @click="$authService.logout()">Logout</button>
<button @click="$authService.renew()">Silent renew</button>
(Authenticated: {{ $authService.isRenewingRef.value ? 'renewing..' : $authService.isAuthenticatedRef }})
</div>
</template>
<script setup lang="ts">
import { useAuthService } from '@badisi/auth-vue';
import { toRaw, watch } from 'vue';
const authService = useAuthService();
watch(authService.userProfileRef, value => {
console.log('user profile:', toRaw(value));
});
</script>