Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Express response headers before redirect

I'm implementing a site login that takes in an email/password combo, retrieves an API token, and returns it to the user to get stored (encrypted) in localStorage.

Currently, on successful POSTing to /login, the app redirects the user to the index page, with the token attached as a query, like so (as suggested here):

login.post('/', function(req, res) {
    ...checking password...

    Auth.getToken(user, function(err, token) {
        res.redirect('/?token=' + token);
    });
});

This works fine, but I'd prefer to keep my URLs as clean as possible and set the token as a header instead:

login.post('/', function(req, res) {
    ...checking password...

    Auth.getToken(user, function(err, token) {
        res.set('x-access-token', token);
        console.log(res._headers);
            // --> {'x-powered-by': 'Express', 'x-access-token': <token>}
        res.redirect('/');
    });
});

console.log-ing res._headers shows that the headers are set as expected, but when I log req.headers on the request to the index page, it's not showing up:

{ host: 'localhost:3000',
  connection: 'keep-alive',
 'cache-control': 'max-age=0',
 accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
 'upgrade-insecure-requests': '1',
 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36',
 referer: 'http://localhost:3000/login',
 'accept-encoding': 'gzip, deflate, sdch',
 'accept-language': 'en-US,en;q=0.8',
 cookie: 'ifusr=crwj; _ga=GA1.1.1933420201.1409901705',
 'if-none-match': '"1195161647"' }

Any suggestions appreciated!

like image 403
wkd Avatar asked Aug 26 '15 19:08

wkd


People also ask

Can't set headers after they are sent redirect?

The "Cannot set headers after they are sent to the client" error occurs when the server in an express. js application sends more than one response for a single request, e.g. calling res. json() twice. To solve the error, make sure to only send a single response for each request.

Can t set headers after they are sent node?

The error "Error: Can't set headers after they are sent." means that you're already in the Body or Finished state, but some function tried to set a header or statusCode. When you see this error, try to look for anything that tries to send a header after some of the body has already been written.

What is RES setHeader?

res. setHeader() is a native method of Node. js and res. header() is an alias of res. set() method from Express framework.

What is redirect Express?

redirect() function lets you redirect the user to a different URL by sending an HTTP response with status 302. The HTTP client (browser, Axios, etc.) will then "follow" the redirect and send an HTTP request to the new URL as shown below.


1 Answers

Setting headers wouldn't work here because a redirect will execute a new http request, you can use express-session to store the auth token and fetch it when you need it

req.session.accessToken = token
like image 74
Julián Duque Avatar answered Oct 15 '22 03:10

Julián Duque