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-router-auth

v1.0.3

Published

Flexible configuration of routes. Closing routes for authorized / unauthorized users.

Downloads

124

Readme

vue-router-auth

Flexible routes setup. Closing routes for authorized / unauthorized users, users with specific roles.

This module is designed for the fact that you already have an authorization system and you need to close the pages for unauthorized / authorized users. If you find an error, incorrect behavior please open the corresponding issue. Please also rate this repository. :blush:

:book: Usage

$ npm i vue-router-auth
import Vue from 'vue';
import VueRouterAuth from 'vue-router-auth';

const options = {
  router,
  guard: ({ $auth }) => $auth.loggedIn,
};

Vue.use(VueRouterAuth, options);

or

import { middleware } from 'vue-router-auth';

const options = {
  router,
  guard: ({ $auth }) => $auth.loggedIn,
};

router.beforeEach(middleware(options));

After which you need to configure the routes:

const routes = [
  {
    path: '/',
    component: () => import('layouts/MainLayout.vue'),
    children: [
      {
        path: 'login',
        component: () => import('pages/Login.vue'),
        meta: {
          auth: {
            access: false,
          },
        },
      },
    ],
  },
  {
    path: '/account',
    component: () => import('layouts/Auth.vue'),
    meta: {
      auth: {
        access: true,
      },
    },
    children: [
      {
        path: '',
        component: () => import('pages/Account.vue'),
      },
      {
        path: 'settings',
        component: () => import('pages/Settings.vue'),
      },
    ],
  },
];

:gear: Customization

Options

List of passed options during initialization:

name | default | description | required | type --------|---------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|-------- router | - | app router | true | Object guard | () => false | Function to check access. It has a context argument: to, from, Vue.prototype and the context that was passed in the module option. Use read-only context (must be redefined) | true | Function routes | { guest: '/public', auth: '/account' } | Routes for redirecting authorized, unauthorized users, users with specific roles. | false | Object context | {} | The data that will be available in the functions (guard, redirect) | false | Object

Also, for full work, you need to configure the meta field in routes:

Route options

List of options passed to the meta.auth route field:

name | description | type ---------|------------------------------------------------------|-------- access | A flag indicating the closure of the route. | Boolean or String redirect | route or function returning a route. | Function or Object or String guard | individual guard function for a specific route | Function

functions redirect and guard have the same arguments as the main guard function.

Description

To close the route for unauthorized users, you must specify true in the meta.auth.access route field. To close the route for authorized users, set the meta.auth.access field to false. In this case, the guard function should return true for authorized users and false for unauthorized users. To configure user redirection, use the routes option. guest - the route where the unauthorized user will be redirected, auth - the authorized user. You can also specify the redirection route individually for each route in the meta.auth.redirect field. The guard and redirect functions in the arguments receive a context, to expand it use the context option.

Using roles

In order to close the route for a specific role in the meta.auth.access route field, you must specify the role name, in this case, the guard function should return a string with the name of this role, otherwise redirection will occur. To configure redirection for a specific role, you need to add a field to the routes option with the name of this role.

:eyes: Examples

import { middleware } from 'vue-router-auth';
import auth from 'auth-module'; // for example

const options = {
  router,
  guard: ({ auth }) => auth.loggedIn,
  context: {
    auth,
  },
};

router.beforeEach(middleware(options));
const routes = [
  {
    path: '/',
    component: () => import('layouts/MainLayout.vue'),
    children: [
      {
        path: 'login',
        component: () => import('pages/Login.vue'),
        meta: {
          auth: {
            access: false,
          },
        },
      },
    ],
  },
  {
    path: '/account',
    component: () => import('layouts/Auth.vue'),
    meta: {
      auth: {
        access: true,
      },
    },
    children: [
      {
        path: '',
        component: () => import('pages/Account.vue'),
      },
      {
        path: 'settings',
        component: () => import('pages/Settings.vue'),
        meta: {
          auth: {
            access: false,
          },
        },
      },
    ],
  },
];

In the example above, the /login route will be closed to authorized users. A route /account will be closed to unauthorized users, but the route /account /settings will be open to unauthorized users and closed to authorized users.

Using roles:

import { middleware } from 'vue-router-auth';
import auth from 'auth-module'; // for example

const options = {
  router,
  guard: ({ auth }) => auth.user.role, // "admin"
  routes: {
    admin: '/route-for-not-admin',
  },
  context: {
    auth,
  },
};

router.beforeEach(middleware(options));
const routes = [
  // ...
  {
    path: '/account',
    component: () => import('layouts/Auth.vue'),
    children: [
      {
        path: 'payments',
        component: () => import('pages/Account.vue'),
        meta: {
          auth: {
            access: 'simple-role',
            redirect: '/any-route',
          },
        },
      },
      {
        path: 'settings',
        component: () => import('pages/Settings.vue'),
        meta: {
          auth: {
            access: 'admin',
          },
        },
      },
    ],
  },
];

:copyright: License

MIT