Hashim Javed
Hashim Javed

Reputation: 21

Getting Error: First argument to verifyIdToken() must be a Firebase ID token string

I'm making an XMLHttpRequest via POST method and then trying to get the tokenid to verify it in my node.js file. However, I keep on getting an error when I try to verify the token. I get an error saying:

Error: First argument to verifyIdToken() must be a Firebase ID token string


This is how I'm making the POST request:

var xhr = new XMLHttpRequest();
xhr.setRequestHeader(
  "Content-Type",
  "application/x-www-form-urlencoded"
);
xhr.onload = function() {
   console.log("Signed in as: " + xhr.responseText);
};
xhr.send("idtoken=" + user.getIdToken());

This is my code for the node.js file:

app.post("/tokensignin", (req, res) => {
    admin
        .auth()
        .verifyIdToken(req.body.idtoken)
        .then(function(decodedToken) {
            let uid = decodedToken.uid;
            console.log("uid is " + uid);
        })
        .catch(function(error) {
            console.log(error);
        });
});

I have tried using req.body, and req.body.token, but the error persists. When I try to print the idtoken, I get [Object object]

Upvotes: 0

Views: 1370

Answers (2)

Robbe Claessens
Robbe Claessens

Reputation: 99

What do you get when logging the request.body object? You have nodejs body parser installed, right? Are you using your own NodeJS server (I am almost certain you do)? But my question is why not use the Firebase environment?

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317497

You are sendinging data using the field name called "idtoken":

xhr.send("idtoken=" + user.getIdToken());

But you are accessing it on your backend using a different name "token":

.verifyIdToken(req.body.token)

I suggest doing more logging in general in order to better understand what you're working with on both sides and debug what's going on.

Upvotes: 1

Related Questions