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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mitchallen/react-cognito-auth-user

v0.2.1

Published

Cognito AuthUser React method

Readme

@mitchallen/react-cognito-auth-user

Cognito AuthUser React method

Installation

$ npm init
$ npm install @mitchallen/react-cognito-auth-user --save

Usage

Combine the code below with example code from @mitchallen/react-cognito-login.

import AWS from "aws-sdk";
import authUser from "@mitchallen/react-cognito-auth-user";

class App extends Component {

  constructor(props) {
    super(props);

    this.state = {
      isAuthenticated: false,
      isAuthenticating: true,
      isLoadingUserToken: true,
      isLoading: false
    };
  }

  async componentDidMount() {
    try {
      if (await authUser({
        AWS: AWS, 
        userPoolId: USER_POOL_ID,
        clientId: APP_CLIENT_ID,
        region: REGION, 
        identyPoolId: IDENTITY_POOL_ID
      })) {
        this.userHasAuthenticated(true);
      }
    }
    catch(e) {
      alert(e);
    }
  
    this.setState({ isAuthenticating: false });
  }

  userHasAuthenticated = authenticated => {
    this.setState({ isAuthenticated: authenticated });
  }
  
  render() {

    return (
      <div className="App">
		...
      </div>
    );
  }
}

Behind the Scenes

Behind the scenes the AWS.config.credentials are updated using code similar to what is listed below.

Call authUser just before making a call to Amazon Services (for example S3) to set the proper credentials.

const authenticator = `cognito-idp.${region}.amazonaws.com/${userPoolId}`;

AWS.config({ region: region });

AWS.config = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: identyPoolId,
    Logins: {
        [authenticator]: userToken
    }
});

Example S3 Call

This example shows how to use authUser to make an S3 call.

import authUser from "@mitchallen/react-cognito-auth-user";

export default async function s3GetTextFile( params ) {

  let { AWS, file, bucket, ...rest } = params;

  if (!await authUser( { AWS, ...rest } )) {
    throw new Error("User is not logged in");
  }
    
  const s3 = new AWS.S3({
    params: {
      Bucket: bucket
    }
  });
          
  return s3.getObject({
    Bucket: bucket,
      Key: file
  })
  .promise()
  .then( (data) => data.Body.toString('utf-8') );
}

You would call the above like this:

const BUCKET_FILE = 'cognito/demo/demo.txt';

s3GetTextFile({ 
    AWS: AWS,
    bucket: S3_BUCKET,
    file: BUCKET_FILE, 
    userPoolId: USER_POOL_ID,
    clientId: APP_CLIENT_ID,
    region: REGION, 
    identyPoolId: IDENTITY_POOL_ID
})
.then((data) => {
    alert(data);
})
.catch(function(err) {
    alert(err);
});

Repo(s)


Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.


Version History

Version 0.2.1

  • updated documentation for change in parameters

Version 0.2.0

  • Now requires AWS parameter
  • No longer accepts awsConfig parameter

Version 0.1.4

  • fixed AWS.config reference

Version 0.1.3

  • cleaned up some build scripts and dependencies

Version 0.1.2

  • added first pass at documentation

Version 0.1.1

  • removed aws-sdk dependency
  • AWS.config must now be passed in as awsConfig

Version 0.1.0

  • initial release