Prevent an authorization dialog on 401

Hi,
I need to check whether the user is logged in and redirect it to the login page if not.
I added the following code in app.js :

function start_event(mode, url, params, xhr, headers, data, promise) {
    if (promise)
        promise.then(null,
                function (err) {
                    if (err.status === 401) {
                        window.location.href = "/login"
                    }
                }
        );
}

webix.attachEvent("onBeforeAjax", start_event);

It works, but if user isn't authorized then main page and browser authorization dialog show for a moment before redirect to the login page.
How to prevent that?

Comments

  • You can try to use the code like next

    webix.attachEvent("onAjaxError", function(xhr){
       if (xhr.status == 401)
           window.location.href = "/login";
    });
    
  • edited March 2018

    Hi Maksim,

    Using your code snippet I always get status == 0, and therefore I can not distinguish whether credentials are wrong or the server is down. If you could provide any further idea I will greatly appreciate that!

    P.S. I do CORS request

  • edited March 2018

    status = 0 can occur if the request was blocked by client side ( so there was no real request at all ). Also please be sure that CORS is correctly configured, if the request was blocked by CORS rules, a browser will not give access to the response content, which includes access to response status as well.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!