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-auth0-plugin

v3.0.22

Published

Simple auth0 wrapper for Vue.js

Downloads

1,456

Readme

Vue-Auth0-Plugin

codecov

Simple Auth0 wrapper for Vue.js based on the Auth0 Single Page App SDK

:warning: Currently, only compatible with Vue 3. Support for Vue 2 is planned.

Prerequisites

You need an Auth0 tenant and a configured Auth0 application. For information about how to create these, take a look at the official documentation.

Installation

npm install --save vue-auth0-plugin

Samples

To see some samples how to use the plugin, take a look into the samples folder in the project.

Usage

Register the plugin in your main.ts file. For a list of available options, take a look here: https://auth0.github.io/auth0-spa-js/interfaces/auth0clientoptions.html.

import { createApp } from 'vue';
import VueAuth0Plugin from 'vue-auth0-plugin';
import App from './App.vue';

const app = createApp(App);
app.use(VueAuth0Plugin, {
  domain: 'YOUR_AUTH0_DOMAIN',
  clientId: 'YOUR_CLIENT_ID',
  // ... other optional options ...
});
app.mount('#app');

Then Auth0 can be injected using the provided injectAuth function. For more information about provide/inject, take a look here https://v3.vuejs.org/guide/component-provide-inject.html. For example:

import { injectAuth } from 'vue-auth0-plugin'

const auth = injectAuth();

const authenticated = auth.authenticated;
const loading = auth.loading;
const user = auth.user;

if (!auth.authenticated) {
    auth.loginWithRedirect();
    
    // If errors occurre during login, they are provided in auth.error property
    if (auth.error != undefined) {
        // Do something with the error
    }
}

Auth0 can also be injected using the Options API like the example below

<template>
  <div class="about">
    <h1>You are logged in as {{ auth.user.name }} ({{ auth.user.nickname }})</h1>
    <img :src="auth.user.picture" alt="Profile picture"/>
    <button v-on:click="auth.logout()">Logout</button>
  </div>
</template>
<script lang="ts">
import { Options, Vue } from 'vue-class-component';
import { injectionToken } from 'vue-auth0-plugin'
@Options({
  inject: [injectionToken],
})
export default class MyComponent extends Vue {}
</script>

If you want to use the state of authentication when you do not have access to the Vue instance, you can use the exported AuthenticationState.

import { AuthenticationState } from 'vue-auth0-plugin';

if (!AuthenticationState.authenticated) {
    // do something here
}

// or asynchronous using Promise
if (!await AuthenticationState.getAuthenticatedAsPromise) {
    // do something here
}

INFO: The synchronous AuthenticationState.authenticated can give wrong result if used before initialization of the plugin. Then the plugin is still loading the state of authentication and returns the default value false. In this case you should use the asynchronous AuthenticationState.getAuthenticatedAsPromise that resolves when the loading has finished and then returns the state of authentication.

If you want to use the properties provided by Auth0 when you do not have access to the Vue instance, you can use the exported AuthenticationProperties.

import { AuthenticationProperties as auth0 } from 'vue-auth0-plugin';

const token = auth0.getTokenSilently();

AuthenticationGuards

The plugin implements two Vue Router NavigationGuards to secure routes with Auth0. The AuthenticationGuard has an integrated redirect to the Auth0 login when the user is not authenticated. The AuthenticationGuardWithoutLoginRedirect does not have this redirect. The example below shows how to use this AuthenticationGuards.

import { AuthenticationGuard } from 'vue-auth0-plugin';
// or alternative
import { AuthenticationGuardWithoutLoginRedirect } from 'vue-auth0-plugin';

let routes = [
    ...
    {
        path: '/private',
        name: 'PrivateRoute',
        component: () => import(/* webpackChunkName: "private" */ '../views/Private.vue'),
        beforeEnter: AuthenticationGuard, // or AuthenticationGuardWithoutLoginRedirect
    },
];

const router = createRouter({
  history: createWebHistory(YOUR_BASE_URL),
  routes,
});

Changelog

Changelog can be seen at GitHub Releases.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

If you have questions or want to provide a good idea for improvements, please open an issue.

License

MIT