Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
177
rated 0 times [  180] [ 3]  / answers: 1 / hits: 34300  / 8 Years ago, fri, june 10, 2016, 12:00:00

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.



Here is my code:



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

}
});


I tried using



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


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



Any help is appreciated.


More From » node.js

 Answers
72

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


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

[#61820] Wednesday, June 8, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sandra

Total Points: 708
Total Questions: 100
Total Answers: 84

Location: Bosnia and Herzegovina
Member since Thu, Jun 24, 2021
3 Years ago
sandra questions
Tue, Jun 30, 20, 00:00, 4 Years ago
Sun, May 31, 20, 00:00, 4 Years ago
Wed, May 20, 20, 00:00, 4 Years ago
Fri, May 31, 19, 00:00, 5 Years ago
;