将set-cookie标头发送到node.js中的重定向URL [英] Send a set-cookie header to a redirect url in node.js

查看:61
本文介绍了将set-cookie标头发送到node.js中的重定向URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用node.js request 模块从API提取id_token.提取了id_token后,我想发送一个 redirect uri 响应和一个 set-cookie标头到重定向的URL .但是我不知道该怎么做.

I am using node.js request module to fetch an id_token from an API. After fetching that id_token I want to send a redirect uri response with a set-cookie header to the redirected url. But I can't quite figure out how to do it.

这是我的代码:

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

            }
        });

我尝试使用

res.redirect(302, "http://localhost:9000");

它可以重定向,但我也不能随同发送cookie

and it is able to redirect but I am not able to send the cookie with it as well

感谢您的帮助.

推荐答案

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

After lots of trials and errors and googling I was finally able to achieve my goal. In order to send a cookie with an expiry to the the redirect URL, I just added

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

这篇关于将set-cookie标头发送到node.js中的重定向URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆