Skip to content

Redirect user to add account if user removed Google account #565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import com.firebase.ui.auth.AuthUI.IdpConfig;
import com.firebase.ui.auth.IdpResponse;
Expand All @@ -35,58 +33,66 @@
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.Scope;
import com.google.android.gms.common.api.Status;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.GoogleAuthProvider;

public class GoogleProvider implements
IdpProvider, OnClickListener, GoogleApiClient.OnConnectionFailedListener {
public class GoogleProvider implements IdpProvider, GoogleApiClient.OnConnectionFailedListener {
private static final String TAG = "GoogleProvider";
private static final int RC_SIGN_IN = 20;
private static final String ERROR_KEY = "error";

private GoogleApiClient mGoogleApiClient;
private Activity mActivity;
private IdpCallback mIDPCallback;
private FragmentActivity mActivity;
private IdpConfig mIdpConfig;
private IdpCallback mIdpCallback;

public GoogleProvider(FragmentActivity activity, IdpConfig idpConfig) {
this(activity, idpConfig, null);
}

public GoogleProvider(FragmentActivity activity, IdpConfig idpConfig, @Nullable String email) {
mActivity = activity;
String clientId = activity.getString(R.string.default_web_client_id);
mIdpConfig = idpConfig;

mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
.enableAutoManage(mActivity, GoogleApiHelper.getSafeAutoManageId(), this)
.addApi(Auth.GOOGLE_SIGN_IN_API, getSignInOptions(email))
.build();
}

public static AuthCredential createAuthCredential(IdpResponse response) {
return GoogleAuthProvider.getCredential(response.getIdpToken(), null);
}

private GoogleSignInOptions getSignInOptions(@Nullable String email) {
String clientId = mActivity.getString(R.string.default_web_client_id);

GoogleSignInOptions.Builder builder =
new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken(clientId);

if (activity.getResources().getIdentifier(
"google_permissions", "array", activity.getPackageName()) != 0) {
if (mActivity.getResources().getIdentifier(
"google_permissions", "array", mActivity.getPackageName()) != 0) {
Log.w(TAG, "DEVELOPER WARNING: You have defined R.array.google_permissions but that is"
+ " no longer respected as of FirebaseUI 1.0.0. Please see README for IDP scope"
+ " configuration instructions.");
}

// Add additional scopes
for (String scopeString : idpConfig.getScopes()) {
for (String scopeString : mIdpConfig.getScopes()) {
builder.requestScopes(new Scope(scopeString));
}

if (!TextUtils.isEmpty(email)) {
builder.setAccountName(email);
}

mGoogleApiClient = new GoogleApiClient.Builder(activity)
.enableAutoManage(activity, GoogleApiHelper.getSafeAutoManageId(), this)
.addApi(Auth.GOOGLE_SIGN_IN_API, builder.build())
.build();
}

public static AuthCredential createAuthCredential(IdpResponse response) {
return GoogleAuthProvider.getCredential(response.getIdpToken(), null);
return builder.build();
}

public String getName(Context context) {
Expand All @@ -100,7 +106,7 @@ public String getProviderId() {

@Override
public void setAuthenticationCallback(IdpCallback callback) {
mIDPCallback = callback;
mIdpCallback = callback;
}

public void disconnect() {
Expand All @@ -121,7 +127,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result != null) {
if (result.isSuccess()) {
mIDPCallback.onSuccess(createIdpResponse(result.getSignInAccount()));
mIdpCallback.onSuccess(createIdpResponse(result.getSignInAccount()));
} else {
onError(result);
}
Expand All @@ -138,21 +144,26 @@ public void startLogin(Activity activity) {
}

private void onError(GoogleSignInResult result) {
String errorMessage = result.getStatus().getStatusMessage();
onError(result.getStatus().getStatusCode() + " " + errorMessage);
Status status = result.getStatus();

if (status.getStatusCode() == CommonStatusCodes.INVALID_ACCOUNT) {
mGoogleApiClient.stopAutoManage(mActivity);
mGoogleApiClient.disconnect();
mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
.enableAutoManage(mActivity, GoogleApiHelper.getSafeAutoManageId(), this)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the second time I've removed an unused click listener!

.addApi(Auth.GOOGLE_SIGN_IN_API, getSignInOptions(null))
.build();
startLogin(mActivity);
} else {
onError(status.getStatusCode() + " " + status.getStatusMessage());
}
}

private void onError(String errorMessage) {
Log.e(TAG, "Error logging in with Google. " + errorMessage);
Bundle extra = new Bundle();
extra.putString(ERROR_KEY, errorMessage);
mIDPCallback.onFailure(extra);
}

@Override
public void onClick(View view) {
Auth.GoogleSignInApi.signOut(mGoogleApiClient);
startLogin(mActivity);
mIdpCallback.onFailure(extra);
}

@Override
Expand Down