I am using Mobile Hub (amazon) in my android project and for sign-in providers i am using cognito and I have their sample code which they provide and I modify the code and the Login with Facebook is working but the issue which I am facing is onSuccess
method when user login successfully called twice and I debugg it but I can not Figure it out how to fix that issue.
SplashActivity.java
public class SplashActivity extends AppCompatActivity implements SignInResultHandler {
IdentityManager identityManager;
private LottieAnimationView animationView;
private static final String LOG_TAG = SplashActivity.class.getSimpleName() + " : ";
private final StartupAuthResultHandler authResultHandler = new StartupAuthResultHandler() {
@Override
public void onComplete(final StartupAuthResult authResult) {
identityManager = authResult.getIdentityManager();
if (authResult.isUserSignedIn()) {
// User has successfully signed in with an identity provider.
final IdentityProvider provider = identityManager.getCurrentIdentityProvider();
Logger.d(LOG_TAG + "Signed in with " + provider.getDisplayName());
} else if (authResult.isUserAnonymous()) {
// User has an unauthenticated anonymous (guest) identity, either because the user never previously
// signed in with any identity provider or because refreshing the provider credentials failed.
// Optionally, you can check whether refreshing a previously signed in provider failed.
final StartupAuthErrorDetails errors = authResult.getErrorDetails();
if (errors.didErrorOccurRefreshingProvider()) {
final AuthException providerAuthException = errors.getProviderRefreshException();
Logger.d(LOG_TAG + String.format(
"Credentials for Previously signed-in provider %s could not be refreshed." +
providerAuthException.getProvider().getDisplayName()) +
providerAuthException);
}
Logger.d(LOG_TAG + "Continuing with unauthenticated (guest) identity.");
} else {
// User has no identity because authentication was unsuccessful due to a failure.
final StartupAuthErrorDetails errors = authResult.getErrorDetails();
Logger.e(LOG_TAG + "No Identity could be obtained. Continuing with no identity.",
errors.getUnauthenticatedErrorException());
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
animationView = (LottieAnimationView) findViewById(R.id.animation_view);
identityManager = AWSMobileClient.defaultMobileClient().getIdentityManager();
identityManager.doStartupAuth(this, authResultHandler);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (identityManager.isUserSignedIn()) {
goMain();
} else {
goSignIn();
}
}
}, 2000);
}
@Override
public void onStart() {
super.onStart();
animationView.setProgress(0f);
animationView.playAnimation();
}
@Override
public void onStop() {
super.onStop();
animationView.cancelAnimation();
}
/**
* Go to the main activity.
*/
private void goMain() {
startActivity(new Intent(this, MainActivity.class));
}
private void goSignIn() {
identityManager.signInOrSignUp(this, this);
finish();
}
/* This method is called two times and I also search in code it inoke only one time*/
@Override
public void onSuccess(Activity callingActivity, IdentityProvider provider) {
callingActivity.finish();
if(provider != null) {
IdentityProfile identityProfile = identityManager.getIdentityProfile();
Logger.d(LOG_TAG + " Name : " + identityProfile.getUserName());
Logger.d(LOG_TAG + " Email : " + identityProfile.getUserEmailAddress());
Logger.d(LOG_TAG + " ImageLink : " + identityProfile.getUserImageUrl());
Logger.d(LOG_TAG + " provider : " + provider.getProviderType());
}
goMain();
}
/**
* User cancelled signing in with a provider on the sign-in activity.
* Note: The user is still on the sign-in activity when this call is made.
*
* @param provider the provider the user canceled with.
*/
public void onIntermediateProviderCancel(Activity callingActivity, IdentityProvider provider) {
Logger.d(LOG_TAG + "onIntermediateProviderCancel() called with: callingActivity = " +
"[" + callingActivity + "], provider = [" + provider + "]");
}
/**
* User encountered an error when attempting to sign-in with a provider.
* Note: The user is still on the sign-in activity when this call is made.
*
* @param provider the provider the user attempted to sign-in with that encountered an error.
* @param ex the exception that occurred.
*/
public void onIntermediateProviderError(Activity callingActivity, IdentityProvider provider, Exception ex) {
Logger.d(LOG_TAG + "onIntermediateProviderError() called with: callingActivity = [" + callingActivity + "], " +
"provider = [" + provider + "], errorMesage = [" + ex.getMessage() + "]");
}
@Override
public boolean onCancel(Activity callingActivity) {
return false;
}
}
SignInActivity.java
public class SignInActivity extends AppCompatActivity {
private static final String LOG_TAG = SignInActivity.class.getSimpleName() + " : ";
private SignInManager signInManager;
/**
* SignInProviderResultHandlerImpl handles the final result from sign in.
*/
private class SignInProviderResultHandlerImpl implements SignInProviderResultHandler {
/**
* Receives the successful sign-in result and starts the main activity.
*
* @param provider the identity provider used for sign-in.
*/
@Override
public void onSuccess(final IdentityProvider provider) {
Logger.d(LOG_TAG + String.format("Sign-in with %s succeeded.", provider.getDisplayName()));
// The sign-in manager is no longer needed once signed in.
SignInManager.dispose();
final IdentityManager identityManager = signInManager.getIdentityManager();
final SignInResultHandler signInResultsHandler = signInManager.getResultHandler();
// Load user name and image.
identityManager.loadUserIdentityProfile(provider, new Runnable() {
@Override
public void run() {
// Call back the results handler.
signInResultsHandler.onSuccess(SignInActivity.this, provider);
finish();
}
});
}
/**
* Receives the sign-in result indicating the user canceled and shows a toast.
*
* @param provider the identity provider with which the user attempted sign-in.
*/
@Override
public void onCancel(final IdentityProvider provider) {
Logger.i(LOG_TAG + String.format("Sign-in with %s canceled.", provider.getDisplayName()));
signInManager.getResultHandler().onIntermediateProviderCancel(SignInActivity.this, provider);
}
/**
* Receives the sign-in result that an error occurred signing in and shows a toast.
*
* @param provider the identity provider with which the user attempted sign-in.
* @param ex the exception that occurred.
*/
@Override
public void onError(final IdentityProvider provider, final Exception ex) {
Logger.d(LOG_TAG + String.format("Sign-in with %s caused an error." +
provider.getDisplayName()) + ex);
signInManager.getResultHandler().onIntermediateProviderError(SignInActivity.this, provider, ex);
}
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in);
LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
List<String> readPermissions = new ArrayList<>();
readPermissions.add("public_profile");
readPermissions.add("email");
loginButton.setReadPermissions(readPermissions);
signInManager = SignInManager.getInstance();
signInManager.setProviderResultsHandler(this, new SignInProviderResultHandlerImpl());
signInManager.initializeSignInButton(IdentityProviderType.FACEBOOK, loginButton);
}
@Override
public void onRequestPermissionsResult(final int requestCode, @NonNull final String permissions[],
@NonNull final int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
signInManager.handleRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
signInManager.handleActivityResult(requestCode, resultCode, data);
}
@Override
public void onBackPressed() {
if (signInManager != null) {
if (signInManager.getResultHandler().onCancel(this)) {
super.onBackPressed();
// Since we are leaving sign-in via back, we can dispose the sign-in manager, since sign-in was cancelled.
SignInManager.dispose();
}
}
finish();
}
}