sst.dev icon indicating copy to clipboard operation
sst.dev copied to clipboard

Comments: Login with AWS Cognito

Open jayair opened this issue 9 years ago • 39 comments

Link to chapter - http://serverless-stack.com/chapters/login-with-aws-cognito.html

jayair avatar Apr 10 '17 01:04 jayair

I would love it if you added a chapter on logging in with facebook account. Do you guys know of any good tuts that would help walk us through this?

auz1111 avatar Jun 01 '17 23:06 auz1111

@auz1111 Yeah it has been on our list.

For the tutorial, I'd have to look for one. You need to use Cognito Federated Identities instead of the User Pool to manage your user accounts.

jayair avatar Jun 03 '17 11:06 jayair

I'm starting to look into this also. It seems that there is a mapping ability to map the different federated identities together but I'm not exactly sure yet how to do this in a way that makes sense.

rogueturnip avatar Jun 08 '17 15:06 rogueturnip

@rogueturnip Yeah it is quite confusing to wrap your head around the set up. But that is how it is supposed to work. Federated identities pulls all your different identity sources together to give you a consolidated view of all your users, regardless of where they are coming from.

jayair avatar Jun 08 '17 15:06 jayair

If I'm understanding it correctly it seems you login the first time and that caches the credentials. Then when a login is done using something like facebook you would do a credentials.get to see if there are existing credentials and then when you do the Login Map you would send in both. Am I following the flow right or completely off base? I can't find an example so I'm piecing together things from the AWS docs.

rogueturnip avatar Jun 08 '17 15:06 rogueturnip

Hmm I'm not completely sure if I follow the flow you are talking about. It is a bit complicated and not super well documented.

jayair avatar Jun 09 '17 15:06 jayair

It's possible I"m messing up what I think the process is. No where is this documented very well.

rogueturnip avatar Jun 09 '17 15:06 rogueturnip

Getting "Error: Network Failure" out of the AWS xhr.js at this point. Have verified the correct username & password, they work from the console with aws cognito-idep admin-initiate-auth. Have verified the right user pool & client IDs in config.js. Not sure what to look at next...

ghost avatar Jul 11 '17 15:07 ghost

Figured it out. NoScript stepped in to block amazonaws.com. That'd do it.

ghost avatar Jul 11 '17 15:07 ghost

+1 for Federated Identities chapter.

eng1n88r avatar Aug 22 '17 06:08 eng1n88r

Great tutorial! I've been through all the content and now building my app by customising the code produced in this tutorial. I just have question on how we can dynamically configure the app to use specific config.js file for different stages. I'm using the created config.js in this tutorial for production environment. Config.js should be different on dev or test environment like the user pool & app client id. It should be somewhere in build time, right?

Thanks in advance!

triunm avatar Oct 17 '17 22:10 triunm

@triunm I was planning on writing on this but a simple way to handle this is as follows.

In your config.js use something like this:

const dev = {
  apiGateway: {
    REGION: 'us-east-1',
    URL: 'https://12345678.execute-api.us-east-1.amazonaws.com/staging',
  },
  cognito: {
    REGION: 'us-east-1',
    USER_POOL_ID : 'us-east-1_123456678',
    APP_CLIENT_ID : '4xxxxxxxxxxxxxxxxxxxx',
    IDENTITY_POOL_ID: 'us-east-1:12345678-1234-1233-9387-1234567890123',
  }
};

const prod = {
  apiGateway: {
    REGION: 'us-east-1',
    URL: 'https://567890.execute-api.us-east-1.amazonaws.com/prod',
  },
  cognito: {
    REGION: 'us-east-1',
    USER_POOL_ID : 'us-east-1_4444444',
    APP_CLIENT_ID : '5xxxxxxxxxxxxxxxxxx',
    IDENTITY_POOL_ID: 'us-east-1:456789-1234-1234-4567-123456789',
  }
};

const config = process.env.REACT_APP_STAGE === 'production'
  ? prod
  : dev;

export default {
  ...config
};

And in your package.json set your scripts as:

  "scripts": {
    "start": "REACT_APP_STAGE=dev react-scripts start",
    "build": "REACT_APP_STAGE=production react-scripts build"
  }

You can extend this idea to have other environments. This is based on the idea of custom environment variables in Create React App.

jayair avatar Oct 19 '17 17:10 jayair

I'm leaving this comment just because I had a hard time finding out what was a problem when I faced 502 error(cors problem).

In that case, there are three possible options. Firstly, you can look at '/libs/response-lib.js' and check whether the function contains 'Access-Control-Allow-Origin' header or not. Secondly, you may have miss indented serverless.yml. Or lastly, you may have forgotten deploying your functions as I did ...

I could see my notes after deploying the functions. If you have same issue like me, you can re-deploy your functions and it might help you get out of trouble.

leejh3224 avatar Nov 28 '17 16:11 leejh3224

@leejh3224 Thanks for leaving your comment here.

jayair avatar Nov 28 '17 20:11 jayair

Hi,

I have reached a point in the splendid tutorial where a first time app-client user should login in for the first time, having his email already verified.

The login function from the Login.js is executed and comes back with a new challenge 'NEW_PASSWORD_REQUIRED'.

The tutorial says nothing about this and assumes the login succeeds?

So I added a function newPasswordRequired: function(userAttributes, requiredAttributes)

-according to http://docs.aws.amazon.com/cognito/latest/developerguide/using-amazon-cognito-identity-user-pools-javascript-example-authenticating-admin-created-user.html-

but i can not get that to work. How should i proceed?

Is the tutorial somewhat outdated regarding this feature? The Login.js in the tutorial does not have the newPasswordRequired method nor are there any problems mentioned at the end of the chapter.

Thnx,

Oscar Henneken

ohenneken avatar Dec 14 '17 19:12 ohenneken

@ohenneken Hmm you shouldn't need to do this. If you've followed this chapter - https://serverless-stack.com/chapters/create-a-cognito-test-user.html

Then you user should be able to login without having to set a new password.

jayair avatar Dec 15 '17 17:12 jayair

Hi Jay,

Thnx for responding!

I am impressed with the amount of information yet clarity you guys have composed these tutorials, it helps a lot!

I followed the chapter, but i guess something went wrong in the part where i had to do:

aws cognito-idp admin-confirm-sign-up
--region us-east-1
--user-pool-id YOUR_USER_POOL_ID
--username [email protected]

I started on a windows computer behind a proxy and had problems, Then switched to a MacBook Pro where i had problems installing pip. So somewhere in that hussle i must have missed i made a mistake.

I can continue with the tutorials because the signup works fine so i can create and authenticated end users.

I receive an error with the list.js component (error 500, status: false), i have yet to find out why, but i learned enough already to keep digging into aws and serverless framework for new applications.

Kind regards, Oscar Henneken

ohenneken avatar Dec 15 '17 21:12 ohenneken

With cognito how would you setup up the ability to reset ones password if the user forgot the password or even within the app?

-Michael

mdhendri avatar Dec 23 '17 15:12 mdhendri

Hi Jay,

Merry X-mas!

I worked through the tutorial but i did not get a "create" call to work decently on the server.

I only got the "create" for the notes-scratch application to work via the serveless invoke local, but never via the apig test. That returns an

500, Internal Server Error

If the serverless invoke local is capable of creating an item in the DynamoDb on the server, apparently all the building blovks are there, so why wouldn't this work on the server itself?

I also created another request call "create_favorite", to create a favorite.

I deployed it apparently correctly, but it states:

app.js:100242 Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.

serverless.yml maps it to a "POST", like so

create_favorite: handler: create_favorite.main events: - http: path: favorites method: post cors: true authorizer: aws_iam

What could it be?

Kind regards, Oscar Henneken

ohenneken avatar Dec 24 '17 10:12 ohenneken

@mdhendri It is a few extra steps but it is fairly straightforward. Cognito will send a code to the user and they can reset their password with the code and their new password. I might write a chapter on this at some point, but here is the sample code from the Cogntio JS SDK docs - https://github.com/aws/amazon-cognito-identity-js

cognitoUser.forgotPassword({
    onSuccess: function (data) {
        // successfully initiated reset password request
        console.log('CodeDeliveryData from forgotPassword: ' + data);
    },
    onFailure: function(err) {
        alert(err);
    },
    //Optional automatic callback
    inputVerificationCode: function(data) {
        console.log('Code sent to: ' + data);
        var verificationCode = prompt('Please input verification code ' ,'');
        var newPassword = prompt('Enter new password ' ,'');
        cognitoUser.confirmPassword(verificationCode, newPassword, {
            onSuccess() {
                console.log('Password confirmed!');
            },
            onFailure(err) {
                console.log('Password not confirmed!');
            }
        });
    }
});

jayair avatar Dec 27 '17 18:12 jayair