npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-aws-cognito-js

v0.0.7

Published

React Native AWS Cognito JS library.

Downloads

1,103

Readme

react-native-aws-cognito-js npm version

This is an adaptation of Amazon Cognito Identity SDK for JavaScript in combination with AWS SDK for JavaScript for 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:

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

or

yarn add react-native-aws-cognito-js

Link project:

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:

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

or

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

Usage

Refer to the Amazon Cognito Identity SDK for JavaScript usage examples.

Example

This is a user authentication sample:

//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

//- 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;
//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);
    },
  });
}