npm package: @ikonintegration/reactnative-idmclient
Description: IDM Client for React native
Latest version: 5.1.14

# idm-client-react-native ![Node.js Package](https://github.com/ikon-integration/idm-client-react-native/workflows/Node.js%20Package/badge.svg)

IDM Client for React native

### Overall

- ![npm](https://img.shields.io/npm/dy/@ikonintegration/reactnative-idmclient) ![npm](https://img.shields.io/npm/v/@ikonintegration/reactnative-idmclient) ![npm (tag)](https://img.shields.io/npm/v/@ikonintegration/reactnative-idmclient/latest) ![Libraries.io dependency status for latest release, scoped npm package](https://img.shields.io/librariesio/release/npm/@ikonintegration/reactnative-idmclient)
- ![GitHub commit activity](http://sprice.redirectme.net:8555/github/commit-activity/m/ikon-integration/idm-client-react-native)
- ![GitHub last commit](http://sprice.redirectme.net:8555/github/last-commit/ikon-integration/idm-client-react-native)

### Opts
```
{
//General Configs
    apiEndpoint: 'https://api.auth-dev.AA-AA.ca',
    cookiesEndpoint: 'https://auth-dev.AA-AA.ca',
    
	//disable client logs to console
    disableLogs?: false, 
    
    //if false it will add 10 minutes gap to JWT expiration to make sure we renew it before actually using it.
    jwtAPIMode?: false, 

    //auto enrol role, can be an array of roles. After login, client will auto enroll user into specified roles (if not present on logged JWT) and will return login response only after enrollement.
    autoEnrollRole?: 'AAA', 
    
//External auth mode
    externalAuth: true, //should use external authentication (redirect)
    externalAuthDomain?: 'auth-dev.AA-AA.ca', 
    externalAuthPath?: '/login',
    externalValidatePath?: '/validate',
    externalRegistrationPath?: '/register',
    externalProfilePath?: '/profile',
    
//Faciliatators
    //Roles mapping
    roles?: { //Optional roles
      USER: 'AA-AA',
    },
    
	//Partitions mapping
    partitions?: { //Optional partitions mapping
      PROFESSIONAL: 'AA',
    },
  }
```

------
## Configure Cookies Hub on the website

#### Create separated build process
    - `mkdir cookiesHub && cd cookiesHub && npm init -y && npm install --save-dev webpack webpack-cli babel-loader @babel/core @babel/preset-env @babel/runtime @babel/plugin-transform-runtime @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties && npm i -S cross-storage`
    - Add into `cookiesHub/package.json`
      ```
        "scripts": {
            "clean": "rm ../public/hub/index.js",
            "build-dev": "webpack --mode development",
            "build": "webpack --mode production"
          },
      ```

#### Setup Hub build
  - Add into `cookiesHub/babel.config.js`
  ```
  module.exports = function (api) {
    api.cache(false);
    const presets = [
        [ "@babel/preset-typescript"],
        [
            "@babel/preset-env",
            {
                "corejs": { "version": 3 },
                "useBuiltIns": "usage",
                "targets": { "edge": "17", "firefox": "60", "chrome": "67", "safari": "11.1", "ie": "11" }
            }
        ]
    ];
    const plugins = [
        ["@babel/plugin-proposal-decorators",{"decoratorsBeforeExport":true}],
        ["@babel/plugin-proposal-class-properties"],
        ["@babel/transform-runtime"]
    ];
    return { presets, plugins };
  };
  ```

  - Add into `cookiesHub/webpack.config.js`
  ```
  const webpack = require('webpack');
  const path = require('path');
  const config = {
    entry: './index.js',
    output: {
      path: path.resolve(__dirname, '../public/hub'),
      filename: 'index.js'
    },
    module: {
      rules: [
        {
          test: /\.js$/,
          use: 'babel-loader',
          exclude: /node_modules/
        }
      ]
    }
  };

  module.exports = config;
  ```


#### Create hub source
- Create index.html at `public/hub/index.html` with the following content:
```
	<!DOCTYPE html>
	<html lang="en"><head> </head> <body> <script src="./index.js"> </script> </body> </html>
```

- Create `index.js` at `cookiesHub/index.js` with the following contents :
```
  import "core-js/stable";
  //Config
  const config = require('../src/app/config/config');
  //Hub
  const CrossStorageHub = require('cross-storage').CrossStorageHub;
  const DefaultCookiesHubPermissioning = ['get', 'set', 'del'];
  /* Private helper */
  function _transformDomainToHubFormat(domainsList) {
    return domainsList.map((domain) => {
      if (domain.includes('*')) return `\.${domain}$`;
      else return `:\/\/(www\.)?${domain}$`;
    }).map((regex) => {
      return { origin: new RegExp(regex), allow: DefaultCookiesHubPermissioning };
    });
  }
  //Open hub
  const allowedDomains = _transformDomainToHubFormat(Object.keys(config.ThemeDomainsMapping));
  CrossStorageHub.init(allowedDomains);
```


#### Modify your build
- Add `cd cookiesHub && npm i && npm run build` as a npm script. (`build-hub`) 
- Add `npm run build-hub` to the start of your build instructions. (Example package.json:scripts:build)
- Also add `npm run build-hub` to start of you `start` script to allow local usage. 
- This will generate the `/public/hub/index.js` at build time, so include it on `.gitignore`
- Add the following to your `buildspec.yml` file
  - `aws s3 cp --acl public-read --cache-control="max-age=0, no-cache, no-store, must-revalidate" ./hub/index.html s3://${DEPLOY_BUCKET}/hub` 
  - `aws s3 cp --acl public-read --cache-control="max-age=0, no-cache, no-store, must-revalidate" ./hub/index.js s3://${DEPLOY_BUCKET}/hub` 
  - Add `/hub/*` to your cloud front invalidation on `buildpsec.yml`