private void checkAccountEmailExistsInFirebase(final String email) { if(!validateEmail()) return; mProgressDialog.setTitle("Resetting Password"); mProgressDialog.setMessage("Please wait while we send password reset email."); mProgressDialog.setCanceledOnTouchOutside(false); mProgressDialog.show(); mFirebaseAuth = FirebaseAuth.getInstance(); mFirebaseAuth.fetchProvidersForEmail(email).addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>() { @Override public void onComplete(@NonNull Task<ProviderQueryResult> task) { //Log.d(TAG, "checking email exists."); if(task.getResult().getProviders() != null){ resetPassword(task.getResult().getProviders(), email); } } }); }
public void checkAccountEmailExistsInFirebase(final String email) { FirebaseApp.initializeApp(this); FirebaseAuth mAuth = FirebaseAuth.getInstance(); mAuth.fetchProvidersForEmail(email).addOnCompleteListener(new OnCompleteListener<ProviderQueryResult>() { @Override public void onComplete(@NonNull Task<ProviderQueryResult> task) { if((task.getResult().getProviders() != null && task.getResult().getProviders().isEmpty())){ signUpUser(email); }else{ mProgressDialog.dismiss(); Snackbar.make(mConstraintLayout, "Account with Email Address Already Exists.", Snackbar.LENGTH_LONG ).show(); } } }); }
@Test public void fetchProvidersForEmail() throws InterruptedException { TestSubscriber<ProviderQueryResult> testSubscriber = new TestSubscriber<>(); RxFirebaseAuth.fetchProvidersForEmail(mockAuth, "email") .subscribeOn(Schedulers.immediate()) .subscribe(testSubscriber); testOnSuccessListener.getValue().onSuccess(mockProviderQueryResult); testOnCompleteListener.getValue().onComplete(mockProviderQueryResultTask); verify(mockAuth).fetchProvidersForEmail(eq("email")); testSubscriber.assertNoErrors(); testSubscriber.assertValueCount(1); testSubscriber.assertReceivedOnNext(Collections.singletonList(mockProviderQueryResult)); testSubscriber.assertCompleted(); testSubscriber.unsubscribe(); }
@Test public void fetchProvidersForEmail() throws InterruptedException { TestObserver<ProviderQueryResult> authTestObserver = RxFirebaseAuth .fetchProvidersForEmail(firebaseAuth, ANY_EMAIL) .test(); testOnSuccessListener.getValue().onSuccess(providerQueryResult); testOnCompleteListener.getValue().onComplete(providerQueryResultTask); verify(firebaseAuth).fetchProvidersForEmail(eq(ANY_EMAIL)); authTestObserver.assertNoErrors() .assertValueCount(1) .assertValueSet(Collections.singletonList(providerQueryResult)) .assertComplete() .dispose(); }
/** * TODO: Should use Maybe instead of Single * TODO: flatten List * * @param instance * @param email * @return <emptyList> if providers is null */ @CheckReturnValue @NonNull public static Single<List<String>> fetchProvidersForEmail( @NonNull final FirebaseAuth instance, @NonNull final String email) { return RxTask.single(new Callable<Task<ProviderQueryResult>>() { @Override public Task<ProviderQueryResult> call() throws Exception { return instance.fetchProvidersForEmail(email); } }).map(new Function<ProviderQueryResult, List<String>>() { @Override public List<String> apply(@NonNull ProviderQueryResult providerQueryResult) throws Exception { List<String> providers = providerQueryResult.getProviders(); if (null == providers) { providers = Collections.emptyList(); } return providers; } }); }
public static Task<String> fetchTopProvider(FirebaseAuth auth, @NonNull String email) { if (TextUtils.isEmpty(email)) { return Tasks.forException(new NullPointerException("Email cannot be empty")); } return auth.fetchProvidersForEmail(email) .continueWith(new Continuation<ProviderQueryResult, String>() { @Override public String then(@NonNull Task<ProviderQueryResult> task) { if (!task.isSuccessful()) return null; List<String> providers = task.getResult().getProviders(); return providers == null || providers.isEmpty() ? null : providers.get(providers.size() - 1); } }); }
/** * Gets the list of provider IDs that can be used to sign in for the given email address. Useful for an "identifier-first" sign-in flow. * * @param firebaseAuth firebaseAuth instance. * @param email An email address. * @return a {@link Maybe} which emits an {@link ProviderQueryResult} if success. * @see <a href="https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth">Firebase Auth API</a> */ @NonNull public static Maybe<ProviderQueryResult> fetchProvidersForEmail(@NonNull final FirebaseAuth firebaseAuth, @NonNull final String email) { return Maybe.create(new MaybeOnSubscribe<ProviderQueryResult>() { @Override public void subscribe(MaybeEmitter<ProviderQueryResult> emitter) throws Exception { RxHandler.assignOnTask(emitter, firebaseAuth.fetchProvidersForEmail(email)); } }); }
@Override public void subscribe(final MaybeEmitter<List<String>> emitter) { final OnCompleteListener<ProviderQueryResult> listener = new OnCompleteListener<ProviderQueryResult>() { @Override public void onComplete(@NonNull Task<ProviderQueryResult> task) { if (!task.isSuccessful()) { if (!emitter.isDisposed()) { emitter.onError(task.getException()); } return; } if (!emitter.isDisposed()) { ProviderQueryResult result = task.getResult(); if (result != null) { List<String> providers = result.getProviders(); if (providers != null) { emitter.onSuccess(providers); return; } } emitter.onComplete(); } } }; instance.fetchProvidersForEmail(email).addOnCompleteListener(listener); }
@NonNull public static Observable<ProviderQueryResult> fetchProvidersForEmail(@NonNull final FirebaseAuth firebaseAuth, @NonNull final String email) { return Observable.create(new Observable.OnSubscribe<ProviderQueryResult>() { @Override public void call(final Subscriber<? super ProviderQueryResult> subscriber) { RxHandler.assignOnTask(subscriber, firebaseAuth.fetchProvidersForEmail(email)); } }); }
@Test public void fetchProvidersForEmailError() throws InterruptedException { TestObserver<ProviderQueryResult> authTestObserver = RxFirebaseAuth .fetchProvidersForEmail(firebaseAuth, ANY_EMAIL) .test(); testOnFailureListener.getValue().onFailure(EXCEPTION); verify(firebaseAuth).fetchProvidersForEmail(ANY_EMAIL); authTestObserver.assertError(EXCEPTION) .assertNotComplete() .dispose(); }
private void mockSuccessfulFetchProvidersResult() { when(mockFetchProvidersTask.isSuccessful()) .thenReturn(true); ProviderQueryResult result = mock(ProviderQueryResult.class); when(result.getProviders()) .thenReturn(new ArrayList<String>()); when(mockFetchProvidersTask.getResult()) .thenReturn(result); //noinspection unchecked when(mockFetchProvidersTask.addOnCompleteListener(onComplete.capture())) .thenReturn(mockFetchProvidersTask); }
@CheckResult public Single<ProviderQueryResult> fetchProvidersForEmail(String email) { return RxTasks.single(auth.fetchProvidersForEmail(email)); }
@Override public Task<ProviderQueryResult> fetchProvidersForEmail(String email) { return mAuth.fetchProvidersForEmail(email); }
@Test @Config(shadows = {AuthHelperShadow.class}) public void testSignInFailed_withFacebookAlreadyLinked() { AppCompatBase mockActivity = mock(AppCompatBase.class); IdpResponse idpResponse = new IdpResponse.Builder( new User.Builder(GoogleAuthProvider.PROVIDER_ID, TestConstants.EMAIL) .build()) .setToken(TestConstants.TOKEN) .build(); CredentialSignInHandler credentialSignInHandler = new CredentialSignInHandler( mockActivity, null, RC_ACCOUNT_LINK, idpResponse); FlowParameters mockFlowParams = mock(FlowParameters.class); when(mockActivity.getFlowParams()).thenReturn(mockFlowParams); AuthHelper mockAuthHelper = mock(AuthHelper.class); when(mockActivity.getAuthHelper()).thenReturn(mockAuthHelper); AuthHelperShadow.getFirebaseAuth(); // Force static initialization when(mockAuthHelper.getFirebaseAuth()).thenReturn(AuthHelperShadow.getFirebaseAuth()); ProgressDialogHolder mockHolder = mock(ProgressDialogHolder.class); when(mockActivity.getDialogHolder()).thenReturn(mockHolder); // pretend the account has Facebook linked already when(AuthHelperShadow.getFirebaseAuth().fetchProvidersForEmail(TestConstants.EMAIL)).thenReturn( new AutoCompleteTask<ProviderQueryResult>( new FakeProviderQueryResult( Arrays.asList(FacebookAuthProvider.PROVIDER_ID)), true, null)); // pretend there was already an account with this email Task<AuthResult> exceptionTask = Tasks.forException( new FirebaseAuthUserCollisionException(LINKING_ERROR, LINKING_EXPLANATION)); credentialSignInHandler.onComplete(exceptionTask); ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); ArgumentCaptor<Integer> intCaptor = ArgumentCaptor.forClass(Integer.class); verify(mockActivity).startActivityForResult(intentCaptor.capture(), intCaptor.capture()); Intent capturedIntent = intentCaptor.getValue(); User capturedUser = User.getUser(capturedIntent); assertEquals(RC_ACCOUNT_LINK, (int) intCaptor.getValue()); assertEquals( WelcomeBackIdpPrompt.class.getName(), capturedIntent.getComponent().getClassName()); assertEquals( TestConstants.EMAIL, capturedUser.getEmail()); assertEquals( FacebookAuthProvider.PROVIDER_ID, capturedUser.getProviderId()); }
@Test @Config(shadows = {AuthHelperShadow.class}) public void testSignInFailed_withPasswordAccountAlreadyLinked() { AppCompatBase mockActivity = mock(AppCompatBase.class); IdpResponse idpResponse = new IdpResponse.Builder( new User.Builder(GoogleAuthProvider.PROVIDER_ID, TestConstants.EMAIL) .build()) .setToken(TestConstants.TOKEN) .build(); CredentialSignInHandler credentialSignInHandler = new CredentialSignInHandler( mockActivity, null, RC_ACCOUNT_LINK, idpResponse); FlowParameters mockFlowParams = mock(FlowParameters.class); when(mockActivity.getFlowParams()).thenReturn(mockFlowParams); AuthHelper mockAuthHelper = mock(AuthHelper.class); when(mockActivity.getAuthHelper()).thenReturn(mockAuthHelper); AuthHelperShadow.getFirebaseAuth(); // Force static initialization when(mockAuthHelper.getFirebaseAuth()).thenReturn(AuthHelperShadow.getFirebaseAuth()); ProgressDialogHolder mockHolder = mock(ProgressDialogHolder.class); when(mockActivity.getDialogHolder()).thenReturn(mockHolder); // pretend there was already an account with this email Task mockTask = mock(Task.class); when(mockTask.getException()).thenReturn( new FirebaseAuthUserCollisionException(LINKING_ERROR, LINKING_EXPLANATION)); // pretend the account has a Password account linked already when(AuthHelperShadow.getFirebaseAuth().fetchProvidersForEmail(TestConstants.EMAIL)).thenReturn( new AutoCompleteTask<ProviderQueryResult>( new FakeProviderQueryResult( Arrays.asList(EmailAuthProvider.PROVIDER_ID)), true, null)); credentialSignInHandler.onComplete(mockTask); ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); ArgumentCaptor<Integer> intCaptor = ArgumentCaptor.forClass(Integer.class); verify(mockActivity).startActivityForResult(intentCaptor.capture(), intCaptor.capture()); Intent capturedIntent = intentCaptor.getValue(); assertEquals(RC_ACCOUNT_LINK, (int) intCaptor.getValue()); assertEquals( WelcomeBackPasswordPrompt.class.getName(), capturedIntent.getComponent().getClassName()); assertEquals( TestConstants.EMAIL, IdpResponse.fromResultIntent(capturedIntent).getEmail()); }
Task<ProviderQueryResult> fetchProvidersForEmail(String email);