passion
passion

Reputation: 1020

Express: Pass the response of a method to another method in the same post

I have two subparts in my app.post, the first one return a string called customToken which I want to pass as a parameter into the second one.
Could somebody remind me where I am getting it wrong?
I get an error of Can't set headers after they are sent.

app.post('/.custom-token', function (req, res, nex) {
    var token = null;

    //part ONE
    admin.auth().createCustomToken(req.body.uid)
        .then(function(customToken) {
            console.log("Successfully created custom-token:", customToken);
            res.status(200).send({token: customToken});
            nex();
            return token = res.status(200).send({token: customToken});
        })
    .catch(function(error) {
        console.log("Error creating custom token:", error);
        res.status(400).send({error: error})

    });

    //part TWO: i need to pass in the token generated above
    firebase.auth().signInWithCustomToken(token)
        .then(function (firebaseUser) {
            //...
        })
        .catch(function(error) {                       
            // ...
        });
});

Upvotes: 2

Views: 1806

Answers (2)

Hosar
Hosar

Reputation: 5292

You can use a middleware, something as follow.:

const getUserToken = (req, res, next) => {
    admin.auth().createCustomToken(req.body.uid)
        .then(function(customToken) {
            console.log("Successfully created custom-token:", customToken);
            req.cutomToken = customToken;

        //Here will pass the control to the next handler
            next();

        })
    .catch(function(error) {
        console.log("Error creating custom token:", error);
        res.status(400).send({error: error});
        res.end();
    });
});

You use the middleware here.

app.post('/.custom-token',getUserToken ,function (req, res, next) {
     const token = req.cutomToken;
     firebase.auth().signInWithCustomToken(token)
        .then(function (firebaseUser) {
            //...
        })
        .catch(function(error) {                       
            // ...
        });
});

This has the advantage that you can reuse the middleware on several endpoints.
Hope this help.

Upvotes: 1

Asif Saeed
Asif Saeed

Reputation: 2045

You can do this by promise chaining

 app.post('/.custom-token', function (req, res, nex) {
    var token = null;

    //part ONE
    admin.auth().createCustomToken(req.body.uid)
        .then(function(customToken) {
            console.log("Successfully created custom-token:", customToken);
            return customToken;
        })
        .then(function(token){
             // token returned in the last promise will be available here in token parameter
            return firebase.auth().signInWithCustomToken(token)
        })
        .then(function(firebaseUser){
                //....
                //here you can send response back
                return res.status(200).send();
        })
        .catch(function(error) {
            console.log("Error creating custom token:", error);
            res.status(400).send({error: error})

        });
});

The second part in your code will run without waiting for the response from first part because of async so token will be null for second part of your code.

Upvotes: 2

Related Questions