All Projects → anthonygore → Vue Router User Roles

anthonygore / Vue Router User Roles

Licence: mit
A Vue.js plugin that protects routes based on user roles. Add your own authentication.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Vue Router User Roles

Accesscontrol
Role and Attribute based Access Control for Node.js
Stars: ✭ 1,723 (+627%)
Mutual labels:  authorization, permissions, roles
Laravel Governor
Manage authorization with granular role-based permissions in your Laravel Apps.
Stars: ✭ 131 (-44.73%)
Mutual labels:  authorization, permissions, roles
Laravel Auth
A powerful authentication, authorization and verification package built on top of Laravel. It provides developers with Role Based Access Control, Two-Factor Authentication, Social Authentication, and much more, compatible Laravel’s standard API and fully featured out of the box.
Stars: ✭ 128 (-45.99%)
Mutual labels:  authorization, permissions, roles
Laratrust
Handle roles and permissions in your Laravel application
Stars: ✭ 1,799 (+659.07%)
Mutual labels:  authorization, permissions, roles
Think Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in ThinkPHP 6.0 .
Stars: ✭ 155 (-34.6%)
Mutual labels:  authorization, permissions, roles
Policyserver.local
Sample OSS version of PolicyServer
Stars: ✭ 444 (+87.34%)
Mutual labels:  authorization, permissions, roles
Sentinel
A framework agnostic authentication & authorization system.
Stars: ✭ 1,354 (+471.31%)
Mutual labels:  authorization, permissions, roles
Bouncer
Eloquent roles and abilities.
Stars: ✭ 2,763 (+1065.82%)
Mutual labels:  authorization, permissions, roles
nova-permissions
Add Permissions based authorization for your Nova installation via User-based Roles and Permissions. Roles are defined in the database whereas Permissions are defined in the code base.
Stars: ✭ 115 (-51.48%)
Mutual labels:  permissions, roles, authorization
HeimGuard
🛡 A simple library that allows you to easily manage permissions in your .NET projects.
Stars: ✭ 77 (-67.51%)
Mutual labels:  permissions, roles, authorization
Brandenburg
Laravel Authentication Package
Stars: ✭ 79 (-66.67%)
Mutual labels:  authorization, permissions, roles
Simpleacl
Simple ACL for PHP
Stars: ✭ 105 (-55.7%)
Mutual labels:  authorization, permissions
Rbac.dev
A collection of good practices and tools for Kubernetes RBAC
Stars: ✭ 115 (-51.48%)
Mutual labels:  authorization, permissions
Appy
🚀 A full stack boilerplate web app
Stars: ✭ 225 (-5.06%)
Mutual labels:  authorization, permissions
React Native Permissions
An unified permissions API for React Native on iOS and Android
Stars: ✭ 2,939 (+1140.08%)
Mutual labels:  permissions, authorization
Appy Backend
A user system to bootstrap your app.
Stars: ✭ 96 (-59.49%)
Mutual labels:  authorization, permissions
Laravel Authz
An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.
Stars: ✭ 136 (-42.62%)
Mutual labels:  authorization, permissions
Maravel Permissions
Because in the Maravelous univer every user deserves super power
Stars: ✭ 139 (-41.35%)
Mutual labels:  permissions, roles
Security.identity
.NET DevPack Identity is a set of common implementations to help you implementing Identity, Jwt, claims validation and another facilities
Stars: ✭ 165 (-30.38%)
Mutual labels:  authorization, roles
Kan
Simple, functional authorization library and role management for ruby
Stars: ✭ 232 (-2.11%)
Mutual labels:  authorization, roles

vue-router-user-roles

Build status Coverage Status

npm vue2

A plugin for Vue.js SPAs that protects routes depending on user role. Add your own authentication.

📖 Usage

Check out the demo here.

Installation

$ npm i vue-router-user-roles

First create a Vue Router instance. It's best to do this in a dedicated file and export as a module e.g.

// router.js

import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

export default new VueRouter(...);

Now you can add this plugin in your main file. You must pass in a router instance as an option router.

// main.js

import Vue from "vue";
import router from "./router";
import VueRouterUserRoles from "vue-router-user-roles";

Vue.use(VueRouterUserRoles, { router });

Protecting routes

To protect a route, add a permissions property to each route configuration object under the meta property.

Assign an array of objects to this, with each object defining the permissions for a different user role.

The three properties required for permissions objects are:

  • role - the user role being configured for this route.
  • access - either a boolean, or function returning a boolean, that defines access for the user. A function will have access to two objects: the user object and the route being accessed.
  • redirect - a route name you wish to redirect to if the user does not have access.
let opts = {
  routes: [
    {
      path: "/protected",
      name: "protected",
      component: Protected,
      meta: {
        permissions: [
          {
            role: "guest",
            access: false,
            redirect: "login"
          }
        ]
      }
    },
    {
      path: "/profile/:id",
      name: "profile",
      component: Profile,
      meta: {
        permissions: [
          {
            role: "registered",
            access: (user, to) => user.id === to.params.id,
            redirect: "login"
          },
          {
            role: "guest",
            access: false,
            redirect: "login"
          }
        ]
      }
    }
  ]
};

const router = new VueRouter(opts);

User

A "user" is an object with one required property: role. Typically this would be set to a string e.g. "guest", "admin" etc.

You can add other properties to this object. You may want to do that if route access is determined by a function, since the function is passed this object. For example, you may create an id property that could be compared to a route parameter e.g /user/:id

Once the plugin is installed, you can access user from within your Vue instance or any component as this.$user.

Set the user

You can set a user with the set method. Here's an example of setting the user before the first instance of Vue is created:

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import VueRouterUserRoles from "vue-router-user-roles";

Vue.use(VueRouterUserRoles, router);

// This would usually be an AJAX call to the server or a cookie check
// Let's assume the user hasn't logged in yet so they're a guest for now.
let authenticate = Promise.resolve({ role: "guest" });

authenticate.then(user => {
  Vue.prototype.$user.set(user);
  new Vue({
    render: h => h(App),
    router
  }).$mount("#app");
});

You'll probably set the user again during the lifecycle of the app. For example, a user may start as a guest, but once they're authenticated their role and permissions will change.

You can access user from within the app as this.$user e.g.

export default {
  methods: {
    logIn(username, password) {
      authenticate("/api/user", { username, password })
        .then(user => {
          this.$user.set(Object.assign(user, { role: "registered" }));
        });
    },
    logOut() {
      this.$user.set({ role: "guest" });
    }
  }
}

The user object is reactive, so each time you set the user, permissions will be reassessed and will potentially redirect the page if the user no longer has access to the current route.

The other API method available is get:

<template>
  <div v-if="$user.get().role === 'guest'">...</div>
</template>

📜 Changelog

Details changes for each release are documented in the CHANGELOG.md.

❗️ Issues

Please make sure to read the Issue Reporting Checklist before opening an issue. Issues not conforming to the guidelines may be closed immediately.

💪 Contribution

Please make sure to read the Contributing Guide before making a pull request.

©️ License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].