Authenticate Firebase in Polymer using OAuth token

2019-06-12 11:02发布

问题:

I have a Firebase database that authenticates with a Google account. Currently I have a <google-signin-aware> element that details the app's sign-in details for use with the Google Sign-in API, and upon sign-in authenticates a Firebase reference with the access token returned by the Google sign-in.

From the callback for <google-signin-aware> element on successful sign-in:

var ref = new Firebase("https://<myapp>.firebaseio.com");
var access_token = gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token;
ref.authWithOAuthToken("google", access_token, function (error, authData){/*...*/});

To try to integrate further with Polymer, I'm migrating to the <firebase-auth> element which is a "wrapper for the Firebase authentication API". However I cannot find an equivalent method to authenticate with an access token. The login() method has two parameters, is there any way to feed the access token to the login method so that Firebase authenticates with this token rather than its own pop-up window?

回答1:

The <firebase-auth> element has an internal Firebase reference ref that you can access by calling

var ref = document.querySelector('#fbauth').ref

And then auth as normal

var access_token = gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token;
ref.authWithOAuthToken("google", access_token, function (error, authData){/*...*/});


回答2:

You might be interested by my custom set that extends Firebase Polymer element set: https://github.com/MeTaNoV/firebase-element-extended