How to get the ID token from FirebaseAuth

ぐ巨炮叔叔 提交于 2019-12-20 12:24:08

问题


I am reading some Json files from my firebase database and I am trying to obtain an ID token for the current user to be used in the header as follows:

 var response = await httpClient.get(url,headers: {'Authorization':"Bearer ${FirebaseAuth.instance.currentUser.getToken()}"});

when I do the previous line, it seems that it does not get the correct token as I can not access the database, however, when I manually include an ID token in the string, my application works as intended. What is it exactly that I am doing wrong ?


回答1:


getIdToken() returns a Future - you have to await it to actually get the token string:

var token = await FirebaseAuth.instance.currentUser().getIdToken();
var response = await httpClient.get(url,headers: {'Authorization':"Bearer $token"});



回答2:


U can also retrieve the id token from the window object like so:

const idToken = window.gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().id_token



回答3:


I think that there is a bug in Flutter.

Now to call the method getIdToken() you need to do this:

FirebaseUser user = await FirebaseAuth.instance.currentUser();

String token;

user.getIdToken().then((result) {

    token = result.token;
});

You can't call FirebaseAuth.instance.currentUser().getIdToken()



来源:https://stackoverflow.com/questions/45563320/how-to-get-the-id-token-from-firebaseauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!