# elephant-auth

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

Latest version **1.0.6** (published 2019-11-19) · MIT license · 0 weekly downloads

## Install

```sh
npm install elephant-auth
pnpm add elephant-auth
yarn add elephant-auth
bun add elephant-auth
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2019-11-19 |
| First published | 2019-11-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8.9.0 |
| Dependencies | 3 |
| Unpacked size | 9.4 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | Jamie |
| Maintainers | yzhijun |
| Keywords | jwt |

## Links

- npm: https://www.npmjs.com/package/elephant-auth
- Repository: https://github.com/yzhiJun/elephant_auth
- Homepage: https://github.com/yzhiJun/elephant_auth#readme
- Issues: https://github.com/yzhiJun/elephant_auth/issues
- npm.io page: https://npm.io/package/elephant-auth

## Dependencies (3)

- [joi](https://npm.io/package/joi.md) ^10.6.0
- [debug](https://npm.io/package/debug.md) ^3.0.1
- [koa-jwt](https://npm.io/package/koa-jwt.md) ^3.2.2

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 1.0.6 (latest) — 2019-11-19
- 1.0.5 — 2019-11-19
- 1.0.4 — 2019-11-15
- 1.0.3 — 2019-11-14
- 1.0.2 — 2019-11-14

## README

# 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分钟)
}))
```

2. 根据权限控制访问
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: '该用户没有权限执行此操作' }
```

---
_Source: https://npm.io/package/elephant-auth · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
