AngularJS promises to catch 401

I use a very simple JS corner code to login, for example:

[...]
this.login = function(email, password) {
    promise = $http.post('/login',
            {
                'email': email,
                'password': password
            });
    promise.then(function(response){
            console.log("success");
        }).catch(function(response){
            console.log("catch");
        }).finally(function(response){
            console.log("finally");
        });
    return promise;
};
[...]

When the REST API generates a response with the code 200, then the client console will register success finallyand the user will log in.
When the server generates a response with a code of 403 or 500, the console will print catch finally.

But when the answer is with 401, angular will not print anything. The console will remain empty, only with a hint POST http://127.0.0.1/login 401 (Unauthorized). But not successor catchas a conclusion. And no finally.

In my project 403 will be displayed globally with $rootScope.$on('event:auth-forbidden', function(r) {...});. Therefore, the REST server will throw only 404 if something is not found, 403 when the user does not have permission and / or is not logged in, and 401 only if the login is unsuccessful.

, 401 $http?
.then 200 .catch != 200?

angularJS v1.6.4 angular -http-auth v1.5.0.

+4
1

, angular-http-auth 401 403:

$http : ( URL, ) HTTP 401 , , , : auth-loginRequired $rootScope.

401 ignoreAuthModule: true:

, , 401 403. ignoreAuthModule: true .

401. , authService.loginConfirmed() authService.loginCancelled(). - HTTP 401. - HTTP 401.

+2

Source: https://habr.com/ru/post/1685049/


All Articles