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

egg-auths

v1.0.2

Published

authentication and authorization egg plugin

Downloads

7

Readme

egg-auths

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Auths is a plugin of eggjs, mainly used to complete user authentication and authorization. Related APIs and modules are mainly designed with reference to Apache Shiro. It supports coarse-grained role-based control of resource access and fine-grained resource-based permission access control. In the current design, the data source (Realm) of roles and permissions is a database, and egg-sequelize is used as the ORM, user identity and credential information are persisted to the egg session.

Install

# depend egg-sequelize
$ npm i egg-auths egg-sequelize --save

Configuration

  1. config egg's plugin.js
// application/config/plugin.js

sequelize: {
  enable: true,
  package: 'egg-sequelize',
},
auths: {
  enable: true,
  package: 'egg-auths'
}
  1. config egg's config.default.js
// application/config/config.default.js

config.sequelize = {
  dialect: 'mysql',
  host: '127.0.0.1',
  port: 3306,
  database: 'test',
  username: 'root',
  password: '********',
  // ORM model definition in the plugin
  // plz copy related ORM models to application/app/model in production environment
  baseDir: '../node_modules/egg-auths/app/model'
}

config.auths = {
  // if you do not need custom any module, then just be an empty object
}
  1. Sync Model to DB
// application/app.js

module.exports = app => {
  if (app.config.env === 'local' || app.config.env === 'unittest') {
    app.beforeStart(async () => {
      await app.model.sync();
    });
  }
};

reference to egg-sequelize,you should use sequelize-cli in production environment.

Usage

  1. Use in router middleware
// application/app/router.js

// include RBACAuth module
const RBACAuth = require('egg-auths/lib/index')

module.exports = app => {
  const { router, controller } = app;
  // get an instance
  const rbac = new RBACAuth({
    // you can config some properties for HTTP Response when authenticate or authorizate failed, see API document
  })
  // set roles or permissions then will get middleware function
  router.get('/admin', rbac.checkRoles(['admin']), controller.admin.index)
  router.get('/orders', rbac.checkRoles(['admin','ceo']), controller.admin.order)
  router.get('/users', rbac.checkPermissions(['admin:user']), controller.admin.order)
  router.get('/profile', rbac.checkLogin(), controller.admin.order)
}
  1. Use in program(controller,service)with subject's methods and property
// application/app/controller/admin.js

const Controller = require('egg').Controller;

class AdminController extends Controller {
  async index () {
    let { ctx } = this
    let subject = await ctx.getSubject()
    subject.hasRoles(['admin','ceo'])
    subject.hasPermissions(['admin:user'])
    subject.isLogined // true or false
  }
}

License

MIT