Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
Nodejs Ajax - redirect to the login page

I'm trying to solve this problem. I use Node Express and jQuery. The user has been logged out (from another page of the site), but is trying to send an ajax request. How to redirect it to the login page? I can throw an error like so:

if (!req.isAuthenticated()) {
    return res.json(function() {});
}

and after on the client side:

$.ajax({
    type: 'GET',
    ...
    error: function() {
        document.location.href = '/logout';
    }
})

But can it be done in some other way? For example, if you do not use Ajax, then I do this:

if (!req.isAuthenticated()) {
    return redirect('/login');
}

Something similar but with Ajax?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Since you mentioned you use Express, you can call the redirect function on the response:

if (!req.isAuthenticated()) {
    res.redirect("/login");
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!