# Approw - Node.js/JavaScript

Approw JavaScript/Node SDK consists of two parts: `ManagementClient` and `AuthenticationClient`. `AuthenticationClient` includes methods such as registering and logging in, resetting mobile phone number email, modifying account information, etc. It sends requests based on end user and is suitable for browsers and back-end use. `ManagementClient` is suitable for use in a back-end or **trusted** front-end environment.Generally speaking, all the operations you can do in the [Approw console](https://console.approw.com/console/userpool) can be done with this SDK.

## Install

Use `npm`:

```
npm install approw-js-sdk
```

Use `yarn`:

```
yarn add approw-js-sdk
```

> If you want to use it in the React Native environment, you need to first run this command in the RN project root directory:

```
npx rn-nodeify --install "crypto,stream"
```

> After that, a `shim.js` file will be generated in the project root directory. Then import shim.js in the first line of App.js.

```
import './shim.js'
```

Use CDN:

```html
<script src="https://cdn.jsdelivr.net/npm/approw-js-sdk/build/browser/index.min.js"></script>

<script>
  /** You can use global variable Approw to acquire AuthenticationClient and ManagementClient */
  var approw = new Approw.AuthenticationClient({
    appId: 'APPROW_APP_ID',
  });
</script>
```

## Use authentication client

### Initialize

Initialization of `AuthenticationClient` needs `AppId`：

> You can check your application list in **Applications** on your console.

```js
import { AuthenticationClient } from 'approw-js-sdk';

const approw = new AuthenticationClient({
  appId: 'YOUR_APP_ID',
});
```

The complete parameter list is as follows:

- `appId`: {{$localeConfig.brandName}} application ID (required);
- `appDomain`: The host address of {{$localeConfig.brandName}} application (required), such as `https://my-awesome-app.approw.com`;
- `token`: Initialize the SDK through the user's `id_token` (optional, you can cache the user `id_token` in the front-end localStorage to implement remember the login).
- `timeout`: Request timeout time, in milliseconds, the default is 10000 (10 seconds).
- `onError`: Error handling function, you can use it to globally catch all exceptions requested by the {{$localeConfig.brandName}} client.See [this document](https://docs.approw.com/reference/error-code.html) for the complete error code. The function is defined as:

```js
(code: number, message: string, data: any) => void
```

- `host`: Approw server address.If you are using the public cloud version, please ignore the parameter. If you are using a privatized deployment version, this parameter is required.The format is as follows: `https://approw-api.mydomain.com` (without / at the end).
- `preflight`: Precheck of whether the network status enabled or not, the default is false.This parameter is suitable for checking whether the Approw server domain name is blocked on the user's network (some companies' intranets will block third-party websites). If the check is successful, it won't send any notification. The error handling function will be called if the check fails. After performing the pre-check, the SDK initialization speed will slow down. Please use it with caution.
- `cdnPreflight`: Precheck of whether the network status enabled or not, the default is false. This parameter is suitable for checking whether the user's network can connect to Qiniu Cloud CDN (it is not accessible in some scenarios such as using proxy). If the check is successful, it won't send any notification. The error handling function will be called if the check fails. After performing the CDN pre-check, the SDK initialization speed will slow down. Please use it with caution.

### Instrcutions

If you use the SDK in a browser environment, after the user logs in, the SDK will write the user's `token` to `localStorage`, and subsequent requests will carry the `token` for access.

```js
const email = 'test@example.com';
const password = 'passw0rd';
const user = await approw.loginByEmail(email, password); // store the token in localStorage after successfully logging in

// this operation can be performed after logging in
await approw.updateProfile((nickname: 'Bob'));
```

### Social login

Send an authorized login request through `authenticationClient.social.authorize`. This method will directly open a new window and redirect to the login authorization page of a third-party social login service provider (such as GitHub etc.). After the user completes the authorization,this window will be automatically closed, and the `onSuccess` callback function will be triggered. Through this function, you can get user information.

Example：

```javascript
const authenticationClient = new AuthenticationClient({
  appId: 'YOUR_APP_ID',
});

await authenticationClient.social.authorize('github', {
  onSuccess: (user) => {
    console.log(user);
  },
  onError: (code, message) => {},
  // customize the postion where the window pops up
  position: {
    w: 100,
    h: 100,
  },
});
```

<details><summary><b>View the list of supported social logins and introducing procedures</b></summary>

Approw currently supports more than 20 social logins around the world, such as GitHub, Sign in with Apple, Google, etc. The following is a complete list:

!!!include(common/social-connections-table.md)!!!

</details>

### App QR code login

App QR code login refers to use your own App scan QR code to log in to the website, [click here to learn more](https://docs.approw.com/guides/authentication/qrcode/use-self-build-app/).

You can use 5 lines of code to implement a complete scan code login form:

```js
authenticationClient.qrcode.startScanning('qrcode', {
  onSuccess: (userInfo, ticket) => {
    console.log(userInfo, ticket);
  },
});
```

### ClientList

- [AuthenticationClient](https://docs.approw.com/reference/sdk-for-node/authentication/AuthenticationClient.html)
- [QrCodeAuthenticationClient](https://docs.approw.com/reference/sdk-for-node/authentication/QrCodeAuthenticationClient.html)
- [MfaAuthenticationClient](https://docs.approw.com/reference/sdk-for-node/authentication/MfaAuthenticationClient.html)
- [SocialAuthenticationClient](https://docs.approw.com/reference/sdk-for-node/authentication/SocialAuthenticationClient.html)

## Use ManagementClient

### Initialize

`ManagementClient` initialization needs to pass in the user pool ID `userPoolId` and the user pool key `secret`：

> You can learn [how to get userPoolId and secret here](https://docs.approw.com/guides/faqs/get-userpool-id-and-secret.html).

```js
import { ManagementClient } from 'approw-js-sdk';

const managementClient = new ManagementClient({
  userPoolId: 'YOUR_USERPOOL_ID',
  secret: 'YOUR_USERPOOL_SECRET',
});
```

The complete parameter list is as follows:

- `userPoolId`: User pool ID.
- `secret`: the secret of user pool.
- `accessToken`: Initialize the SDK with the administrator's token. (Optional, **one of secret and accessToken must be filled in**.)
- `timeout`: Request timeout time, in milliseconds, the default is 10000 (10 seconds).
- `onError`: Error handling function, you can use it to globally catch all exceptions requested by the Approw client. The function is defined as:

```js
(code: number, message: string, data: any) => void
```

> See [this document](https://docs.approw.com/reference/error-code.html) for the complete error code.

- `host`: Approw server address.If you are using the public cloud version, please ignore the parameter. If you are using a privatized deployment version, this parameter is required.The format is as follows: https://approw-api.mydomain.com (without / at the end).
- `preflight`: Precheck of whether the network status enabled or not, the default is false.This parameter is suitable for checking whether the Approw server domain name is blocked on the user's network (some companies' intranets will block third-party websites). If the check is successful, it won't send any notification. The error handling function will be called if the check fails. After performing the pre-check, the SDK initialization speed will slow down. Please use it with caution.
- `cdnPreflight`: Precheck of whether the network status enabled or not, the default is false. This parameter is suitable for checking whether the user's network can connect to Qiniu Cloud CDN (it is not accessible in some scenarios such as using proxy). If the check is successful, it won't send any notification. The error handling function will be called if the check fails. After performing the CDN pre-check, the SDK initialization speed will slow down. Please use it with caution.

### Instructions

`ManagementClient` can be used to manage users, roles, policies, groups, organizations, and user pool configuration. In theory, any operation you can do in the [Approw console](https://console.approw.com/console/userpool) can be done with this SDK.

Get a list of user directories:

```js
// list of the users in current page
// totalCount of users
const { list, totalCount } = await managementClient.users.list();
```

Create roles:

```js
const role = await managementClient.roles.create('code', 'role name');
```

Modify the user pool configuration:

```js
const userpool = await managementClient.userpool.update({
  registerDisabled: true, // disable the register function of user pool
});
```

### Client list

- [UsersManagementClient](https://docs.approw.com/reference/sdk-for-node/management/UsersManagementClient.html) 
- [RolesManagementClient](https://docs.approw.com/reference/sdk-for-node/management/RolesManagementClient.html) 
- [GroupsManagementClient](https://docs.approw.com/reference/sdk-for-node/management/GroupsManagementClient.html) 
- [OrgManagementClient](https://docs.approw.com/reference/sdk-for-node/management/OrgManagementClient.html) 
- [PoliciesManagementClient](https://docs.approw.com/reference/sdk-for-node/management/PoliciesManagementClient.html) 
- [AclManagementClient](https://docs.approw.com/reference/sdk-for-node/management/AclManagementClient.html) 
- [UdfManagementClient](https://docs.approw.com/reference/sdk-for-node/management/UdfManagementClient.html) 
- [WhitelistManagementClient](https://docs.approw.com/reference/sdk-for-node/management/WhitelistManagementClient.html) 
- [UserPoolManagementClient](https://docs.approw.com/reference/sdk-for-node/management/UserpoolManagementClient.html) 

## Error handling

You can use `try catch` for error handling:

```js
try {
  const user = await approw.loginByEmail('test@example.com', 'passw0rd');
} catch (error) {
  console.log(error.code); // 2004
  console.log(error.message); // user does not exist
}
```

> You can find the whole error code document [here](https://docs.approw.com/reference/error-code.html).

You can also specify `onError` to uniformly capture all Approw request exceptions, such as using front-end components like `antd` to display error prompts.

```js
import { message } from 'antd';
const approw = new AuthenticationClient({
  userPoolId,
  onError: (code, msg: any) => {
    message.error(msg);
  },
});
```

## Privatization deployment

The **privatization deployment** scenario needs to specify the GraphQL endpoint of your privatized Approw service (**without protocol header and Path**). If you are not sure about it, you can contact the Approw IDaaS service administrator.

```js
import { AuthenticationClient, ManagementClient } from 'approw-js-sdk';

const authenticationClient = new AuthenticationClient({
  appId: 'YOUR_APP_ID',
  host: 'https://core.you-approw-service.com',
});

const managementClient = new ManagementClient({
  userPoolId: 'YOUR_USERPOOL_ID',
  secret: 'YOUR_USERPOOL_SECRET',
  host: 'https://core.you-approw-service.com',
});
```

## Interface index

Authentication clients：

[AuthenticationClient](https://docs.approw.com/reference/sdk-for-node/authentication/AuthenticationClient.html)

[QrCodeAuthenticationClient](https://docs.approw.com/reference/sdk-for-node/authentication/QrCodeAuthenticationClient.html)

[MfaAuthenticationClient](https://docs.approw.com/reference/sdk-for-node/authentication/MfaAuthenticationClient.html)

[SocialAuthenticationClient](https://docs.approw.com/reference/sdk-for-node/authentication/SocialAuthenticationClient.html)

Management clients：

[UsersManagementClient](https://docs.approw.com/reference/sdk-for-node/management/UsersManagementClient.html)

[RolesManagementClient](https://docs.approw.com/reference/sdk-for-node/management/RolesManagementClient.html)

[PoliciesManagementClient](https://docs.approw.com/reference/sdk-for-node/management/PoliciesManagementClient.html)

[AclManagementClient](https://docs.approw.com/reference/sdk-for-node/management/AclManagementClient.html)

[GroupsManagementClient](https://docs.approw.com/reference/sdk-for-node/management/GroupsManagementClient.html)

[OrgManagementClient](https://docs.approw.com/reference/sdk-for-node/management/OrgManagementClient.html)

[UdfManagementClient](https://docs.approw.com/reference/sdk-for-node/management/UdfManagementClient.html)

[WhitelistManagementClient](https://docs.approw.com/reference/sdk-for-node/management/WhitelistManagementClient.html)

[UserpoolManagementClient](https://docs.approw.com/reference/sdk-for-node/management/UserpoolManagementClient.html)

## Get Help

Join us on Gitter: [#approw-chat](https://gitter.im/approw-chat/community)

