Can't seem to get res.cookie to set cookie for reason unknown to myself, why is this the case?

时间:2019-04-17 00:28:57

标签: node.js express

I am trying to set a cookie via express with the following code snippet:

return res.cookie('session', sessionCookie, options).status(200).json({ user });

where sessionCookie is a long string and options is an object as follows:

const options = {
    maxAge: 86400 * 1000,
    httpOnly: true,
    secure: false,
    signed: true
};

I've configured CORS appropriately and have tried using both Postman and axios to generate requests, Postman shows no cookies in the response and axios via browser is the same.

No idea what I'm doing wrong, any help would be great thanks.

user is just an object containing token values and what not

EDIT: Here is all the server side code that is related.

router.post('/auth/login', (req, res) => {
    const result = Joi.validate(
        { email: req.body.email, password: req.body.password },
        schema
    );
    if (result.error) {
        return res
            .status(400)
            .json({ message: result.error.details[0].message });
    }

    firebase
        .auth()
        .signInWithEmailAndPassword(req.body.email, req.body.password)
        .then(result => {
            const user = result.user;
            const expiresIn = 60 * 60 * 24 * 5 * 1000;

            firebase
                .auth()
                .currentUser.getIdToken(true)
                .then(idToken => {
                     admin
                        .auth()
                        .createSessionCookie(idToken, {
                            expiresIn,
                        })
                        .then(sessionCookie => {
                            const options = {
                                maxAge: 86400 * 1000,
                                httpOnly: false,
                                secure: false,
                                signed: true
                            };
                            return res.cookie('session', sessionCookie, options).status(200).json({ user });
                        })
                        .catch(err => console.log(err));
                 });
        })
        .catch(err => {
            return res.status(403).json({ message: err.message });
        });
    });

0 个答案:

没有答案