npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-adal

v1.3.1

Published

Vue plugin for using Azure Active Directory

Downloads

1,553

Readme

Vue Adal

Vue Adal is a plugin for Vue.js to help with using Azure Active Directory.

Check the sample folder for a usage example.

Installation

yarn add vue-adal

OR

npm install vue-adal

Basic Usage

import Adal from 'vue-adal'
Vue.use(Adal, {
// This config gets passed along to Adal, so all settings available to adal can be used here.
  config: {
    // 'common' (multi-tenant gateway) or Azure AD Tenant ID
    tenant: '<guid>',

    // Application ID
    clientId: '<guid>',

    // Host URI
    redirectUri: '<host addr>',

    cacheLocation: 'localStorage'
  },

  // Set this to true for authentication on startup
  requireAuthOnInitialize: true,

  // Pass a vue-router object in to add route hooks with authentication and role checking
  router: router
})

important: make sure to set the mode on your router to 'history' so that it doesn't use hashes! This will have implications on the serverside.

new Router({
  mode: 'history', // Required for Adal library
  ... // Rest of router init
})

Getting user Information

After signing in, get access to the user as follows:

import { AuthenticationContext } from 'vue-adal' // This will be populated with a valid context after initialization

...
const profile = AuthenticationContext.user.profile
...

Getting Access to a Resource

After configuring Vue Adal, you'll still need to get a token to a resource.

Important: your Azure application must be configured to allow the oauth2ImplicitFlow in the manifest, like so: implicit flow

Axios HTTP Client / Interceptor

Vue Adal provides a convenient and automated way to do that with an axios http client, called AxiosAuthHttp. It configures an interceptor the auto-acquires tokens and will retry requests after a 401 and another attempt to get a token.

Here is an example:

import axios from 'axios'
import { default as Adal, AxiosAuthHttp } from 'vue-adal'

...
Vue.use({
  install (vue, opts = {}) {
    // Configures an axios http client with a interceptor to auto-acquire tokens
    vue.prototype.$graphApi = AxiosAuthHttp.createNewClient({
      // Required Params
      axios: axios,
      resourceId: graphApiResource, // Resource id to get a token against

      // Optional Params
      router: router, // Enables a router hook to auto-acquire a token for the specific resource

      baseUrl: graphApiBase, // Base url to configure the client with

      onTokenSuccess (http, context, token) { // Token success hook
        // When an attempt to retrieve a token is successful, this will get called.
        // This enables modification of the client after a successful call.
        if (context.user) {
          // Setup the client to talk with the Microsoft Graph API
          http.defaults.baseURL = `${graphApiBase}/${context.user.profile.tid}`
        }
      },

      onTokenFailure (error) { // Token failure hook
        // When an attempt to retrieve a token is not successful, this will get called.
        console.log(error)
      }
    })
  }
})
...

Take a look at the sample for more details.

Manually getting a token

If you'd like to get a token yourself, use the acquireToken command on the Authentication context:

import { AuthenticationContext } from 'vue-adal' // This will be populated with a valid context after initialization

...
  AuthenticationContext.acquireToken(resource, (err, token) => {
    if (err) {
      let errCode = err.split(':')[0]
      switch (errCode) {
        case 'AADSTS50058': // Need to prompt for user sign in
          AuthenticationContext.login()
          break
        case 'AADSTS65001': // Token is invalid; grab a new one
          AuthenticationContext.acquireTokenRedirect(resource)
          break
        case 'AADSTS16000': // No Access
        default:
          // Need a pop-up forcing a login
          AuthenticationContext.login()
          break
      }
      return
    }
    const headers = {
      'Authorization': `BEARER ${token}`
    }
  })
...

Route Hooks

If you pass in a router object as an option in Vue Adal, it will configure a global hook before each route allowing for route meta tags around authentication and roles.

To make a route that requires auth, add a meta object to the route with requireAuth set to true:

// Other routes
...
    {
      path: '/secret',
      name: 'secret',
      component: MySecretComponent,
      meta: {
        requireAuth: true
      }
    }
...

To make a route that requires role(s), add a :

// Other routes
...
    {
      path: '/secretRoles',
      name: 'secretRoles',
      component: MySecretRolesComponent,
      meta: {
        requireAuth: true
        // Much match at least one of these roles
        requireRoles: [
          'TheMan',
          'Admin'
        ]
      }
    }
...

Conditionally Show Content Based on Auth Status

Conditionally show content when authenticated:

<div v-if="$adal.isAuthenticated()">You are signed in!</div>

Conditionally show content only when at least one role exists on user:

<div v-if="$adal.checkRoles(['Admin'])">You are an admin!</div>