# react-native-aws-cognito-js

> React Native AWS Cognito JS library.

Latest version **0.0.7** (published 2017-09-05) · SEE LICENSE IN LICENSE license · 0 weekly downloads

## Install

```sh
npm install react-native-aws-cognito-js
pnpm add react-native-aws-cognito-js
yarn add react-native-aws-cognito-js
bun add react-native-aws-cognito-js
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.7 |
| Published | 2017-09-05 |
| First published | 2017-04-25 |
| Weekly downloads | 0 |
| License | SEE LICENSE IN LICENSE |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 132 |
| Author | Jon Parsons |
| Maintainers | jmparsons |
| Keywords | react-native, amazon, aws, cognito, identity |

## Links

- npm: https://www.npmjs.com/package/react-native-aws-cognito-js
- Repository: https://github.com/AirLabsTeam/react-native-aws-cognito-js
- Issues: https://github.com/AirLabsTeam/react-native-aws-cognito-js/issues
- npm.io page: https://npm.io/package/react-native-aws-cognito-js

## Dependencies (1)

- [aws-sdk](https://npm.io/package/aws-sdk.md) *

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 0.0.7 (latest) — 2017-09-05
- 0.0.6 — 2017-08-07
- 0.0.5 — 2017-06-13
- 0.0.4 — 2017-05-04
- 0.0.3 — 2017-04-26
- 0.0.2 — 2017-04-25
- 0.0.1 — 2017-04-25

## README

## react-native-aws-cognito-js [![npm version](https://badge.fury.io/js/react-native-aws-cognito-js.svg)](http://badge.fury.io/js/react-native-aws-cognito-js)

This is an adaptation of [Amazon Cognito Identity SDK for JavaScript](https://github.com/aws/amazon-cognito-identity-js) in combination with [AWS SDK for JavaScript](https://github.com/aws/aws-sdk-js/tree/react-native) for [React Native](https://github.com/facebook/react-native/).

This project uses Native Modules to handle intensive math operations on the device using the React Native bridge.

## Installation

Install in your application directory:

```bash
npm install --save react-native-aws-cognito-js
```

or

```bash
yarn add react-native-aws-cognito-js
```

Link project:

```bash
react-native link react-native-aws-cognito-js
```

## MemoryStorage

The local MemoryStorage can handle everything on the authentication side, but the data is lost when the app is reopened. The storage sync method will grab only the MemoryStorage data from AsyncStorage and update the local MemoryStage with the values.

The new sync method can be used from either the userPool or cognitoUser:

```js
userPool.storage.sync((err, result) => {
  // MemoryStorage is now updated with AsyncStorage values
});
```

or

```js
cognitoUser.storage.sync((err, result) => {
  // MemoryStorage is now updated with AsyncStorage values
});
```

## Usage

Refer to the Amazon Cognito Identity SDK for JavaScript usage [examples](https://github.com/aws/amazon-cognito-identity-js#usage).

## Example

This is a user authentication sample:

```js
//imports
import {
  Config,
  CognitoIdentityCredentials
} from 'aws-sdk/dist/aws-sdk-react-native';

import {
  AuthenticationDetails,
  CognitoUser,
  CognitoUserPool,
  CognitoUserAttribute
} from 'react-native-aws-cognito-js';

const appConfig = {
  region: '',
  IdentityPoolId: '',
  UserPoolId: '',
  ClientId: '',
}

//setting config
Config.region = appConfig.region;

//component:
state = {
  username: '',
  password: '',
}

login = () => {
  const { username, password } = this.state;
  const authenticationData = {
    Username: username,
    Password: password,
  };
  const authenticationDetails = new AuthenticationDetails(authenticationData);
  const poolData = {
    UserPoolId: appConfig.UserPoolId,
    ClientId: appConfig.ClientId
  };
  const userPool = new CognitoUserPool(poolData);
  const userData = {
    Username: username,
    Pool: userPool
  };
  const cognitoUser = new CognitoUser(userData);
  cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: (result) => {
      console.log('access token + ' + result.getAccessToken().getJwtToken());
      Config.credentials = new CognitoIdentityCredentials({
        IdentityPoolId: appConfig.IdentityPoolId,
        Logins: {
          [`cognito-idp.${appConfig.region}.amazonaws.com/${appConfig.UserPoolId}`]: result.getIdToken().getJwtToken()
        }
      });
      alert('Success');
      console.log(Config.credentials);
    },
    onFailure: (err) => {
      alert(err);
    },
  });
}
```

## Advanced Example

```js
//- AWS.js
//create AWS with userPool using dotenv to hold environment variables
import AWS, { CognitoIdentityServiceProvider } from 'aws-sdk/dist/aws-sdk-react-native';
import * as enhancements from 'react-native-aws-cognito-js';
import { AWS_REGION, AWS_POOL_ID, AWS_POOL_CLIENT_ID } from 'react-native-dotenv';

Object.keys(enhancements).forEach(key => (CognitoIdentityServiceProvider[key] = enhancements[key]));

AWS.config.update({ region: AWS_REGION });

export const poolData = {
  UserPoolId: AWS_POOL_ID,
  ClientId: AWS_POOL_CLIENT_ID,
};

export const userPool = new AWS.CognitoIdentityServiceProvider.CognitoUserPool(poolData);

export default AWS;
```

```js
//import AWS and userPool
import AWS, { userPool } from './AWS';

//component:
state = {
  username: '',
  password: '',
}

login = () => {
  const { username, password } = this.state;
  const authenticationData = { Username: username, Password: password };
  const authenticationDetails = new AWS.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
  const userData = { Username: username, Pool: userPool };
  const cognitoUser = new AWS.CognitoIdentityServiceProvider.CognitoUser(userData);
  cognitoUser.authenticateUser(authenticationDetails, {
    onSuccess: (result) => {
      console.log('result', result);
      console.log('access token:', result.getAccessToken().getJwtToken());
    },
    onFailure: (error) => {
      Alert.alert(error.code, error.message);
    },
  });
}
```

---
_Source: https://npm.io/package/react-native-aws-cognito-js · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
