Skip to content

Instantly share code, notes, and snippets.

@haerulmuttaqin
Last active December 10, 2020 04:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haerulmuttaqin/f0fd16bc0e7d9992dff9911f4ea622c3 to your computer and use it in GitHub Desktop.
Save haerulmuttaqin/f0fd16bc0e7d9992dff9911f4ea622c3 to your computer and use it in GitHub Desktop.
Firebase Login with Google (youtube tutorial)
<vector
android:height="124dp"
android:viewportHeight="180"
android:viewportWidth="180"
android:width="124dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillAlpha="1" android:fillColor="#ff9900"
android:fillType="nonZero"
android:pathData="m35.239,132.063c0.261,-1.888 0.806,-5.14 1.212,-7.227 0.406,-2.087 1.442,-8.437 2.303,-14.11 1.789,-11.793 3.832,-24.985 6.088,-39.298 3.112,-19.749 4.336,-27.603 5.524,-35.466 1.205,-7.972 3.236,-20.083 4.221,-25.169 0.413,-2.132 0.912,-2.716 2.476,-2.9 2.313,-0.272 2.461,-0.072 9.153,12.356 2.671,4.96 5.728,10.61 6.793,12.556 1.066,1.946 3.38,6.252 5.143,9.568l3.206,6.029 -4.867,9.225c-28.82,54.634 -40.436,76.466 -41.025,77.107 -0.431,0.468 -0.519,-0.564 -0.228,-2.67z"
android:strokeAlpha="1" android:strokeColor="#00000000"
android:strokeLineCap="round" android:strokeLineJoin="round" android:strokeWidth="20.3085556"/>
<path android:fillAlpha="1" android:fillColor="#ff6600"
android:fillType="nonZero"
android:pathData="m35.239,134.063c0.001,-0.198 4.205,-8.269 9.343,-17.937 5.138,-9.668 15.348,-28.968 22.689,-42.89 11.182,-21.206 13.614,-25.314 14.988,-25.324 1.681,-0.013 2.625,1.258 7.356,9.903 1.263,2.307 2.861,5.053 3.551,6.102 0.691,1.049 1.665,2.861 2.166,4.027 0.501,1.166 1.584,2.369 2.408,2.674 1.382,0.511 1.27,0.808 -1.455,3.849 -1.624,1.812 -3.153,3.269 -3.398,3.237 -0.245,-0.032 -0.523,0.2 -0.616,0.514 -0.374,1.253 -2.611,3.421 -4.621,4.478 -1.167,0.614 -1.935,1.42 -1.705,1.792 0.23,0.371 -0.279,0.896 -1.13,1.166 -0.851,0.27 -1.383,0.758 -1.181,1.084 0.338,0.547 -2.663,3.617 -3.97,4.061 -0.313,0.106 -0.517,0.365 -0.452,0.575 0.136,0.442 -2.174,2.669 -2.768,2.669 -0.22,0 -0.347,0.172 -0.282,0.381 0.139,0.449 -1.391,1.927 -2.215,2.14 -0.315,0.081 -0.565,0.327 -0.555,0.545 0.032,0.773 -3.64,4.326 -5.438,5.262 -1.003,0.522 -1.771,1.264 -1.706,1.65 0.119,0.706 -1.656,2.653 -2.391,2.623 -0.218,-0.009 -0.375,0.199 -0.348,0.462 0.026,0.263 -0.746,0.874 -1.716,1.357 -0.97,0.484 -1.769,1.213 -1.776,1.62 -0.019,1.215 -1.194,2.699 -1.827,2.308 -0.652,-0.403 -5.272,4.088 -5.448,5.297 -0.061,0.419 -0.368,0.694 -0.681,0.611 -0.652,-0.174 -6.063,5.195 -6.111,6.063 -0.017,0.315 -0.289,0.581 -0.603,0.592 -0.315,0.011 -2.717,2.146 -5.339,4.745 -2.622,2.599 -4.766,4.563 -4.765,4.365z"
android:strokeAlpha="1" android:strokeColor="#00000000"
android:strokeLineCap="round" android:strokeLineJoin="round" android:strokeWidth="20.3085556"/>
<path android:fillAlpha="1" android:fillColor="#ff9900"
android:fillType="nonZero"
android:pathData="m96.241,70.382c-0.514,-0.62 -3.401,-5.478 -6.414,-10.795 -4.622,-8.157 -6.151,-10.166 -8.698,-11.433 -0.456,-0.227 7.666,-15.462 8.702,-16.322 2.739,-2.273 3.908,-0.702 14.118,18.984 5.448,10.505 5.908,11.706 4.353,11.383 -0.505,-0.105 -0.934,0.21 -0.953,0.699 -0.045,1.153 -1.069,2.161 -2.196,2.161 -0.489,0 -0.895,0.343 -0.902,0.763 -0.021,1.346 -1.215,2.709 -1.967,2.244 -0.401,-0.248 -0.635,0.004 -0.519,0.56 0.115,0.556 -0.219,0.924 -0.744,0.819 -0.524,-0.105 -0.868,0.191 -0.763,0.658 0.296,1.317 -2.968,1.543 -4.018,0.279z"
android:strokeAlpha="1" android:strokeColor="#00000000"
android:strokeLineCap="round" android:strokeLineJoin="round" android:strokeWidth="20.3085556"/>
<path android:fillAlpha="1" android:fillColor="#ffcc00"
android:fillType="nonZero"
android:pathData="m85.958,162.87c-2.517,-1.396 -6.464,-3.605 -8.771,-4.908 -8.442,-4.768 -25.007,-14.063 -33.361,-18.718 -4.715,-2.628 -8.576,-5.092 -8.581,-5.477 -0.004,-0.385 0.152,-0.539 0.349,-0.343 0.196,0.196 22.244,-21.541 48.996,-48.305C131.963,37.725 135.415,34.561 136.985,37.1c0.624,1.01 4.82,26.009 9.514,56.692 0.813,5.316 1.669,10.292 1.902,11.059 0.233,0.767 1.129,6.028 1.991,11.691 0.862,5.663 1.851,11.929 2.196,13.924 0.593,3.42 0.516,3.686 -1.349,4.653 -4.569,2.37 -44.298,24.529 -48.464,27.031 -6.507,3.908 -10.747,4.089 -16.818,0.721z"
android:strokeAlpha="1" android:strokeColor="#00000000"
android:strokeLineCap="round" android:strokeLineJoin="round" android:strokeWidth="20.3085556"/>
</vector>
<vector android:height="42dp"
android:viewportHeight="500"
android:viewportWidth="500"
android:width="42dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillAlpha="1" android:fillColor="#ffcc00"
android:fillType="nonZero"
android:pathData="m133.685,346.193c-0.083,-0.881 -0.083,-2.033 0,-2.561 0.083,-0.528 -0.377,-0.961 -1.023,-0.961 -1.602,0 -9.774,-24.912 -12.813,-39.062 -3.396,-15.812 -4.365,-34.074 -2.836,-53.461 1.262,-15.998 7.217,-44.513 9.296,-44.513 0.574,0 0.712,-0.864 0.307,-1.921 -0.405,-1.057 -0.065,-1.921 0.756,-1.921 0.821,0 1.178,-0.51 0.793,-1.134 -1.008,-1.631 1.824,-9.112 3.45,-9.112 0.753,0 1.008,-0.361 0.566,-0.803 -0.442,-0.442 0.075,-2.5 1.147,-4.573l1.95,-3.771 4.723,3.458c2.598,1.902 5.796,4.681 7.108,6.174 1.312,1.494 2.508,2.428 2.657,2.076 0.414,-0.975 31.457,23.184 31.457,24.481 0,0.623 0.432,0.996 0.961,0.828 1.656,-0.526 15.689,10.768 15.689,12.625 0,0.972 -0.432,1.768 -0.961,1.77 -0.528,0.001 -1.108,2.062 -1.288,4.58 -0.18,2.517 -0.828,5.542 -1.439,6.721 -1.696,3.27 -0.945,35.45 1.088,46.595l1.8,9.872 -3.738,2.935c-2.056,1.614 -4.181,2.791 -4.723,2.615 -0.542,-0.176 -0.985,0.187 -0.985,0.807 0,1.398 -13.481,11.68 -15.314,11.68 -0.735,0 -1.336,0.501 -1.336,1.113 0,1.703 -23.159,18.798 -24.807,18.312 -0.796,-0.235 -1.026,-0.008 -0.512,0.509 1.062,1.062 -7.769,8.153 -10.221,8.207 -0.881,0.021 -1.669,-0.685 -1.753,-1.565z"
android:strokeAlpha="1" android:strokeColor="#00000000"
android:strokeLineCap="round" android:strokeLineJoin="round" android:strokeWidth="34.1012001"/>
<path android:fillAlpha="1" android:fillColor="#cc0000"
android:fillType="nonZero"
android:pathData="m188.908,225.867c-4.579,-3.517 -12.647,-9.649 -17.93,-13.627 -5.283,-3.978 -9.893,-7.538 -10.246,-7.911 -0.352,-0.373 -6.259,-4.925 -13.127,-10.116 -6.868,-5.19 -12.487,-10.014 -12.487,-10.718 0,-1.725 12.219,-22.052 17.101,-28.448 19.924,-26.103 44.636,-46.32 72.231,-59.092 17.565,-8.13 16.497,-7.708 28.494,-11.255 25.393,-7.508 61.536,-9.635 87.851,-5.17 20.317,3.447 51.647,14.917 64.519,23.62 1.78,1.204 5.828,3.863 8.994,5.911 8.01,5.178 20.497,14.64 20.497,15.532 0,1.877 -55.363,55.76 -56.126,54.625 -0.476,-0.708 -3.459,-3.045 -6.629,-5.193 -49.693,-33.675 -120.59,-21.735 -156.568,26.367 -2.848,3.808 -7.997,12.427 -11.442,19.152 -3.445,6.725 -6.386,12.338 -6.535,12.472 -0.15,0.134 -4.018,-2.633 -8.597,-6.15z"
android:strokeAlpha="1" android:strokeColor="#00000000"
android:strokeLineCap="round" android:strokeLineJoin="round" android:strokeWidth="34.1012001"/>
<path android:fillAlpha="1" android:fillColor="#669900"
android:fillType="nonZero"
android:pathData="M292.315,452.464C227.826,448.201 168.297,409.977 137.361,352.967l-3.676,-6.774 4.978,-4.112c2.738,-2.262 11.189,-8.853 18.78,-14.647 7.591,-5.794 18.855,-14.468 25.03,-19.275 6.175,-4.807 11.888,-9.14 12.696,-9.63 0.945,-0.573 3.405,3.105 6.896,10.31 18.324,37.818 51.292,61.133 93.115,65.853 22.879,2.582 50.415,-2.125 69.874,-11.942l7.423,-3.745 11.147,8.628c6.131,4.745 19.739,15.254 30.241,23.351l19.094,14.723 -5.647,4.9c-26.157,22.699 -67.515,39.577 -101.462,41.406 -7.175,0.387 -14.486,0.856 -16.246,1.043 -1.761,0.187 -9.541,-0.08 -17.29,-0.592z"
android:strokeAlpha="1" android:strokeColor="#00000000"
android:strokeLineCap="round" android:strokeLineJoin="round" android:strokeWidth="34.1012001"/>
<path android:fillAlpha="1" android:fillColor="#3366cc"
android:fillType="nonZero"
android:pathData="m412.392,391.203c-9.861,-7.754 -22.973,-17.927 -29.136,-22.607 -6.163,-4.68 -11.204,-8.826 -11.201,-9.213 0.003,-0.387 3.517,-3.542 7.81,-7.012 13.934,-11.262 24.648,-28.731 29.318,-47.802l1.019,-4.162h-52.374,-52.374v-37.141,-37.141h90.174,90.174l1.703,9.285c2.12,11.56 2.108,43.849 -0.021,59.083 -3.899,27.894 -14.467,56.525 -29.363,79.554 -7.664,11.847 -24.383,31.449 -26.71,31.315 -0.598,-0.034 -9.156,-6.407 -19.017,-14.16z"
android:strokeAlpha="1" android:strokeColor="#00000000"
android:strokeLineCap="round" android:strokeLineJoin="round" android:strokeWidth="34.1012001"/>
</vector>
//package com.haerulmuttaqin.firebaselogin;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
import com.squareup.picasso.Picasso;
public class LoginActivity extends AppCompatActivity {
static final int GOOGLE_SIGN_IN = 123;
FirebaseAuth mAuth;
Button btn_login, btn_logout;
TextView text;
ImageView image;
ProgressBar progressBar;
GoogleSignInClient mGoogleSignInClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btn_login = findViewById(R.id.login);
btn_logout = findViewById(R.id.logout);
text = findViewById(R.id.text);
image = findViewById(R.id.image);
progressBar = findViewById(R.id.progress_circular);
mAuth = FirebaseAuth.getInstance();
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
btn_login.setOnClickListener(v -> SignInGoogle());
btn_logout.setOnClickListener(v -> Logout());
if (mAuth.getCurrentUser() != null) {
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
}
}
public void SignInGoogle() {
progressBar.setVisibility(View.VISIBLE);
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, GOOGLE_SIGN_IN);
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d("TAG", "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, task -> {
if (task.isSuccessful()) {
progressBar.setVisibility(View.INVISIBLE);
Log.d("TAG", "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
progressBar.setVisibility(View.INVISIBLE);
Log.w("TAG", "signInWithCredential:failure", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GOOGLE_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
GoogleSignInAccount account = task.getResult(ApiException.class);
if (account != null) firebaseAuthWithGoogle(account);
} catch (ApiException e) {
Log.w("TAG", "Google sign in failed", e);
}
}
}
private void updateUI(FirebaseUser user) {
if (user != null) {
String name = user.getDisplayName();
String email = user.getEmail();
String photo = String.valueOf(user.getPhotoUrl());
text.append("Info : \n");
text.append(name + "\n");
text.append(email);
Picasso.with(LoginActivity.this).load(photo).into(image);
btn_logout.setVisibility(View.VISIBLE);
btn_login.setVisibility(View.INVISIBLE);
} else {
text.setText("Firebase Login \n");
Picasso.with(LoginActivity.this).load(R.drawable.ic_firebase_logo).into(image);
btn_logout.setVisibility(View.INVISIBLE);
btn_login.setVisibility(View.VISIBLE);
}
}
private void Logout() {
FirebaseAuth.getInstance().signOut();
mGoogleSignInClient.signOut().addOnCompleteListener(this,
task -> updateUI(null));
}
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#ffcc00" />
<corners android:radius="50dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#fff"/>
<corners
android:bottomRightRadius="50dp"
android:bottomLeftRadius="50dp"
android:topLeftRadius="50dp"
android:topRightRadius="50dp"/>
</shape>
</item>
</selector>
@mfauzanazima
Copy link

This absolutely give the error..
Can't login with this method.. Maybe you can update and send the fixed code..

@alessio034
Copy link

me funciono perfectamente y lo pude integrar con mi app de tesis gracias

@akibraza91
Copy link

it is so helpfull

@GuillermoTorresRenner
Copy link

Muchísimas gracias por la lección y el código!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment