Skip to content

Commit

Permalink
Version 7.2.0
Browse files Browse the repository at this point in the history
Version 7.2.0
  • Loading branch information
samtstern authored Jun 14, 2021
2 parents 3d578ef + 4903947 commit 41c7a73
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 87 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ libraries.
```groovy
dependencies {
// FirebaseUI for Firebase Realtime Database
implementation 'com.firebaseui:firebase-ui-database:7.1.1'
implementation 'com.firebaseui:firebase-ui-database:7.2.0'
// FirebaseUI for Cloud Firestore
implementation 'com.firebaseui:firebase-ui-firestore:7.1.1'
implementation 'com.firebaseui:firebase-ui-firestore:7.2.0'
// FirebaseUI for Firebase Auth
implementation 'com.firebaseui:firebase-ui-auth:7.1.1'
implementation 'com.firebaseui:firebase-ui-auth:7.2.0'
// FirebaseUI for Cloud Storage
implementation 'com.firebaseui:firebase-ui-storage:7.1.1'
implementation 'com.firebaseui:firebase-ui-storage:7.2.0'
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.ErrorCodes;
import com.firebase.ui.auth.FirebaseAuthUIActivityResultContract;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.data.model.FirebaseAuthUIAuthenticationResult;
import com.firebase.uidemo.R;
import com.firebase.uidemo.databinding.ActivityAnonymousUpgradeBinding;
import com.firebase.uidemo.util.ConfigurationUtils;
Expand All @@ -22,20 +24,24 @@

import java.util.List;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AnonymousUpgradeActivity extends AppCompatActivity {
public class AnonymousUpgradeActivity extends AppCompatActivity
implements ActivityResultCallback<FirebaseAuthUIAuthenticationResult> {

private static final String TAG = "AccountLink";

private static final int RC_SIGN_IN = 123;

private ActivityAnonymousUpgradeBinding mBinding;

private AuthCredential mPendingCredential;

private final ActivityResultLauncher<Intent> signIn =
registerForActivityResult(new FirebaseAuthUIActivityResultContract(), this);

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -48,8 +54,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
// Occurs after catching an email link
IdpResponse response = IdpResponse.fromResultIntent(getIntent());
if (response != null) {
handleSignInResult(RC_SIGN_IN, ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT,
getIntent());
handleSignInResult(ErrorCodes.ANONYMOUS_UPGRADE_MERGE_CONFLICT, response);
}

mBinding.anonSignIn.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -100,12 +105,12 @@ public void onComplete(@NonNull Task<AuthResult> task) {

public void startAuthUI() {
List<AuthUI.IdpConfig> providers = ConfigurationUtils.getConfiguredProviders(this);
Intent intent = AuthUI.getInstance().createSignInIntentBuilder()
Intent signInIntent = AuthUI.getInstance().createSignInIntentBuilder()
.setLogo(R.drawable.firebase_auth_120dp)
.setAvailableProviders(providers)
.enableAnonymousUsersAutoUpgrade()
.build();
startActivityForResult(intent, RC_SIGN_IN);
signIn.launch(signInIntent);
}

public void resolveMerge() {
Expand Down Expand Up @@ -145,34 +150,25 @@ public void onComplete(@NonNull Task<Void> task) {
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
handleSignInResult(requestCode, resultCode, data);
}

private void handleSignInResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RC_SIGN_IN) {
IdpResponse response = IdpResponse.fromResultIntent(data);
if (response == null) {
// User pressed back button
return;
}
if (resultCode == RESULT_OK) {
setStatus("Signed in as " + getUserIdentifier(FirebaseAuth.getInstance()
.getCurrentUser()));
} else if (response.getError().getErrorCode() == ErrorCodes
.ANONYMOUS_UPGRADE_MERGE_CONFLICT) {
setStatus("Merge conflict: user already exists.");
mBinding.resolveMerge.setEnabled(true);
mPendingCredential = response.getCredentialForLinking();
} else {
Toast.makeText(this, "Auth error, see logs", Toast.LENGTH_SHORT).show();
Log.w(TAG, "Error: " + response.getError().getMessage(), response.getError());
}

updateUI();
private void handleSignInResult(int resultCode, @Nullable IdpResponse response) {
if (response == null) {
// User pressed back button
return;
}
if (resultCode == RESULT_OK) {
setStatus("Signed in as " + getUserIdentifier(FirebaseAuth.getInstance()
.getCurrentUser()));
} else if (response.getError().getErrorCode() == ErrorCodes
.ANONYMOUS_UPGRADE_MERGE_CONFLICT) {
setStatus("Merge conflict: user already exists.");
mBinding.resolveMerge.setEnabled(true);
mPendingCredential = response.getCredentialForLinking();
} else {
Toast.makeText(this, "Auth error, see logs", Toast.LENGTH_SHORT).show();
Log.w(TAG, "Error: " + response.getError().getMessage(), response.getError());
}

updateUI();
}

private void updateUI() {
Expand Down Expand Up @@ -220,4 +216,9 @@ private String getUserIdentifier(FirebaseUser user) {
return "unknown";
}
}

@Override
public void onActivityResult(@NonNull FirebaseAuthUIAuthenticationResult result) {
handleSignInResult(result.getResultCode(), result.getIdpResponse());
}
}
37 changes: 20 additions & 17 deletions app/src/main/java/com/firebase/uidemo/auth/AuthUiActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.firebase.ui.auth.AuthUI.IdpConfig;
import com.firebase.ui.auth.ErrorCodes;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.FirebaseAuthUIActivityResultContract;
import com.firebase.ui.auth.data.model.FirebaseAuthUIAuthenticationResult;
import com.firebase.ui.auth.util.ExtraConstants;
import com.firebase.uidemo.R;
import com.firebase.uidemo.databinding.AuthUiLayoutBinding;
Expand All @@ -43,6 +45,8 @@
import java.util.ArrayList;
import java.util.List;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -51,7 +55,8 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;

public class AuthUiActivity extends AppCompatActivity {
public class AuthUiActivity extends AppCompatActivity
implements ActivityResultCallback<FirebaseAuthUIAuthenticationResult> {
private static final String TAG = "AuthUiActivity";

private static final String GOOGLE_TOS_URL = "https://www.google.com/policies/terms/";
Expand All @@ -65,6 +70,9 @@ public class AuthUiActivity extends AppCompatActivity {

private AuthUiLayoutBinding mBinding;

private final ActivityResultLauncher<Intent> signIn =
registerForActivityResult(new FirebaseAuthUIActivityResultContract(), this);

@NonNull
public static Intent createIntent(@NonNull Context context) {
return new Intent(context, AuthUiActivity.class);
Expand Down Expand Up @@ -209,11 +217,11 @@ public void flipEmailLinkProviderCheckbox(boolean passwordProviderIsChecked) {
}

public void signIn() {
startActivityForResult(buildSignInIntent(/*link=*/null), RC_SIGN_IN);
signIn.launch(getSignInIntent(/*link=*/null));
}

public void signInWithEmailLink(@Nullable String link) {
startActivityForResult(buildSignInIntent(link), RC_SIGN_IN);
signIn.launch(getSignInIntent(link));
}

@NonNull
Expand All @@ -226,8 +234,7 @@ public AuthUI getAuthUI() {
return authUI;
}

@NonNull
public Intent buildSignInIntent(@Nullable String link) {
private Intent getSignInIntent(@Nullable String link) {
AuthUI.SignInIntentBuilder builder = getAuthUI().createSignInIntentBuilder()
.setTheme(getSelectedTheme())
.setLogo(getSelectedLogo())
Expand Down Expand Up @@ -262,7 +269,6 @@ public Intent buildSignInIntent(@Nullable String link) {
if (auth.getCurrentUser() != null && auth.getCurrentUser().isAnonymous()) {
builder.enableAnonymousUsersAutoUpgrade();
}

return builder.build();
}

Expand All @@ -280,14 +286,6 @@ public void onComplete(@NonNull Task<AuthResult> task) {
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
handleSignInResponse(resultCode, data);
}
}

@Override
protected void onResume() {
super.onResume();
Expand All @@ -298,9 +296,7 @@ protected void onResume() {
}
}

private void handleSignInResponse(int resultCode, @Nullable Intent data) {
IdpResponse response = IdpResponse.fromResultIntent(data);

private void handleSignInResponse(int resultCode, @Nullable IdpResponse response) {
// Successfully signed in
if (resultCode == RESULT_OK) {
startSignedInActivity(response);
Expand Down Expand Up @@ -502,4 +498,11 @@ private List<String> getFacebookPermissions() {
private void showSnackbar(@StringRes int errorMessageRes) {
Snackbar.make(mBinding.getRoot(), errorMessageRes, Snackbar.LENGTH_LONG).show();
}

@Override
public void onActivityResult(@NonNull FirebaseAuthUIAuthenticationResult result) {
// Successfully signed in
IdpResponse response = result.getIdpResponse();
handleSignInResponse(result.getResultCode(), response);
}
}
2 changes: 1 addition & 1 deletion auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Gradle, add the dependency:
```groovy
dependencies {
// ...
implementation 'com.firebaseui:firebase-ui-auth:7.1.1'
implementation 'com.firebaseui:firebase-ui-auth:7.2.0'
// Required only if Facebook login support is required
// Find the latest Facebook SDK releases here: https://github.com/facebook/facebook-android-sdk/blob/master/CHANGELOG.md
Expand Down
4 changes: 4 additions & 0 deletions auth/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ android {
}

dependencies {
implementation(Config.Libs.Androidx.activity)
// The new activity result APIs force us to include Fragment 1.3.0
// See https://issuetracker.google.com/issues/152554847
implementation(Config.Libs.Androidx.fragment)
implementation(Config.Libs.Androidx.design)
implementation(Config.Libs.Androidx.customTabs)
implementation(Config.Libs.Androidx.constraint)
Expand Down
29 changes: 27 additions & 2 deletions auth/src/main/java/com/firebase/ui/auth/AuthUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,9 @@ private String getDefaultIso() {
public static final class GoogleBuilder extends Builder {
public GoogleBuilder() {
super(GoogleAuthProvider.PROVIDER_ID);
}

private void validateWebClientId() {
Preconditions.checkConfigured(getApplicationContext(),
"Check your google-services plugin configuration, the" +
" default_web_client_id string wasn't populated.",
Expand Down Expand Up @@ -1103,8 +1106,29 @@ public GoogleBuilder setSignInOptions(@NonNull GoogleSignInOptions options) {
ExtraConstants.GOOGLE_SIGN_IN_OPTIONS);

GoogleSignInOptions.Builder builder = new GoogleSignInOptions.Builder(options);
builder.requestEmail().requestIdToken(getApplicationContext()
.getString(R.string.default_web_client_id));

String clientId = options.getServerClientId();
if (clientId == null) {
validateWebClientId();
clientId = getApplicationContext().getString(R.string.default_web_client_id);
}

// Warn the user that they are _probably_ doing the wrong thing if they
// have not called requestEmail (see issue #1899 and #1621)
boolean hasEmailScope = false;
for (Scope s : options.getScopes()) {
if ("email".equals(s.getScopeUri())) {
hasEmailScope = true;
break;
}
}
if (!hasEmailScope) {
Log.w(TAG, "The GoogleSignInOptions passed to setSignInOptions does not " +
"request the 'email' scope. In most cases this is a mistake! " +
"Call requestEmail() on the GoogleSignInOptions object.");
}

builder.requestIdToken(clientId);
getParams().putParcelable(
ExtraConstants.GOOGLE_SIGN_IN_OPTIONS, builder.build());

Expand All @@ -1115,6 +1139,7 @@ public GoogleBuilder setSignInOptions(@NonNull GoogleSignInOptions options) {
@Override
public IdpConfig build() {
if (!getParams().containsKey(ExtraConstants.GOOGLE_SIGN_IN_OPTIONS)) {
validateWebClientId();
setScopes(Collections.<String>emptyList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.firebase.ui.auth;

import android.content.Context;
import android.content.Intent;

import com.firebase.ui.auth.data.model.FirebaseAuthUIAuthenticationResult;

import androidx.activity.result.contract.ActivityResultContract;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
* A {@link ActivityResultContract} describing that the caller can launch authentication flow with a
* {@link Intent} and is guaranteed to receive a {@link FirebaseAuthUIAuthenticationResult} as
* result. The given input intent <b>must</b> be created using a
* {@link com.firebase.ui.auth.AuthUI.SignInIntentBuilder} in order to guarantee a successful
* launch of the authentication flow.
*/
public class FirebaseAuthUIActivityResultContract extends
ActivityResultContract<Intent, FirebaseAuthUIAuthenticationResult> {

@NonNull
@Override
public Intent createIntent(@NonNull Context context, Intent input) {
return input;
}

@Override
@NonNull
public FirebaseAuthUIAuthenticationResult parseResult(int resultCode, @Nullable Intent intent) {
return new FirebaseAuthUIAuthenticationResult(resultCode, IdpResponse.fromResultIntent(intent));
}

}
Loading

0 comments on commit 41c7a73

Please sign in to comment.