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

@mitchallen/cognito-login

v0.1.2

Published

@mitchallen/cognito-login == AWS Cognito login module --

Downloads

6

Readme

@mitchallen/cognito-login

AWS Cognito login module

Installation

$ npm init
$ npm install @mitchallen/cognito-login --save

Usage

var factory = require("@mitchallen/cognito-login");

factory.create({
    userPoolId: COGNITO_TEST_USER_POOL_ID,
    clientId: COGNITO_TEST_CLIENT_ID
})
.then( obj => obj.login({
        username: COGNITO_TEST_USER,    
        password: COGNITO_TEST_PASSWORD 
    })
)
.then( token => {
    // console.log(token);
    // user has successfully logged in
    // update state or redux store
})
.catch( err => { 
    console.error(err);
    // login failed 
});

AWS Cognito

https://console.aws.amazon.com/cognito/

Creating a Test User

To create a test user you need to do the following:

  • Install and setup aws-cli (the aws command line interface)
  • Set some environment variables
  • Call an api to signup the test user
  • Call an api to confirm the test users signup

Install and setup aws-cli

TODO:

Set Testing Environment Variables

export COGNITO_TEST_USER_POOL_ID=(Cognito user pool id)
export COGNITO_TEST_CLIENT_ID=(Cognito client id)
export COGNITO_TEST_REGION=(Cognito region)
export COGNITO_TEST_IDENTITY_POOL_ID=(Cognito identity pool id)
export COGNITO_TEST_USER=(user email)
export COGNITO_TEST_PASSWORD=(user password)

On a Mac

Add the lines above to ~/.bash_profile. Then at the command line run this command:

$ source ~/.bash_profile

Signup the user from the command line

On a Mac

To create a test user based on the environment variables, run this from the command line (minus the $):

$ aws cognito-idp sign-up --client-id $COGNITO_TEST_CLIENT_ID --region $COGNITO_TEST_REGION --username $COGNITO_TEST_USER --password $COGNITO_TEST_PASSWORD --user-attributes Name=email,Value=$COGNITO_TEST_USER 

Once the test user is signed up, the next step below is to use an admin command to confirm the user from the command line.

Admin Confirm User Signup from the command line

$ aws cognito-idp admin-confirm-sign-up --user-pool-id $COGNITO_TEST_USER_POOL_ID --region $COGNITO_TEST_REGION --username $COGNITO_TEST_USER

Modules

cognito-login

Module

cognito-login.package()

Returns the package name

Kind: instance method of cognito-login

cognito-login.login(username, password)

Login method.

Kind: instance method of cognito-login

| Param | Type | Description | | --- | --- | --- | | username | string | Cognito user name | | password | string | Cognito user password |

Example (Usage Example)

var factory = require("@mitchallen/cognito-login");

factory.create({
    userPoolId: COGNITO_TEST_USER_POOL_ID,
    clientId: COGNITO_TEST_CLIENT_ID
})
.then( obj => obj.login({
        username: COGNITO_TEST_USER,    
        password: COGNITO_TEST_PASSWORD 
    })
)
.then( token => {
    // console.log(token);
    // user has successfully logged in
    // update state or redux store
})
.catch( err => { 
    console.error(err);
    // login failed 
});

cognito-login-factory

Factory module

cognito-login-factory.create(userPool, userPoolId, clientId) ⇒ Promise

Factory method.

Kind: static method of cognito-login-factory
Returns: Promise - that resolves to {module:cognito-login}

| Param | Type | Description | | --- | --- | --- | | userPool | Object | Cognito user pool | | userPoolId | string | Cognito user pool id | | clientId | string | Cognito client id |

Example (Use existing pool)

var factory = require("@mitchallen/cognito-login");

factory.create({
    userPool: userPool 
})
.then( obj => obj.login({ ... }) )
.catch( err => { 
    console.error(err);
});

Example (Create pool from id's example)

var factory = require("@mitchallen/cognito-login");

factory.create({
    userPoolId: COGNITO_TEST_USER_POOL_ID,
    clientId: COGNITO_TEST_CLIENT_ID
})
.then( obj => obj.login({ ... }) )
.catch( err => { 
    console.error(err);
});

Additional Cognito API Notes

See: http://docs.aws.amazon.com/cli/latest/reference/cognito-idp/index.html#cli-aws-cognito-idp

Forgot Password

$ aws cognito-idp forgot-password --client-id $COGNITO_TEST_CLIENT_ID --username $COGNITO_TEST_USER --region $COGNITO_TEST_REGION

This will cause an email with a verification code to be sent to the user.

To change the password, take the confirmation code from the email and plugin it into this command line, along with the new password parameter:

$ aws cognito-idp confirm-forgot-password  --client-id $COGNITO_TEST_CLIENT_ID --username $COGNITO_TEST_USER --region $COGNITO_TEST_REGION --password (new password) --confirmation-code (verification code) 

Note that for testing you can currently use the old password as the new password, unless Cognito has been configured to now allow that.


Enable a user that has been disabled

$ aws cognito-idp admin-enable-user --user-pool-id $COGNITO_TEST_USER_POOL_ID --region $COGNITO_TEST_REGION --username $COGNITO_TEST_USER

Dealing with FORCE_CHANGE_PASSWORD

See:

  • https://stackoverflow.com/questions/40287012/how-to-change-user-status-force-change-password
  • http://docs.aws.amazon.com/cli/latest/reference/cognito-idp/admin-respond-to-auth-challenge.html

TODO: describe how to get session:

$ aws cognito-idp admin-respond-to-auth-challenge --user-pool-id $COGNITO_TEST_USER_POOL_ID --client-id $COGNITO_TEST_CLIENT_ID --region $COGNITO_TEST_REGION --challenge-name NEW_PASSWORD_REQUIRED --challenge-responses USERNAME=$COGNITO_TEST_USER,NEW_PASSWORD=Test1234! --session (TODO)

Create User

For test users it is simpler to use steps listed far above to just use sign-up followed by admin-confirm-sign-up.

The command below requires a few more hoops to get the user enabled.

$ aws cognito-idp admin-create-user --user-pool-id $COGNITO_TEST_USER_POOL_ID --region $COGNITO_TEST_REGION --username $COGNITO_TEST_USER ----temporary-password (some temp password)

TODO: Instructions for: User must then enter temp password with new password in signup.


Testing

To test, go to the root folder and type (sans $):

$ npm test

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.1.2

  • Can now initialize with existing pool instead of pool and client ids.

Version 0.1.1

  • Fixed formatting error in doc

Version 0.1.0

  • initial release