Skip to content

Instantly share code, notes, and snippets.

@nabeelnazir163
Created August 24, 2017 15:27
Show Gist options
  • Save nabeelnazir163/8c84521a132a1d6c9a50a455678a5166 to your computer and use it in GitHub Desktop.
Save nabeelnazir163/8c84521a132a1d6c9a50a455678a5166 to your computer and use it in GitHub Desktop.
public class signin extends BaseActivity implements View.OnClickListener,
GoogleApiClient.OnConnectionFailedListener {
private SignInButton SignIn;
private static final int REQ_CODE = 9001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signin);
SignIn = (SignInButton) findViewById(R.id.bn_login);
SignIn.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQ_CODE) {
GoogleSignInResult result =
Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
// dismissProgressDialog();
}
} else {
// dismissProgressDialog();
}
} else {
// dismissProgressDialog();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.bn_login:
signIn();
break;
}
}
public void signIn(){
Intent intent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(intent,REQ_CODE);
}
private void firebaseAuthWithGoogle(final GoogleSignInAccount account) {
AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//Let's create the models quick(I know! it's weird time in this tutorial
//To be building it
User user = new User();
String photoUrl = null;
if (account.getPhotoUrl() != null) {
user.setPhotoUrl(account.getPhotoUrl().toString());
}
user.setEmail(account.getEmail());
user.setUser(account.getDisplayName());
user.setUid(mAuth.getCurrentUser().getUid());
FirebaseUtils.getUserRef(account.getEmail().replace(".", ","))
.setValue(user, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
mFirebaseUser = mAuth.getCurrentUser();
}
});
} else {
// dismissProgressDialog();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment