0
votes

I am writing a back-end server (using Node.js) to let users to interact with Firebase. Here is what I have so far:

  • I can let users create their firebase account (using email and password) via admin ADK;
  • I can let users log in to their firebase account (using email and password they already created) via REST API; and return back their idToken as well as refreshToken.

However, I really wonder that, every time a user log in, they will have an entirely new idToken (that lasts for 1 hour); so will the old one expire instantaneously when the new idToken is generated? So, I think I would use the firebase-admin SDK verifyIdToken function to do just what I need.

The problem is, even when I input the new idToken, the function just fails. I don't really know what's going on here.

Here's the error in case I don't perform a catch:

(node:9240) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received type number (1598554002)
at write_ (_http_outgoing.js:653:11)
at ServerResponse.write (_http_outgoing.js:621:15)
at D:\Viet Properties\Coding\ViMath\spreadsheet.js:315:19
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async verifyid (D:\Viet Properties\Coding\ViMath\spreadsheet.js:314:5)
at async Server.<anonymous> (D:\Viet Properties\Coding\ViMath\spreadsheet.js:584:17)
(node:9240) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag`--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9240) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Here's the function that I call:

async function verifyid(data, serverres) {
   serverres.write(data.id);
   await admin.auth().verifyIdToken(data.id).then(function(token) {
       serverres.write(token.exp)
   }).catch(function(error) {
       serverres.write(`Error code: ${error.code} \r\nError message: ${error.message}`);
   });
};

The main code is here:

var server = http.createServer(async function (req, res) {
    var urllink = url.parse(req.url, true);
    var urldata = urllink.query;

    if (Object.entries(urldata).length !== 0){
        switch(urldata.goal){
            //I dropped some cases here for clearance of the code
            case 'verifyid':
                await verifyid(urldata, res)
                res.end();
                break;
            default:
                res.write("Error: No action has been specified!")
                res.end();
        };
    };
  });


//initialize admin
var admin = require('firebase-admin');
var serviceAccount = require("./vimath_serviceAccountKey.json");
const { parse } = require('path');
const { userInfo } = require('os');

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: 'https://[my project].firebaseio.com'
});

server.listen(8080);

Can you guys have a look?

Thank you very much in advance,

And have a nice day, enter image description here enter image description here

1
@DougStevenson I even print it out in my webpage =((((((( - user49685
On Stack Overflow, please don't show images of text. Copy the text into the question so that it's easy to read, copy, and search. What you have right now is incredibly difficult to read. - Doug Stevenson
@DougStevenson I just wanna show that I did check for everything, I just don't know what went wrong, and I am confused. :< - user49685

1 Answers

1
votes

The error is thrown at this line:

serverres.write(token.exp)

That API expects a string or a Buffer, but you're passing a number.