2

I have created an app using flutter for UI and Restapi in nodejs as the backend. Now i want to verify the user, who signuped with the firebase authentication,the sending the current user's firebase Id token. In order, to do this I am sending the id token using the HTTP Post method. But got an error.

Sample code:

        import 'package:http/http.dart' as http;

    FirebaseUser user = await FirebaseAuth.instance.currentUser();
       if(user!=null){
  Future<IdTokenResult> idtoken = user.getIdToken();
    }
 http.Response response = await http.post("https://www.something.com/something/something",
                              body:jsonEncode({
                                "name": name.text,
                                "pincode": "$pincode",
                                "token": idtoken,
                                "phone": "${widget.phoneNo}",
                              }),
                                  headers: {'Content-Type': 'application/json'},
                              );

Got Error:

     Unhandled Exception: Converting object to an encodable object failed: Instance of 'Future<IdTokenResult>'

P.S: Suggest an edit, if something wrong with the question, I am for ideas (Thanks for the help in advance :) )

Sai
  • 143
  • 6

2 Answers2

4

You need token(String) not idtoken(IdTokenResult)

import 'package:http/http.dart' as http;

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

if(user!=null){
  IdTokenResult idtoken = await user.getIdToken(); 
  // get String token  
  String token = idtoken.token;
}
 http.Response response = await http.post("https://www.something.com/something/something",
                              body:jsonEncode({
                                "name": name.text,
                                "pincode": "$pincode",
                                "token": token,
                                "phone": "${widget.phoneNo}",
                              }),
                                  headers: {'Content-Type': 'application/json'},
                              );

Taym95
  • 2,331
  • 13
  • 14
  • Thanks for the answer, Taym95. So, in the rest api side, firebase-admin sdk, require only **token** not other parameters like time of creation,or expiration time, uid? to verify the current users? – Sai Jun 10 '20 at 12:38
  • 1
    Yes, firebase-admin needs only token(String) and you can thank me by upvoting the answer as correct so the others can benefit from the answer!! – Taym95 Jun 10 '20 at 12:43
  • Can we do like this answer : https://stackoverflow.com/a/50295533/13462594 – Sai Jun 10 '20 at 12:54
-1

I think the problem is getIdToken() part.

Can you try below code please:

FirebaseAuth.instance.currentUser().then(user => {
        if (user != null) {
           user.getIdToken().then(token => {
              http.Response response = await http.post("https://www.something.com/something/something",
                              body:jsonEncode({
                                "name": name.text,
                                "pincode": "$pincode",
                                "token": token,
                                "phone": "${widget.phoneNo}",
                              }),
                                  headers: {'Content-Type': 'application/json'},
                              );
        }
});
Lunedor
  • 1,410
  • 1
  • 14
  • 33