AWS Cognito for Web
A browserified clientside library for authenticating users with AWS Cognito. It was designed and tested to be used with auth0 but other authentication providers should work just fine.
When combined with the auth0 lock widget, you can set up an entire secure, serverless backend with nearly no effort on your part.
Getting Started
Prerequisties: AWS account, auth0 account; rudimentary knowledge of both
Config
- Ignore SAML auth0 + Cognito. I've never been able to get it to work.
- Follow this tutorial for setting up auth0 + cognito: https://auth0.com/docs/scenarios/amazon-cognito
- (Optional) Edit the generated Cognito auth identity and add the ability to put objects to an S3 bucket
Setting up aws-cognito-for-web (AuthenticationClient)
Set up a browserfied project. (Details on how to do that are outside scope here.)
npm install auth0-lock aws-cognito-for-web --save
// main.js var { s3; }; var Auth0Lock = ;var AuthenticationClient = ; var identityPoolId = 'your cognito identity pool id';// this was (probably) automatically created when you created your identity poolvar authRoleArn = 'your cognito authenticated role arn'; // this is your auth0 account. it will match what you set up in the auth0 + cognito tutorial mentioned abovevar auth0Endpoint = '[your host].auth0.com'; var lock = 'the auth0 app client id' auth0Endpoint; // AuthenticationClient.resumeSession is sugar that handles serializing/unserialzing the user// session and all the required bits to make everything work.var auth = AuthenticationClient; auth; auth;
Setting up env for tests
Some tests have been built to run against a live auth0 and AWS account. If you run tests yourself without doing some configuration you will see failed tests.
Setting up test env
In order to set up your local env to run the tests:
- Copy
.env-template
to.env
- Enter all the required info
- run
npm test
If you entered valid info and your live environment has been properly configured all tests should pass.
.env
example
TEST_COGNITO_POOL=us-east-1:c52axxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxTEST_COGNITO_POOL_REGION=us-east-1TEST_COGNITO_ROLE=arn:aws:iam::170xxxxxxxxx:role/Cognito_pool_for_auth_testsAuth_RoleTEST_AUTH0_DOMAIN=example.auth0.comTEST_AUTH0_CLIENT=Hnc4yxxxxxxxxxxxxxxxxxxxx# in your auth0 app, create a user and enter the info here TEST_AUTH0_USER=auth_tests@example.comTEST_AUTH0_PASS=password1234# the auth0 connection to use for auth (probably just the auth0 users database) TEST_AUTH0_CONN=for-auth-tests