README.md 1.2 KB
Newer Older
赵培然's avatar
赵培然 committed
1 2 3 4
# egg-kpauth

## Install

赵培然's avatar
赵培然 committed
5 6
安装最新的tag下的安装包

赵培然's avatar
赵培然 committed
7
```bash
赵培然's avatar
赵培然 committed
8
$ yarn add https://code.keepwork.com/open/egg-kpauth/-/archive/v1.0.1/egg-kpauth-v1.0.1.tar.gz
赵培然's avatar
赵培然 committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
```

## Usage

```js
// {app_root}/config/plugin.js
exports.kpauth = {
	enable: true,
	package: "egg-kpauth",
};
```

## Configuration

```js
赵培然's avatar
赵培然 committed
24
// {app_root}/config/config.{env}.js
赵培然's avatar
赵培然 committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38
exports.kpauth = {
	coreserviceURL: "http://10.28.18.44:3001/mock/32",
	INTERNAL_API_KEY: "test",
};
```

see [config/config.default.js](config/config.default.js) for more detail.

## Example

<!-- example here -->

In controllers:

赵培然's avatar
赵培然 committed
39

赵培然's avatar
赵培然 committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
### 用户身份校验

> 校验失败会抛错,返回状态码 401

```JS
const user = this.ctx.authenticated();
```

### 管理员身份校验

```JS
const admin = this.ctx.adminAuthenticated();
```

### 刷新或者更换 token

```JS
const token = await this.ctx.refreshToken(oldToken, tokenBody);
```

旧的 token 会失效,tokenBody 里面必要参数为`userId`,要保证`userId`与 oldToken 一致,否则也会返回 401

赵培然's avatar
赵培然 committed
62 63 64 65 66 67
### 异步校验token

```JS
const user = await this.ctx.authenticatedAsync(token);
```

赵培然's avatar
赵培然 committed
68 69 70 71 72 73 74
## Questions & Suggestions

Please open an issue [here](https://github.com/eggjs/egg/issues).

## License

[MIT](LICENSE)