简体   繁体   中英

How to getIdtoken on authentication

I just need to print Idtoken of user's once he is authenticated. I'm using phone authentication of firebase in flutter.

FirebaseAuth.instance.currentUser().then((user) {
            if (user != null) {
              var idtoken = user.getIdToken().toString();
              print("Firebase uid: "+idtoken);}}

how to correct this

based on https://firebase.google.com/docs/reference/js/firebase.User.html#getidtoken you need to handle the promise from the getTokenId() method call.

your code becomes:

FirebaseAuth.instance.currentUser().then(user => {
        if (user != null) {
           user.getIdToken().then(token => {
             //handle token
           }
        }
});

The same with async-await

final tokenResult = await FirebaseAuth.instance.currentUser();
final idToken = await tokenResult.getIdToken();
final token = idToken.token;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM