Search code examples
angularbasic-authenticationhttp-status-code-401

How to use browser's default basic auth dialog pop up in angular2


Error image

I get the above error(401 unauthorised) on making a http post request to a server. I find many online sources suggesting to intercept it and make my own login interface but I would like to use the browser's default login interface for basic auth.

Browser's basic login interface

How could one pop up the browser's default basic auth dialog on receiving 401 unauthorised status in angular2 instead of just logging it as an error?


Solution

  • Same Issue here. Working chrome with :

    var xhttp=new XMLHttpRequest();
              xhttp.withCredentials = true;
              xhttp.open("GET", "https://httpbin.org/basic-auth/user/passwd", true);
              xhttp.send();
    

    Use "user" and "passwd" as credentials for this endpoint. Don't know your code but exceptions shows XMLHttpRequest. therefore withCredentials should do the job