How to send current user's(person using the app), Firebase Id token,from the Flutter app, to restapi, using http POST method?

405

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'},
                              );

Share:
405
Sai
Author by

Sai

Updated on December 21, 2022

Comments

  • Sai
    Sai over 1 year

    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
    Sai almost 4 years
    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?
  • Taym95
    Taym95 almost 4 years
    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!!
  • Sai
    Sai almost 4 years
    Can we do like this answer : stackoverflow.com/a/50295533/13462594