Skip to content
Eugene Wang edited this page Oct 17, 2018 · 20 revisions

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

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

Redirect Urls

The lib does not allow you to define the various redirect urls in the configuration object in the createOidcAuth() method. Use the url formats below when registering your app with an openid connect provider.

// authName and appUrl are the values passed in createOidcAuth()

// register these as the redirect url 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 the mainOidc instance, add its authName to the route's meta property like below

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 signIn() and signOut() methods. It's usually unnecessary to call signIn() manually but it's still there if needed.

References

This lib uses the oidc-client lib under the hood and the configuration object for createOidcAuth() is the same one used by UserManager. Refer to the Configuration section to see all the possible config properties. Note the redirect urls are always defined by this lib and cannot be overridden.

Clone this wiki locally