Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize firebase after android google login?

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.

like image 329
Noman Avatar asked Dec 16 '16 21:12

Noman


People also ask

How do I enable Google Firebase?

Enable Google as a sign-in method in the Firebase console: In the Firebase console, open the Auth section. On the Sign in method tab, enable the Google sign-in method and click Save.


2 Answers

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

like image 85
Faraz Avatar answered Oct 09 '22 21:10

Faraz


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.

like image 44
Vyacheslav Avatar answered Oct 09 '22 21:10

Vyacheslav