Amazon-cognito-identity-js: get sub on signup

Created on 23 Mar 2017  Â·  20Comments  Â·  Source: amazon-archives/amazon-cognito-identity-js

First off, thanks for this library – it's been working great so far.

I have one last issue though: When a user registered, I need to add an entry to my user table with a reference to sub (in order to later get the logged-in user).

If I'm not mistaken, I can only get sub with getUserAttributes which is only possible with an authenticated user. Is there a way to get sub when the user has registered (or confirmed the registration)?

Most helpful comment

@itrestian Is there any news of this feature?

All 20 comments

Wondering how to do the same.

seems like it could be put on the response from signUp. Have you found any work arounds?

@sehmaschine can you describe your use case in more detail?

@aaronelasticmedia I'm having a database with a user table (and lots of relations to the user). This table has a primary key (ID) and a field sub. I'm mainly using the internal ID in order to reference the user (makes testing and writing fixtures much easier). Therefore, I need to create an entry within that table once a new user registered.

@collinforrester The current workaround is this: Instead of creating the user entry on signup I add the user with the first login. I check if an entry with sub already exists with the internal user table and I create a new entry if it doesn't.

With my API, I'm using the token to get sub and then I query the internal user table to get the user ID. I hope my use case is understandable.

I'm just surprised that signup does return username instead of sub – although sub is the unique value which actually identifies the user.

We are working on a feature to return the sub on the sign up call. This will be available in the near future.

@itrestian That's great news. IMO, the unique identifier (sub) should be available with every response with regards to a user (except when I ask for certain attributes specifically).

@sehmaschine why don't you save your internal ID as a custom attribute in Cognito ?

@jogold Mainly because the registration could fail and then I end up with a user in my database which needs to be deleted (when I create that user beforehand). Doesn't seem like a clean solution IMHO.

Are you using javascript? Or what is the SDK/language that you are using?

Yes, javascript.

@itrestian Is there any news of this feature?

Stumbled upon this little bug today. Any updates?

+1 to include the sub in the signup response

SignUp API now returns sub in response.

The response from Use Case 1 (in README) still does not return userSub.

It seems like /src/CognitoUserPool.js needs an update to explicitly include userSub: data.userSub after line 98:

    var returnData = {
        user: new _CognitoUser2.default(cognitoUser),
        userConfirmed: data.UserConfirmed,
        userSub: data.userSub
    };

As a reminder, User Case 1 is as follows:

    userPool.signUp('username', 'password', attributeList, null, function(err, result){
        if (err) {
            alert(err);
            return;
        }
        cognitoUser = result.user;
        console.log('user name is ' + cognitoUser.getUsername());
    });

Thanks!

The sub should be available in the newest version.

How can I get this update? I am using [email protected]

I'm still not able to get the sub after a signup.
I'm using "amazon-cognito-identity-js": "1.19.0" and "aws-sdk": "2.84.0".
Can someone guide me in the right direction.
This is what I'm getting in my response:

{
  user:CognitoUser
  userConfirmed:true
  userSub:undefined
}

Thanks before hand

@sehmaschine - once you have 'sub' of the new user how do you pass it to the backend in a reliable way, avoiding fake submissions?

@scheler We decided to dump cognito in favor of a custom solution (for a variety of reasons). Therefore, I'm not able to answer your question. But if I remember it correctly, we did not pass sub to the backend but used the access token instead. On login we return the user object which contains sub.

Was this page helpful?
0 / 5 - 0 ratings