Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/luckcoding/koa-authority
Koa middleware for the authority module. Support v1 and v2.
https://github.com/luckcoding/koa-authority
koa koa-authority koa-middleware
Last synced: 24 days ago
JSON representation
Koa middleware for the authority module. Support v1 and v2.
- Host: GitHub
- URL: https://github.com/luckcoding/koa-authority
- Owner: luckcoding
- Created: 2017-06-19T10:28:57.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-11-05T13:33:16.000Z (about 5 years ago)
- Last Synced: 2024-10-02T06:20:11.146Z (about 1 month ago)
- Topics: koa, koa-authority, koa-middleware
- Language: JavaScript
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# koa-authority
[![npm version](https://img.shields.io/npm/v/koa-authority.svg)](https://www.npmjs.com/package/koa-authority)
This module provides a minimalistic verification.
## Installation
```
npm install koa-authority
```## Use
**real example:[==> hotchcms](https://github.com/luckcoding/hotchcms)**
### example 1
> use koa-router
```
var koaAuthority = require('koa-authority')
var Router = require('koa-router')// 将要被校验的路由
var router = new Router()
router.post('/check', function(){})// koaAuthority 提供更改权限的中间件 (middle)
function authority(routes) {
reutrn koaAuthority({
routes: routes,
useKoaRouter: true,
middleware: function(ctx, { routes }) {
return new Promise(function(resolve, reject) {
if (err) return reject('500')
if (isAdmin) {
resolve(routes) // or resolve(true)
} else {
resolve([])
}
})
}
})
}// 使用
app.use(authority(router))
```### example 2
> whitout koa-router
```
var koaAuthority = require('koa-authority')// koaAuthority 提供更改权限的中间件 (middle)
function authority(routes) {
reutrn koaAuthority({
routes: routes,
middleware: function(ctx, { routes }) {
return new Promise(function(resolve, reject) {
if (err) return reject('500')
if (isAdmin) {
resolve(routes) // or resolve(true)
} else {
resolve([])
}
})
}
})
}// 使用
app.use(authority([
{
path: 'api/user',
methods: ['POST'],
},
{
path: 'api/content/xxx',
methods: ['GET','HEAD', ...],
},
...
]))
```## options
* **routes**. `koa-router` Object or an Array like `[{path:'',methods:['GET'...]}...]`
* **useKoaRouter**. Bollean, default false, *use koa-router ?*
* **middleware**. Function, *return a Promise*.