简体   繁体   中英

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?

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

if (!req.isAuthenticated()) {
    res.redirect("/login");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM