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

@rtidatascience/harness-authentication

v0.1.4

Published

Vue plugin for authentication in the Harness framework.

Downloads

5

Readme

harness-authentication

harness-authentication is a module for the Harness Vue library for handling user authentication.

Setup

Module Configuration

harness-authentication is a Vue module that uses Vuex and Vue Router. To add the module to your Vue app, add the following to your main.js

// main.js (or equivalent)

import Authentication from `harness-authentication`

const loginApiUrl = '/api/login'
const accessTokenCookiePath = '/app/path'
Vue.use(Authentication, { store, router, url: loginApiUrl, cookiePath: accessTokenCookiePath })
  • url: string is the url to your authentication endpoint
  • cookiePath: string is the path that the access token cookie is saved under, to restrict browser access

The module will initialize its state in Vuex automatically.

Router Configuration

You must configure your router to utilize the authentication functionality. A very simple setup might look something like this:

// router.js (or equivalent)

const router = new VueRouter({
  routes: [
    { 
      path: '/'.
      meta: {
        allowGuest: true,
        ignoreIfAuthenticated: false,
        logout: false
      }
    }
  ]
})

router.beforeEach(async (to, from, next) => {
  const { allow, isGuest } = await verifyRouterAuthentication(store, to)

  if (allow) {
    next()
  } else {
    next('/')
  }
})

First, each route has a set of variables within meta that describes to the module the access parameters for each route:

  • allowGuest: boolean (default=false) Whether the route is available to unauthenticated users
  • ignoreIfAuthenticated: boolean (default=false) Whether the route is available to authenticated users
  • logout: boolean (default=false) Whether to automatically log the user out on navigating to this route

Next, you need to intercept each nagivation event to determine whether the requested navigation is allowed for the current user.

const { allow, isGuest } = await verifyRouterAuthentication(store, to)

verifyRouterAuthentication returns two pieces of information about the user:

  • allow: boolean Whether the user is allowed to access the route they are navigating to
  • isGuest: boolean Whether the user is and authenticated user (false), or is an unauthenticated guest (true)

You must handle the navigation yourself by calling the next function provided by the router.

Authenticating

The module injects into your components the authenticateUser({ username: string, password: string}) action that you can use to send the authentication request.

async onSubmit () {
  try {
    await this.authenticateUser(this.form)
    this.$router.push('/')
  } catch (e) {
    this.formError = e.message
  }
}

By calling authenticateUser, the module will make an authentication request, and if successful, will store the credentials in Vuex. On failure, it will throw an Error.

API

The module exports a number of methods to help you access its functionality

getIsAuthenticatedFromStore(store): boolean

Given the Vuex store, will return whether the user has been authenticated.

getAuthHeaderFromStore(store): { Authorization: string }

Returns the Authorization header for the current authorized user, to be added to any requests requiring the authorization credentials.

Throws an error if the user is not authenticated.

isResponseAuthorized(response): boolean

Given a request response, returns whether the response was authorized.

verifyLogin(store)

Used to retrieve a saved authorization from the user's cookies. If there is a saved authentication, it will be saved to the store.

dispatchLogoutUser(store)

Clears the local user authentication. A logoutUser event will be dispatched to the root store that you can listen for and respond to.

expireUserAuthentication(store)

Clears the local user authentication. A `loginExpired' event will be dispatched to the root store that you can listen for and respond to. Useful for distinguishing between a willful logout of the user, versus removing authorization because the token has expired.