Aws-sdk-android: Is this the correct usage? MobileClient storing CognitoId after successful Sign up

Created on 27 Aug 2019  路  3Comments  路  Source: aws-amplify/aws-sdk-android

I'm using the AWSMobileClient to signup a new user. After they successfully sign up I would like to store their unique uuid aka "sub" id in my SQL database.

AWSMobileClient.getInstance().signUp(username, password, attributes, null, new 
Callback<SignUpResult>() {
@Override
public void onResult(final SignUpResult signUpResult) {
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //Successful signing up a new user
        //What would I do here?
    }
});
}

@Override
public void onError(Exception e) {
    Log.e(TAG, "Sign-up error", e);
}

The user successfully signs up, would I make a post request to my database in the callback method? What happens if the user's network fails during midcall or something else happens and the post request doesn't successfully record the user's uuid. What would be the correct way of doing this?

AWSMobileClient Usage Question

Most helpful comment

@bobguy1234 Thanks for reaching out. I have submitted a PR https://github.com/aws-amplify/aws-sdk-android/pull/1107 that exposes userSub in the SignUpResult returned in the callback upon successful sign up. I do not have a firm timeline but this should be released in next couple of weeks. Once that is available, you can make post call to database to record usersub in the database. You will have to handle error conditions such as network failure there itself as follows :

AWSMobileClient.getInstance().signUp(username, password, attributes, null, new 
Callback<SignUpResult>() {
@Override
public void onResult(final SignUpResult signUpResult) {
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //Successful signing up a new user
        try {
               // Make a post request to store signUpResult.getUserSub() to the database
        } catch {
               // Deal with the failure condition here
        }
    }
});
}

@Override
public void onError(Exception e) {
    Log.e(TAG, "Sign-up error", e);
}

Alternatively, you can use a countdown latch to wait for successful completion and make post request on the calling thread as follows :

CountDownLatch signUpLatch = new CountDownLatch(1);
AWSMobileClient.getInstance().signUp(username, password, attributes, null, new 
Callback<SignUpResult>() {
@Override
public void onResult(final SignUpResult signUpResult) {
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //Successful signing up a new user
        signUpLatch.countDown();
    }
});
}

@Override
public void onError(Exception e) {
    Log.e(TAG, "Sign-up error", e);
}

// Wait for the latch
try{
signUpLatch.await();
} catch {
// error handling
}
/** Snippet for post request to database and corresponding error handling logic */

Hope that helps!

All 3 comments

@bobguy1234 Thanks for reaching out. I have submitted a PR https://github.com/aws-amplify/aws-sdk-android/pull/1107 that exposes userSub in the SignUpResult returned in the callback upon successful sign up. I do not have a firm timeline but this should be released in next couple of weeks. Once that is available, you can make post call to database to record usersub in the database. You will have to handle error conditions such as network failure there itself as follows :

AWSMobileClient.getInstance().signUp(username, password, attributes, null, new 
Callback<SignUpResult>() {
@Override
public void onResult(final SignUpResult signUpResult) {
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //Successful signing up a new user
        try {
               // Make a post request to store signUpResult.getUserSub() to the database
        } catch {
               // Deal with the failure condition here
        }
    }
});
}

@Override
public void onError(Exception e) {
    Log.e(TAG, "Sign-up error", e);
}

Alternatively, you can use a countdown latch to wait for successful completion and make post request on the calling thread as follows :

CountDownLatch signUpLatch = new CountDownLatch(1);
AWSMobileClient.getInstance().signUp(username, password, attributes, null, new 
Callback<SignUpResult>() {
@Override
public void onResult(final SignUpResult signUpResult) {
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //Successful signing up a new user
        signUpLatch.countDown();
    }
});
}

@Override
public void onError(Exception e) {
    Log.e(TAG, "Sign-up error", e);
}

// Wait for the latch
try{
signUpLatch.await();
} catch {
// error handling
}
/** Snippet for post request to database and corresponding error handling logic */

Hope that helps!

@bobguy1234 PR https://github.com/aws-amplify/aws-sdk-android/pull/1107 has been merged and will be out in the next release. Please feel free to give it a try once it is out and let me know if you have any feedback.

Closing this issue as the code changes to expose userSub through SignUpResult is released.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

logo17 picture logo17  路  4Comments

devxpy picture devxpy  路  4Comments

zgao67 picture zgao67  路  4Comments

nsadiq-radpits picture nsadiq-radpits  路  3Comments

PikkaPikkachu picture PikkaPikkachu  路  4Comments