// [START auth_with_google]
  private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
    // [START_EXCLUDE]
    showProgressDialog();
    // [END_EXCLUDE]
    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth
        .signInWithCredential(credential)
        .addOnCompleteListener(
            this,
            new OnCompleteListener<AuthResult>() {
              @Override
              public void onComplete(@NonNull Task<AuthResult> task) {
                Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());

                // If sign in fails, display a message to the user. If sign in succeeds
                // the auth state listener will be notified and logic to handle the
                // signed in user can be handled in the listener.
                if (!task.isSuccessful()) {
                  Log.w(TAG, "signInWithCredential", task.getException());
                  Toast.makeText(
                          GoogleSignInActivity.this, "Authentication failed.", Toast.LENGTH_SHORT)
                      .show();
                }
                // [START_EXCLUDE]
                hideProgressDialog();
                // [END_EXCLUDE]
              }
            });
  }
Exemplo n.º 2
0
 private void handleSignInResult(GoogleSignInResult result) {
   Log.d(TAG, "handleSignInResult:" + result.isSuccess());
   if (result.isSuccess()) {
     // Signed in successfully, show authenticated UI.
     GoogleSignInAccount acct = result.getSignInAccount();
     User user = new User(acct.getId(), acct.getEmail(), acct.getDisplayName());
     AuthUtils.saveUser(this, user);
   } else {
     // Signed out, show unauthenticated UI.
     Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show();
   }
 }