use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.
the class PhoneActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fui_activity_register_phone);
final PhoneProviderResponseHandler handler = new ViewModelProvider(this).get(PhoneProviderResponseHandler.class);
handler.init(getFlowParams());
handler.getOperation().observe(this, new ResourceObserver<IdpResponse>(this, R.string.fui_progress_dialog_signing_in) {
@Override
protected void onSuccess(@NonNull IdpResponse response) {
startSaveCredentials(handler.getCurrentUser(), response, null);
}
@Override
protected void onFailure(@NonNull Exception e) {
handleError(e);
}
});
mPhoneVerifier = new ViewModelProvider(this).get(PhoneNumberVerificationHandler.class);
mPhoneVerifier.init(getFlowParams());
mPhoneVerifier.onRestoreInstanceState(savedInstanceState);
mPhoneVerifier.getOperation().observe(this, new ResourceObserver<PhoneVerification>(this, R.string.fui_verifying) {
@Override
protected void onSuccess(@NonNull PhoneVerification verification) {
if (verification.isAutoVerified()) {
Toast.makeText(PhoneActivity.this, R.string.fui_auto_verified, Toast.LENGTH_LONG).show();
FragmentManager manager = getSupportFragmentManager();
if (manager.findFragmentByTag(SubmitConfirmationCodeFragment.TAG) != null) {
// Ensure the submit code screen isn't visible if there's no code to submit.
// It's possible to get into this state when an SMS is sent, but then
// automatically retrieved.
manager.popBackStack();
}
}
handler.startSignIn(verification.getCredential(), new IdpResponse.Builder(new User.Builder(PhoneAuthProvider.PROVIDER_ID, null).setPhoneNumber(verification.getNumber()).build()).build());
}
@Override
protected void onFailure(@NonNull Exception e) {
if (e instanceof PhoneNumberVerificationRequiredException) {
// already be visible so we have nothing to do.
if (getSupportFragmentManager().findFragmentByTag(SubmitConfirmationCodeFragment.TAG) == null) {
showSubmitCodeFragment(((PhoneNumberVerificationRequiredException) e).getPhoneNumber());
}
// Clear existing errors
handleError(null);
} else {
handleError(e);
}
}
});
if (savedInstanceState != null) {
return;
}
Bundle params = getIntent().getExtras().getBundle(ExtraConstants.PARAMS);
CheckPhoneNumberFragment fragment = CheckPhoneNumberFragment.newInstance(params);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_phone, fragment, CheckPhoneNumberFragment.TAG).disallowAddToBackStack().commit();
}
use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.
the class PhoneActivity method handleError.
private void handleError(@Nullable Exception e) {
TextInputLayout errorView = getErrorView();
if (errorView == null) {
return;
}
if (e instanceof FirebaseAuthAnonymousUpgradeException) {
IdpResponse response = ((FirebaseAuthAnonymousUpgradeException) e).getResponse();
finish(ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT, response.toIntent());
} else if (e instanceof FirebaseAuthException) {
FirebaseAuthError error = FirebaseAuthError.fromException((FirebaseAuthException) e);
if (error == FirebaseAuthError.ERROR_USER_DISABLED) {
IdpResponse response = IdpResponse.from(new FirebaseUiException(ErrorCodes.ERROR_USER_DISABLED));
finish(RESULT_CANCELED, response.toIntent());
return;
}
errorView.setError(getErrorMessage(error));
} else if (e != null) {
errorView.setError(getErrorMessage(FirebaseAuthError.ERROR_UNKNOWN));
} else {
errorView.setError(null);
}
}
use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.
the class SignInKickstarter method handleCredential.
private void handleCredential(final Credential credential) {
String id = credential.getId();
String password = credential.getPassword();
if (TextUtils.isEmpty(password)) {
String identity = credential.getAccountType();
if (identity == null) {
startAuthMethodChoice();
} else {
redirectSignIn(ProviderUtils.accountTypeToProviderId(credential.getAccountType()), id);
}
} else {
final IdpResponse response = new IdpResponse.Builder(new User.Builder(EmailAuthProvider.PROVIDER_ID, id).build()).build();
setResult(Resource.forLoading());
getAuth().signInWithEmailAndPassword(id, password).addOnSuccessListener(result -> handleSuccess(response, result)).addOnFailureListener(e -> {
if (e instanceof FirebaseAuthInvalidUserException || e instanceof FirebaseAuthInvalidCredentialsException) {
// In this case the credential saved in SmartLock was not
// a valid credential, we should delete it from SmartLock
// before continuing.
GoogleApiUtils.getCredentialsClient(getApplication()).delete(credential);
}
startAuthMethodChoice();
});
}
}
use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.
the class AnonymousUpgradeActivity method onCreate.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBinding = ActivityAnonymousUpgradeBinding.inflate(getLayoutInflater());
setContentView(mBinding.getRoot());
updateUI();
// Got here from AuthUIActivity, and we need to deal with a merge conflict
// Occurs after catching an email link
IdpResponse response = IdpResponse.fromResultIntent(getIntent());
if (response != null) {
handleSignInResult(ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT, response);
}
mBinding.anonSignIn.setOnClickListener(view -> signInAnonymously());
mBinding.beginFlow.setOnClickListener(view -> startAuthUI());
mBinding.resolveMerge.setOnClickListener(view -> resolveMerge());
mBinding.signOut.setOnClickListener(view -> signOut());
}
use of com.firebase.ui.auth.IdpResponse in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandlerTest method testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_failedSignInPropagated.
@Test
@SuppressWarnings("all")
public void testStartSignIn_linkingFlowWithAnonymousUpgradeEnabled_failedSignInPropagated() {
mHandler.getOperation().observeForever(mResponseObserver);
setupAnonymousUpgrade();
when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
mPersistenceManager.saveEmail(ApplicationProvider.getApplicationContext(), TestConstants.EMAIL, TestConstants.SESSION_ID, TestConstants.UID);
mPersistenceManager.saveIdpResponseForLinking(ApplicationProvider.getApplicationContext(), buildFacebookIdpResponse());
// Need to control FirebaseAuth's return values
AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
authOperationManager.mScratchAuth = mScratchMockAuth;
when(mScratchMockAuth.signInWithCredential(any(AuthCredential.class))).thenReturn(AutoContinueTask.<AuthResult>forFailure(new Exception("FAILED")));
mHandler.startSignIn();
// Verify sign in was called
verify(mScratchMockAuth).signInWithCredential(any(AuthCredential.class));
// Validate that the data was cleared
assertThat(mPersistenceManager.retrieveSessionRecord(ApplicationProvider.getApplicationContext())).isNull();
// Validate failure
ArgumentCaptor<Resource<IdpResponse>> captor = ArgumentCaptor.forClass(Resource.class);
InOrder inOrder = inOrder(mResponseObserver);
inOrder.verify(mResponseObserver).onChanged(argThat(ResourceMatchers.<IdpResponse>isLoading()));
inOrder.verify(mResponseObserver).onChanged(captor.capture());
assertThat(captor.getValue().getException()).isNotNull();
}
Aggregations