将set-cookie标头发送到node.js中的重定向网址

Shu*_*tri 13 javascript cookies redirect node.js express

我正在使用node.js request模块从API获取id_token.获取id_token之后我想发送一个带有redirect uri响应的响应set-cookie header to the redirected url.但我无法弄明白该怎么做.

这是我的代码:

app.use("/nodejs-server/retrieveCode", function(req, res) {

        var clientID = 'some random string'
        var client_Secret = 'another random string'
        var code_token = clientID + ":" + client_Secret
        var buffer = new Buffer(code_token)
        var token = buffer.toString('base64')
        var rtoken = "Basic " + token;
        var headers = {
            'Content-Type': 'application/json',
            'Authorization': rtoken
        }
        var postData = {grant_type: 'authorization_code', code: req.query.code, redirect_uri: 'http://localhost:3000/nodejs-server/retrieveCode'} //Query string data
        var options  = {
            method: 'POST', //Specify the method
            body: postData,
            url: 'http://localhost:4000/server/token',
            json: true,
            headers: headers
        }
        request(options
        , function(error, response, body){
            if(error) {
                console.log(error);
            } else {
                //send a redirect uri and set-cookie header response

            }
        });
Run Code Online (Sandbox Code Playgroud)

我试过用

res.redirect(302, "http://localhost:9000");
Run Code Online (Sandbox Code Playgroud)

它能够重定向,但我也无法发送cookie

任何帮助表示赞赏.

Shu*_*tri 21

经过大量的试验,错误和谷歌搜索,我终于能够实现我的目标.为了将带有过期的cookie发送到重定向URL,我刚刚添加了

s = body.exp.toUTCString();
res.cookie('id_token' ,body.id_token, {expires: s});
res.redirect(302, 'http://localhost:8080');
Run Code Online (Sandbox Code Playgroud)

  • 只需注意,不需要302,因为Express已将302设为默认状态代码. (3认同)