0

I have such Api action method:

[Authorize(Roles = "Admin")]
[HttpGet, Route("")]
public List<TaskViewModel> GetAll()
{
    return TasksRepository.SelectAll(Context);
}

and using angular to fetch data through some http service; and have this routing configuration:

 $routeProvider.when('/Tasks', {
        title: 'Tasks',
        controller: 'tCtrlr',
        templateUrl: '../HTML/Tasks.html',
    })

The problem with that approach is that I cant notify the user that he is getting an empty data because he is not authorized to view its content? so I want to show a message or redirect him to another page, if the API action has Authorize attribute, that the user must be logged in or he cannot view the contents of the page?

mshwf
  • 7,009
  • 12
  • 59
  • 133

1 Answers1

0

You"ll have to check the response header .. If the status is 401 (unauthorized) then handle the redirection or else

Try something like this

$http("api/yourAPI")
.then(function (success) { ... }  ,
function (error) {
if (error.status==401) {
// handle unauthorised 
}
});

I'm not quite sure about syntax but you got the idea

Edit for Refrence Check this for more clear syntax but keep in mind that .success .error are deprecated in angularjs latest version

Community
  • 1
  • 1
Modar Na
  • 873
  • 7
  • 18