1.0.6 • Published 4 years ago

elephant-auth v1.0.6

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

elephant-auth

用于小象用户登陆校验和简单的权限控制

只适配 Koa2

##Getting Start

  1. 校验登陆状态
const auth = require('elephant-auth')
app.use(auth.validate({
  errHandle: async function (ctx) {
    console.log('授权错误')
    // 检测到用户没有登陆时会触发此 handle
    // 跳转到SSO的登陆页面或者 返回url给前端跳转
    ctx.body = {
      code: 1,
      message: '授权错误',
      url: 'http://'
    }
    ctx.status = 200
  },
  cookieDomain: 'localhost' // 更新cookie domain
  cookieMaxAge: 30 * 60 * 1000 // 更新cookie时间(30分钟)
}))
  1. 根据权限控制访问 demo: 在 router 里添加
import { allow } from 'elephant-auth'
router.get('/user/:username', allow('管理员', '开发'), UserController.getUserByName)

allow('管理员', '开发') 表示此 url 只允许 管理员 和 开发 角色访问 非以上角色访问会默认返回(后面会开放自定义处理函数): 200

{ code: 1, message: '该用户没有权限执行此操作' }

ban('实习') 表示此 url 不允许 实习 角色访问 非以上角色访问会默认返回:200

{ code: 1, message: '该用户没有权限执行此操作' }
1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago