Skip to content

Pre v1 Docs

Eugene Wang edited this page Apr 24, 2021 · 2 revisions

NOTE: this page describes pre-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.

Using the lib

Creating an Auth Instance

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';

// 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.

Transpiling

When imported from node_modules like above, the referenced file is the source and may need transpiling. In a vue-cli 3 project you can add this lib to the transpiling list.

// in vue.config.js
module.exports = {
  transpileDependencies: ['vue-oidc-client']
}

Redirect Urls

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}`;

Router Integration

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);

App Start

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');
  }
});

Using it in Vue Components

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()

Events

This library re-emits the events from the underlying oidc-client library. You can hook them like this:

mainOidc.$on('userLoaded', function(user) {
  console.log('user loaded', user)
  // you can interact with your Vuex store if you want to save some details
})

mainOidc.$on('userSignedOut', function() {
  console.log('user signed out');
})

The events available are:

  • accessTokenExpiring
  • accessTokenExpired
  • silentRenewError
  • userLoaded
  • userUnloaded
  • userSignedOut
  • userSessionChanged

See the sample project for more examples.