US Office

1176 Shadeville Rd, Crawfordville Florida 32327, USA

 +1 (850) 780-1313

India Office

Office No. 501, Shree Ugati Corporate Park, Gandhinagar - 382421, Gujarat, India

[email protected]

How to Get the ID Token from FirebaseAuth in Flutter?

How to get the ID token from FirebaseAuth in Flutter

How to Get the ID Token from FirebaseAuth in Flutter?

Hello folks, hope you guys are doing well !!!
Earlier we have been through Step By Step Guide to Firebase With Flutter So in this article we will learn about how to get the ID token from FirebaseAuth?

We have to use dependencies to get Logged In user token using FirebaseAuth which is listed below:

firebase_auth: 
firebase_core: 
google_sign_in: 
flutter_signin_button:

You can also use command to add flutter pub

flutter pub add 

Flutter Pub

How to get the ID token from FirebaseAuth?

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

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

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

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

FirebaseUser user = await FirebaseAuth.instance.currentUser;

String token;

user.getIdTokenResult().then((result) {

    token = result.token;
});

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

then you can access the token with

var tokenResult = await response.user.getIdTokenResult();
log('TokenID ${tokenResult.token}');

and after that, you can use it into

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

Output

Firebase Auth UserID Token

Conclusion:

Hope this article is useful for you !!!
Thanks for reading !!!

Drop us your suggestion/feedback to serve you better.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Hire a Flutter Expert!
Consult Now

Post a Comment