jl-vue-oidc-client

1.0.10 • Public • Published

jl-vue-oidc-client

This is a wrapper around oidc-client-js to better work in a Vue application with Vue Router integration.

Installation

NPM

npm install jl-vue-oidc-client

Yarn

yarn add jl-vue-oidc-client

Usage

See the wiki for quick docs.

Running the sample

Go into either the vue2 or vue3 folder. Run it with the typical install and run steps like below:

npm install

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.

Using the lib

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-oidc';
// or
import { createOidcAuth, SignInType } from 'vue-oidc-client/vue3-oidc';

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/vue2-oidc';

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

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/callback`;
`${appUrl}/auth/silent-signin`;
`${appUrl}/auth/signoutpop`;

// also register these as the logout redirect url 
// (or as normal redirect urls if not separately available)
`${appUrl}`;
`${appUrl}auth/signoutpop`;

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

Package Sidebar

Install

npm i jl-vue-oidc-client

Weekly Downloads

78

Version

1.0.10

License

MIT

Unpacked Size

507 kB

Total Files

36

Last publish

Collaborators

  • lhuusonk35