use of com.firebase.ui.auth.FirebaseUiException in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandler method startSignIn.
public void startSignIn() {
setResult(Resource.forLoading());
String link = getArguments().emailLink;
if (!getAuth().isSignInWithEmailLink(link)) {
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.INVALID_EMAIL_LINK_ERROR)));
return;
}
final EmailLinkPersistenceManager persistenceManager = EmailLinkPersistenceManager.getInstance();
SessionRecord sessionRecord = persistenceManager.retrieveSessionRecord(getApplication());
EmailLinkParser parser = new EmailLinkParser(link);
String sessionIdFromLink = parser.getSessionId();
String anonymousUserIdFromLink = parser.getAnonymousUserId();
String oobCodeFromLink = parser.getOobCode();
String providerIdFromLink = parser.getProviderId();
boolean forceSameDevice = parser.getForceSameDeviceBit();
if (isDifferentDeviceFlow(sessionRecord, sessionIdFromLink)) {
if (TextUtils.isEmpty(sessionIdFromLink)) {
// There should always be a valid session ID in the link
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.INVALID_EMAIL_LINK_ERROR)));
return;
}
if (forceSameDevice || !TextUtils.isEmpty(anonymousUserIdFromLink)) {
// In both cases, the link was meant to be completed on the same device.
// For anonymous user upgrade, we don't support the cross device flow.
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.EMAIL_LINK_WRONG_DEVICE_ERROR)));
return;
}
// If we have no SessionRecord/there is a session ID mismatch, this means that we were
// not the ones to send the link. The only way forward is to prompt the user for their
// email before continuing the flow. We should only do that after validating the link.
determineDifferentDeviceErrorFlowAndFinish(oobCodeFromLink, providerIdFromLink);
return;
}
if (anonymousUserIdFromLink != null) {
// Same device flow, need to ensure uids match
if (getAuth().getCurrentUser() == null || (getAuth().getCurrentUser().isAnonymous() && !anonymousUserIdFromLink.equals(getAuth().getCurrentUser().getUid()))) {
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.EMAIL_LINK_DIFFERENT_ANONYMOUS_USER_ERROR)));
return;
}
}
finishSignIn(sessionRecord);
}
use of com.firebase.ui.auth.FirebaseUiException in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandler method finishSignIn.
private void finishSignIn(@NonNull String email, @Nullable IdpResponse response) {
if (TextUtils.isEmpty(email)) {
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.EMAIL_MISMATCH_ERROR)));
return;
}
final AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
final EmailLinkPersistenceManager persistenceManager = EmailLinkPersistenceManager.getInstance();
String link = getArguments().emailLink;
if (response == null) {
handleNormalFlow(authOperationManager, persistenceManager, email, link);
} else {
handleLinkingFlow(authOperationManager, persistenceManager, response, link);
}
}
use of com.firebase.ui.auth.FirebaseUiException in project FirebaseUI-Android by firebase.
the class LinkingSocialProviderResponseHandler method startSignIn.
public void startSignIn(@NonNull final IdpResponse response) {
if (!response.isSuccessful()) {
setResult(Resource.forFailure(response.getError()));
return;
}
if (isInvalidProvider(response.getProviderType())) {
throw new IllegalStateException("This handler cannot be used to link email or phone providers.");
}
if (mEmail != null && !mEmail.equals(response.getEmail())) {
setResult(Resource.forFailure(new FirebaseUiException(ErrorCodes.EMAIL_MISMATCH_ERROR)));
return;
}
setResult(Resource.forLoading());
// return a credential.
if (isGenericIdpLinkingFlow(response.getProviderType())) {
getAuth().getCurrentUser().linkWithCredential(mRequestedSignInCredential).addOnSuccessListener(authResult -> handleSuccess(response, authResult)).addOnFailureListener(e -> Resource.<IdpResponse>forFailure(e));
return;
}
final AuthOperationManager authOperationManager = AuthOperationManager.getInstance();
final AuthCredential credential = ProviderUtils.getAuthCredential(response);
if (authOperationManager.canUpgradeAnonymous(getAuth(), getArguments())) {
if (mRequestedSignInCredential == null) {
// The user has provided a valid credential by signing in with a federated
// idp. linkWithCredential will fail because the user is anonymous and the account
// exists (we're in the welcome back flow).
// We know that they are signing in with the same IDP because requestSignInCredential
// is null.
// We just need to have the developer handle the merge failure.
handleMergeFailure(credential);
} else {
// The user has logged in with an IDP that has the same email with another IDP
// present on the account.
// These IDPs belong to the same account - they must be linked, but we can't lose
// our anonymous user session
authOperationManager.safeLink(credential, mRequestedSignInCredential, getArguments()).addOnSuccessListener(result -> handleMergeFailure(credential)).addOnFailureListener(e -> setResult(Resource.forFailure(e)));
}
} else {
getAuth().signInWithCredential(credential).continueWithTask(task -> {
final AuthResult result = task.getResult();
if (mRequestedSignInCredential == null) {
return Tasks.forResult(result);
} else {
return result.getUser().linkWithCredential(mRequestedSignInCredential).continueWith(task1 -> {
if (task1.isSuccessful()) {
return task1.getResult();
} else {
// to backtrack so we just ignore any errors.
return result;
}
});
}
}).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
handleSuccess(response, task.getResult());
} else {
setResult(Resource.forFailure(task.getException()));
}
});
}
}
use of com.firebase.ui.auth.FirebaseUiException in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandlerTest method testStartSignIn_differentDeviceLinkWithForceSameDeviceTrue_expectWrongDeviceError.
@Test
@SuppressWarnings("all")
public void testStartSignIn_differentDeviceLinkWithForceSameDeviceTrue_expectWrongDeviceError() {
String differentSessionId = SessionUtils.generateRandomAlphaNumericString(10);
initializeHandlerWithSessionInfo(differentSessionId, null, null, true);
mHandler.getOperation().observeForever(mResponseObserver);
when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
mHandler.startSignIn();
verify(mMockAuth).isSignInWithEmailLink(any(String.class));
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());
FirebaseUiException exception = (FirebaseUiException) captor.getValue().getException();
assertThat(exception).isNotNull();
assertThat(exception.getErrorCode()).isEqualTo(ErrorCodes.EMAIL_LINK_WRONG_DEVICE_ERROR);
}
use of com.firebase.ui.auth.FirebaseUiException in project FirebaseUI-Android by firebase.
the class EmailLinkSignInHandlerTest method testStartSignIn_differentDeviceLinkWithValidSessionInfo_expectPromptForEmailError.
@Test
@SuppressWarnings("all")
public void testStartSignIn_differentDeviceLinkWithValidSessionInfo_expectPromptForEmailError() {
String differentSessionId = SessionUtils.generateRandomAlphaNumericString(10);
initializeHandlerWithSessionInfo(differentSessionId, null, null, false);
mHandler.getOperation().observeForever(mResponseObserver);
when(mMockAuth.isSignInWithEmailLink(any(String.class))).thenReturn(true);
when(mMockAuth.checkActionCode(any(String.class))).thenReturn(AutoCompleteTask.forSuccess(mMockActionCodeResult));
mHandler.startSignIn();
verify(mMockAuth).isSignInWithEmailLink(any(String.class));
verify(mMockAuth).checkActionCode(any(String.class));
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());
FirebaseUiException exception = (FirebaseUiException) captor.getValue().getException();
assertThat(exception).isNotNull();
assertThat(exception.getErrorCode()).isEqualTo(ErrorCodes.EMAIL_LINK_PROMPT_FOR_EMAIL_ERROR);
}
Aggregations