Skip to content

Commit faaa892

Browse files
authored
feat(AWSMobileClient): add deleteUser API (#2785)
1 parent 5fd017c commit faaa892

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

aws-android-sdk-mobile-client/src/androidTest/java/com/amazonaws/mobile/client/AWSMobileClientTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,27 @@ public void testSignedOutWithoutRevokeToken() throws Exception {
686686
public void testSignInWrongPassword() throws Exception {
687687
AWSMobileClient.getInstance().signIn(getPackageConfigure().getString("username"), "wrong", null);
688688
}
689+
690+
@Test
691+
public void testDeleteUser() throws Exception {
692+
auth.signIn(username, PASSWORD, null);
693+
assertTrue("isSignedIn is true", auth.isSignedIn());
694+
695+
auth.deleteUser();
696+
assertTrue("isSignedIn is false", auth.isSignedIn());
697+
698+
try {
699+
auth.signIn(username, PASSWORD, null);
700+
fail("Sign in should fail since the user should no longer exist in the user pool.");
701+
} catch (Exception e) {
702+
assertTrue(e instanceof com.amazonaws.services.cognitoidentityprovider.model.UserNotFoundException);
703+
}
704+
}
705+
706+
@Test(expected = com.amazonaws.mobileconnectors.cognitoidentityprovider.exceptions.CognitoNotAuthorizedException.class)
707+
public void testDeleteUserWithUnauthenticatedUser() throws Exception {
708+
auth.deleteUser();
709+
}
689710

690711
@Test
691712
public void testFederatedSignInFail() {

aws-android-sdk-mobile-client/src/main/java/com/amazonaws/mobile/client/AWSMobileClient.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
import com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProvider;
111111
import com.amazonaws.services.cognitoidentityprovider.AmazonCognitoIdentityProviderClient;
112112
import com.amazonaws.services.cognitoidentityprovider.model.GlobalSignOutRequest;
113+
import com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException;
113114
import com.amazonaws.util.StringUtils;
114115

115116
import org.json.JSONArray;
@@ -1682,6 +1683,60 @@ public void onError(Exception e) {
16821683
};
16831684
}
16841685

1686+
/**
1687+
* Delete the account of the currently logged-in user.
1688+
* @throws Exception if the user cannot be deleted successfully
1689+
*/
1690+
@WorkerThread
1691+
public void deleteUser() throws Exception {
1692+
final InternalCallback<Void> internalCallback = new InternalCallback<>();
1693+
internalCallback.await(_deleteUser(internalCallback));
1694+
}
1695+
1696+
/**
1697+
* Delete the account of the currently logged-in user.
1698+
* @param callback the callback will be invoked to notify the success or
1699+
* failure of the deleteUser operation
1700+
*/
1701+
@AnyThread
1702+
public void deleteUser(final Callback<Void> callback) {
1703+
final InternalCallback<Void> internalCallback = new InternalCallback<>(callback);
1704+
internalCallback.async(_deleteUser(internalCallback));
1705+
}
1706+
1707+
private Runnable _deleteUser(final Callback<Void> callback) {
1708+
return () -> {
1709+
if (userpool == null) {
1710+
callback.onError(new InvalidUserPoolConfigurationException(
1711+
"A user pool must be configured in order to delete a user."
1712+
));
1713+
} else {
1714+
CognitoUser currentUser = userpool.getCurrentUser();
1715+
currentUser.deleteUserInBackground(new GenericHandler() {
1716+
@Override
1717+
public void onSuccess() {
1718+
signOut(SignOutOptions.builder().signOutGlobally(true).invalidateTokens(true).build(), new Callback<Void>() {
1719+
@Override
1720+
public void onResult(Void result) {
1721+
callback.onResult(result);
1722+
}
1723+
1724+
@Override
1725+
public void onError(Exception e) {
1726+
callback.onError(e);
1727+
}
1728+
});
1729+
}
1730+
1731+
@Override
1732+
public void onFailure(Exception exception) {
1733+
callback.onError(exception);
1734+
}
1735+
});
1736+
}
1737+
};
1738+
}
1739+
16851740
/**
16861741
* Federate tokens from custom identity providers into Cognito Identity Pool by providing the
16871742
* logins key and token

0 commit comments

Comments
 (0)