# ciam-node-sdk

> [产品快速入口](https://console.cloud.tencent.com/ciam)

Latest version **1.1.2** (published 2022-08-02) · 0 weekly downloads

## Install

```sh
npm install ciam-node-sdk
pnpm add ciam-node-sdk
yarn add ciam-node-sdk
bun add ciam-node-sdk
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2022-08-02 |
| First published | 2021-09-08 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 2 |
| Unpacked size | 316.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | shingao |
| Maintainers | shingao |

## Links

- npm: https://www.npmjs.com/package/ciam-node-sdk
- npm.io page: https://npm.io/package/ciam-node-sdk

## Dependencies (2)

- [crypto](https://npm.io/package/crypto.md) ^1.0.1
- [ciam-js-sdk](https://npm.io/package/ciam-js-sdk.md) ^1.1.2

## Recent versions

- 1.1.2 (latest) — 2022-08-02
- 1.1.1-beta.14 (beta) — 2022-03-09
- 1.1.1 — 2022-03-10
- 1.1.1-beta.13 — 2022-03-08
- 1.1.1-beta.12 — 2022-03-07
- 1.1.1-beta.11 — 2022-03-07
- 1.1.1-beta.10 — 2022-03-07
- 1.1.1-beta.7 — 2022-03-07
- 1.1.1-beta.5 — 2022-03-07
- 1.1.1-beta.3 — 2022-03-07
- 1.1.1-beta.2 — 2022-03-04
- 1.1.1-beta.0 — 2022-03-04
- 1.0.0 — 2021-10-09
- 1.0.0-beta.3 — 2021-09-08

## README

### 一、CIAM控制台链接
[产品快速入口](https://console.cloud.tencent.com/ciam)


### 二、SDK概览
ciam-node-sdk 提供以下登录鉴权方法：

|方法名称 |描述 |备注 |
|:----|:----|:----|
|generateLoginUrl |生成认证登录URL |- |
|fetchToken |通过code获取token |- |
|getUser| 获取用户信息 | - | 
|logout |退出登录 |- |

### 三、SDK接口说明
#### 1. 初始化
- 代码示例
```
const { NodeClient } = require('ciam-node-sdk'); // node-sdk
const ciam = new NodeClient({
  clientId: ‘your-clientid', // 此处为CIAM的应用ID，CIAM应用中获取
  userDomain: 'your-userDomain', // 此处为租户域名，CIAM域名管理中获取
  redirectUri:'your-redirectUri', // 此处为回调地址，CIAM应用管理中获取
  logoutRedirectUrl: 'your-logoutRedirectUrl', // 此处为退出回调地址，CIAM应用管理中获取
  scopes: ['openid'],
  protocol: 'OIDC_PKCE',
});
```

- 参数说明

|参数名 |类型 |是否必填 |长度限制 |描述 |
|:----|:----|:----|:----|:---- |
|clientId |string |是 |- |管理端添加的小程序应用ID | 
|userDomain |string |是 |- |租户域名（自定义域名获取） |
|redirectUri |string |是 |- |登录成功后跳转的URL |
|logoutRedirectUrl |string | 是 |- |登录退出后跳转的URL |
|scopes |array[Agreement] |是 |- | 遵循oauth2.0规范，默认为openid |
|protocol |string |否 |- |OIDC_PKCE（默认）、OIDC_DEFAULT |


#### 2. 生成认证登录URL
用于生成登录URL，实现快速跳转到CIAM登录页面。
- 代码示例
```
// 登录CIAM
app.get('/login', async (req, res) => {
  const url = await ciam.generateAuthUrl();
  res.redirect(url);
})
```

#### 3. 获取token和用户信息
该方法用于当CIAM登录成功时，根据页面返回的code调用fetchToken，获取到的token用于获取用户信息。
- 代码示例
```
// 处理redirect回调
app.get('/callback', async (req, res) => {
  const { code } = req.query;
  const result = await ciam.fetchToken(code);
  const { access_token, id_token } = result;
  const userInfo = await ciam.getUser();
  req.session.user = { ...userInfo };
  res.redirect('/');
})
```
- fetchToken参数说明

|参数名 |类型 |是否必填 |长度限制 |描述 |
|:----|:----|:----|:----|:---- |
| code |string | 是 | 与CIAM认证返回的code相同 |   该参数由CIAM托管并返回  |


#### 4. 退出登录
该方法用于退出CIAM登录。
- 代码示例

```
// 退出CIAM登录
app.get('/logout', async (req, res) => {
  if (!req.session.user) {
    res.redirect('/');
  }
  const url = await ciam.logout();
  req.session.destroy();
  res.redirect(url);
})
```

### sample下载
TODO


### 更新日志
1.1.0 
breakchange 去掉pkce模式

1.0.0
feat 支持pkce、授权码模式

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