question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

superagent global 401 handler

See original GitHub issue

I am using superagent to do my ajax data fetching job.

request
    .get('/some/end/point')
    .end(function(err, res) {

        if (err && res.status === 401) {
            // redirect to login
        }

        // do something with res.body

    });

many of my request need authentication check, but is that possible to config if (err && res.status === 401) {} globally?

or I should repeat the code in every request I make ?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
vicansocommented, Jan 18, 2016

You can use like this:

var unauthorizedRedirect = function(req) {
  req.on('response', function (res) {
    if (res.status === 401) {
      console.dir('redirects')
      // redirect to login
    }
  });
};

request
  .get('/some/end/point')
  .use(unauthorizedRedirect)
  .end(function(err, res) {
    // do something with res.body
  });
6reactions
vicansocommented, Jan 19, 2016

@allces

function authorizedGet(url) {
    return request.get(url)
        .use(unauthorizedRedirect);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

superagent global 401 handler · Issue #856 - GitHub
I am using superagent to do my ajax data fetching job. request .get('/some/end/point') .end(function(err, res) { if (err && res.status ...
Read more >
Handling 401 Unauthorised responses with visionmedia ...
I am trying to catch the 401 Unauthorised request with superagent, then handle the token refresh. The problem is superagent is not returning ......
Read more >
node_modulesold/superagent/docs · master - GitLab
SuperAgent is light-weight progressive ajax API crafted for flexibility, readability, and a low learning curve after being frustrated with many of the existing ......
Read more >
superagent - npm
Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features.
Read more >
@lakca/superagent-toolkit NPM | npm.io
Superagent plugin, providing frequently used tools, like reporting message of current request, directly retrieving specific property of response, etc.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found