1

I do http.get from angular js

$http.get('/api/users?id='+userID)

Further if unauthorized I do redirect with status from express js

res.status(401).location('/login').end();

Вut a response from the server 200 with login page in a response data

Why return status is 200 and not 401 how to fix it?

and my another question

Community
  • 1
  • 1
name
  • 63
  • 1
  • 8

2 Answers2

0

You must redirect user in client side. Try this:

$http.get('/api/users?id='+userID).error(function(data, status){

    if(status==401){
        window.location.href='/login';
    }

});

Server side:

res.status(401).send({ hasError: true });
MBN
  • 1,006
  • 7
  • 17
0

Your $http.get is waiting for a response which in your case is just the login page, and not a redirect instruction. Therefore you need to respond with a status code, then redirect on the client side.

$http.get('/api/users?id='+userID).error(function(data, status){

    if(status==401){
        $window.location.href='/login';
    }

});

On the server side, if they're not authorized, just send a status code.

res.status(401).end();
Alex Logan
  • 1,221
  • 3
  • 18
  • 27