-
Notifications
You must be signed in to change notification settings - Fork 33
V1 Docs
NOTE: this page describes v1 versions.
This lib is meant to be used in a Vue project with Vue Router. If you're not using router functionality then this lib may be of limited use. Typescript typings are included as well.
Both Vue 2 and Vue 3 are supported. Choose either import-from paths for the appropriate version like below.
import { createOidcAuth, SignInType } from 'vue-oidc-client/vue2';
// or
import { createOidcAuth, SignInType } from 'vue-oidc-client/vue3';
Assuming your app will be hosted on the url https://mydomain.com/myapp/
, then you can import the lib and create an instance of it like the example below.
import { createOidcAuth, SignInType } from 'vue-oidc-client/vue2';
// note the ending '/'
const appUrl = 'https://mydomain.com/myapp/';
// SignInType could be Window or Popup
var mainOidc = createOidcAuth('main', SignInType.Window, appUrl , {
authority: 'https://demo.identityserver.io/',
client_id: 'implicit',
response_type: 'id_token token',
scope: 'openid profile email api'
});
The last parameter is the same configuration object for oidc-client
's UserManager
as described
in its configuration section.
The lib defines these default redirect urls if you don't define them in the config object. Use the formats below when registering your app with an openid connect provider if using the default paths.
// authName and appUrl are the values passed in createOidcAuth()
// register these as the allowed redirect urls with the provider
`${appUrl}auth/signinsilent/${authName}`;
`${appUrl}auth/signinwin/${authName}`;
`${appUrl}auth/signinpop/${authName}`;
// also register these as the logout redirect url
// (or as normal redirect urls if not separately available)
`${appUrl}`;
`${appUrl}auth/signoutpop/${authName}`;
When there's a route that needs to be protected with an oidc
instance, add its authName
to the route's meta property like below (note the use of history
mode)
const router = new Router({
mode: 'history',
routes: [
...
{
path: '/secret',
name: 'secret-route',
meta: {
authName: mainOidc.authName
},
...
}
]
});
After you've created the router instance, call the useRouter()
method to generate the callback handler routes and navigation guards.
mainOidc.useRouter(router);
Before you create your root Vue instance, you need to call the startup()
method and wait until it's done. This is required to load existing user info or handle redirects on first page load. If the promise returns true then it's ok to create the Vue instance.
mainOidc.startup().then(ok => {
if (ok) {
new Vue({
router,
render: h => h(App)
}).$mount('#app');
}
});
The auth instance provides properties related to current user or initial config that you can use in various Vue components. These properties are reactive so the component will be notified if they change.
mainOidc.isAuthenticated; // if logged in
mainOidc.accessToken; // if applicable and valid
mainOidc.userProfile; // user claims object
mainOidc.appUrl; // value passed in createOidcAuth()
mainOidc.authName; // value passed in createOidcAuth()
The auth instance also provides these additional methods:
signIn()
signOut()
startSilentRenew()
stopSilentRenew()
This library exposes the user manager events objects from the underlying oidc-client
library. See the events docs on what it supports. You can hook them like this:
mainOidc.events.addUserLoaded(function(user) {
console.log('user loaded', user)
// you can interact with your Vuex store if you want to save some details
})
mainOidc.events.addUserSignedOut(function() {
console.log('user signed out');
})
See repo has quick sample projects for both Vue 2 and Vue 3.