Comments: Signup with AWS Cognito
Link to chapter - http://serverless-stack.com/chapters/signup-with-aws-cognito.html
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);
})
);
}
@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.
Oops! Didn't know that! I appreciate the response. Thanks!
When a user signs up I get this screen

But I get an email link instead:

which takes me to this page

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.
@QuantumInformation Hmm can you check this setting?
It should have defaulted to Code instead of Link.
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?
@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?
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);
})
);
}
@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.
@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.
@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.
@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?
@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.
@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.
@d0ruk Yeah I'm not sure about amplify. Are you not able to change the User Pool settings from the AWS Console?
@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.
Is there a link/reference to how to register the user with an Identity pool after sign up?
@PraneshBalekai What do you mean by registering a user with an Identity Pool?
@jayair I want to use Federated Identities in Cognito. I already created an identity pool and added my cognito user pool as an authentication provider. However, it does not automatically register as a new identity when I call confirmRegistration or authenticateUser. I tried to follow the method as given in use case 4 of https://github.com/aws/amazon-cognito-identity-js and tried to refresh but it doesn't work (error: missing region in config, although it was all configured). Federated Identities was not covered in this tutorial but I was wondering if there is a guide or reference to managing federated identities in a reactJS app