0

In my angular controller, I have the following code to check if logon is required:

$scope.checkApi = "api/check";
$http.post($scope.checkApi).then(function (response) 
{
    if (response.status != 200) 
    {
        alert("Logon required.");
    }
}, function (response) 
{
    if (response.status != 200)
    {
        alert("Logon required.");
    }
});

If logon is required, then from the controller I want to display the logon page to get user name and password, then use the user name and password to access the web API like this:

var userCredential = btoa(userName + ":" + password");
$http.get(usersApi, { headers: { "Authorization": "Basic " + userCredential } }).then(function (response) 
{
});

Thanks.

  • Check this out: http://stackoverflow.com/questions/11541695/redirecting-to-a-certain-route-based-on-condition. – ryndshn Oct 14 '15 at 15:19

1 Answers1

0

You can redirect to login page instead to giving alert. Use $window.location.href= url mapping for login page;

  • Thanks for the comment. If I added the following line: $window.location.href = 'pages/logon.html'; The logon page will display, then how can I get the user name and password from the logon page and go back to my main application? – AngularLearner Oct 14 '15 at 17:08
  • Ideally, the logon page should be opened as a model dialog on top of the main page, the main page should always stays. In my other case, I have a dialog like this in one of my html file: This dialog can be opened by button like this: – AngularLearner Oct 14 '15 at 18:09