15
votes

I wanted to try and keep my native android firebase development to minimum so as to when I'm ready to port to IOS/web I won't be doing a lot there.

Right now firebase's Javascript doesn't allow google login from Android, this can be taken care of from the plugin. But what I'm stuck on is how to initialize firebase based on the Java Android Google login.

So this is what I'm trying to achieve:

Cordova calls Java-Android-Native login into google ---> based on this, how would I initialize firebase?

This plugin can let me login into google natively: https://www.npmjs.com/package/cordova-plugin-googleplus

But I guess I need auth token? token ID?

firebase.auth().signInWithCredential(credential).catch(function(error) {
  } else {
    console.error(error);
  }
 });

Can this give me the above required toke? https://developers.google.com/identity/sign-in/android/sign-in

GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);

Update 1: Just want to share more information. When getting the user logged in through google on android I have the below object

GoogleSignInAccount

https://developers.google.com/android/reference/com/google/android/gms/auth/api/signin/GoogleSignInAccount

It has public String getIdToken () & public String getServerAuthCode () why can't these be used to authenticate firebase using JS?

Update 2: Answer provided by Faraz seems to be working. Here is reference for the function signInWithCredential https://firebase.google.com/docs/reference/js/firebase.auth.Auth#signInWithCredential

Thank you for your help.

3

3 Answers

5
votes

Use auth.signInWithCredential with the GoogleAuthProvider credential.

Here is an example:

auth.signInWithCredential(firebase.auth.GoogleAuthProvider.credential(googleAccessToken)).then(function(user) {
  // Google User is logged in
}).catch(function(error) {
  // Error
});

Source for more infomation

1
votes

You can read this GitHub example about Firebase usage.

And Here you can find -

mFirebaseRef.authWithOAuthToken("google", token, new AuthResultHandler("google"));

Which (if all is success) calls this public void onAuthenticated(AuthData authData)

Where token is your getIdToken I mean it is possble to login into Firebase using Google, Facebook,Twitter in all cases you have to send token you receive to Firebase server that checks your token whether you already logged in or not. You can setup your own server in the same way.

0
votes

After google login you have to Use auth.signInWithCredential with the GoogleAuthProvider credential:

Here is an code:

private void firebaseAuthWithGoogle(GoogleSignInAccount account) { Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId()); final String name = account.getDisplayName(); final String email = account.getEmail(); AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null); getAuth().signInWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { Log.d(TAG, "onComplete: sign in with credentials " + task.isSuccessful()); if (task.ienter code heresSuccessful()) { Log.e(TAG, "success: sign in with credentials "); } if (!task.isSuccessful()) { Log.e(TAG, "onComplete: sign in with credentials " + task.getException()); } } }); }