Signup with AWS Cognito


#1

From @jayair on Mon Apr 10 2017 01:05:17 GMT+0000 (UTC)

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

Copied from original issue: https://github.com/AnomalyInnovations/serverless-stack-com/issues/46


#2

From @DushyantShukla on Tue Sep 05 2017 17:15:28 GMT+0000 (UTC)

Is this code functional? As I was unable to register a new user. I found that the email attribute should be passed against the key email in the userAttributes parameter of SignUp API. Finally, below is my updated signup() method which works just fine:

signup(email, password) {
        const userPool = new CognitoUserPool({
            UserPoolId: config.cognito.USER_POOL_ID,
            ClientId: config.cognito.APP_CLIENT_ID
        });
        const attributeList = [];
        attributeList.push(new CognitoUserAttribute({
            Name: 'email',
            Value: email
        }));
        return new Promise((resolve, reject) =>
            userPool.signUp(email, password, attributeList, null, (err, result) => {
                if (err) {
                    reject(err);
                    return;
                }
                resolve(result.user);
            })
        );
    }

#3

From @jayair on Tue Sep 05 2017 19:30:44 GMT+0000 (UTC)

@DushyantShukla Yeah it is functional. The tutorial has been updated so that the email can be passed in as the username. You can read more about the update here - https://github.com/AnomalyInnovations/serverless-stack-com/issues/123.

If you need to reference the old version of the tutorial, there is a hosted version here - https://59a70f2ccf321c2ccb15113f–serverless-stack.netlify.com.


#4

From @DushyantShukla on Wed Sep 06 2017 06:38:16 GMT+0000 (UTC)

Oops! Didn’t know that! I appreciate the response. Thanks!


#5

From @QuantumInformation on Sun Oct 08 2017 08:33:03 GMT+0000 (UTC)

When a user signs up I get this screen

But I get an email link instead:

which takes me to this page


#6

From @QuantumInformation on Mon Oct 09 2017 10:44:13 GMT+0000 (UTC)

I rechecked my pool with the advice in https://serverless-stack.com/chapters/create-a-cognito-user-pool.html but it seems the same to me.


#7

From @jayair on Mon Oct 09 2017 23:10:48 GMT+0000 (UTC)

@QuantumInformation Hmm can you check this setting?

It should have defaulted to Code instead of Link.


#8

From @elnbado on Thu Oct 12 2017 12:19:05 GMT+0000 (UTC)

In handleSubmit = async event =>

I get the following Warning when calling:

  this.setState({
        newUser: newUser
  });


Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.

Please check the code for the Signup component.

Anyone else has this problem? Any ideas to resolve this?


#9

From @jayair on Thu Oct 12 2017 18:40:55 GMT+0000 (UTC)

@elnbado This happens usually when you make the setState call after you move away from the page. Is your app redirecting after you hit the Signup button?


#10

From @millergd on Fri Oct 20 2017 03:33:13 GMT+0000 (UTC)

For some reason, my signup() method did not work until I added an attribute list to the userPool.signUp() call. Is this out of date?

this is what my working code looks like:

signup(email, password) {
        const userPool = new CognitoUserPool({
            UserPoolId: config.cognito.USER_POOL_ID,
            ClientId: config.cognito.APP_CLIENT_ID
        });
        const attributeList = [];
        attributeList.push(new CognitoUserAttribute({
            Name: 'email',
            Value: email
        }));
        return new Promise((resolve, reject) =>
            userPool.signUp(email, password, attributeList, null, (err, result) => {
                if (err) {
                    reject(err);
                    return;
                }
                resolve(result.user);
            })
        );
    }

#11

From @jayair on Fri Oct 20 2017 16:51:37 GMT+0000 (UTC)

@millergd The tutorial had been updated a while back to set the email as the username in the User Pool. This makes it so that you don’t have to set the attribute. You can read more about it here - https://github.com/AnomalyInnovations/serverless-stack-com/issues/123.


#12

From @millergd on Fri Oct 20 2017 17:03:44 GMT+0000 (UTC)

@jayair yeah it looks like I was using an older version of the tutorial and setting up the userpool different.

However, I don’t really understand the changes. Can you clarify what was the norm before and after the update?

Previously, we were manually setting using the email as a user’s username. There is one major issue
with this; the username cannot be changed in your Cognito User Pool. AWS fixed this by adding the
option of using the email as the username while logging in and signing up. 

#13

From @jayair on Fri Oct 20 2017 18:54:52 GMT+0000 (UTC)

@millergd Yeah the issue with setting the email as the username manually (as we were doing before) was that the username in Cognito cannot be changed. So you would need to work around this in case your users need to change their email.

But with the new email as username setting that’s been introduced recently, the User Pool sets a unique string as the username internally. This allows you to change a user’s email (and username) easily.

Hopefully that makes sense. It is a bit confusing though.


#14

From @d0ruk on Thu Jan 04 2018 13:49:55 GMT+0000 (UTC)

@jayair

setting the email as the username manually

Can you elaborate on this? Did you use to derive a dummy username from the email?

I am using aws-amplify for Auth. Signature of the signUp function is

signUp(username: string, password: string, email: string, phone_number: string): Promise

I have to supply a username. So I derive a username from the email, and do signUp(dummyName, password, email). However, this way, the user gets a username she doesn’t even see. It’s some specific way of string manipulation my client does.

Thus, when the user logs in via her email, that same util function derives the same username, and makes the request to the user pool with that. So the e-mail is really not needed, the way I went about doing it.

Did you have to do it like the above? Make up a username?


p.s. From the looks of this PR, the new signature will be;

signUp(username: string, password: string, email: string|Array<Object>, phone_number: string): Promise<any>

I think that will allow you to attach custom attributes to your user. However, that still seems to necessitate a username.

p.p.s When I try to signup with email in place of the username, AWS returns

Username cannot be of email format, since user pool is configured for email alias.

Which is cryptic to me. When I try to verify the user, I again need to supply the derived username.

However, when logging in, I can use the e-mail address as is, apparently. So, the username is used only for the signup phase? I don’t need it afterwards?


#15

From @jayair on Fri Jan 05 2018 20:58:41 GMT+0000 (UTC)

@d0ruk We were setting the username manually. As in passing in the email as the username. The error you are getting is because you have the email as alias option enabled. This means users can login with either their username or email. But it does not allow usernames that are of the email format since you could have an unverified email as the username. In the tutorial we don’t use this option.


#16

From @d0ruk on Fri Jan 05 2018 21:20:32 GMT+0000 (UTC)

@jayair

Thus, if I don’t use any aliases, would the user pool accept signup/login flows with an email? As I linked above, the signUp() method requires a username.

I’m asking this because there might not be such an option in the aws-amplify API, yet. Cognito might be supportting it, however. I’m trying to discern what is possible with aws-amplify.


#17

From @jayair on Mon Jan 08 2018 02:33:31 GMT+0000 (UTC)

@d0ruk Yeah I’m not sure about amplify. Are you not able to change the User Pool settings from the AWS Console?


#18

From @d0ruk on Tue Jan 09 2018 15:32:14 GMT+0000 (UTC)

@jayair

I’m trying to figure out the .yaml representation of the userpool in the tutorial. That’s outside the scope of the book.

I’ll leave it as is (derive username for login/verify) since it works. I’ll note it somewhere here in case this turns out to be a no-no with aws-amplify.

Cheers.


#19

From @PraneshBalekai on Mon Feb 05 2018 12:57:13 GMT+0000 (UTC)

Is there a link/reference to how to register the user with an Identity pool after sign up?


#20

From @jayair on Mon Feb 05 2018 18:30:29 GMT+0000 (UTC)

@PraneshBalekai What do you mean by registering a user with an Identity Pool?