How to avoid $compile:tpload errors on 401 status code response

后端 未结 3 882
抹茶落季
抹茶落季 2021-02-02 15:58

We are developing a Single Page Application with AngularJS and ASP.NET MVC Json Rest API.

When an unauthenticated client tries to navigate to a private route (Ex: /F

3条回答
  •  囚心锁ツ
    2021-02-02 16:23

    As I see it, you have two options:

    Option A)

    go with the interceptors. However, to eliminate the compile you need to return success status code inside response error (BAD) OR redirect to the login page inside the interceptor (Good):

    app.factory('authInterceptorService', function () {
    
        var interceptor = {};
    
        interceptor.responseError = function (rejection) {
    
            if (rejection.status === 401 && rejection.config.url === "home template url") {
    
                //BAD IDEA
                //console.log("faking home template");
                //rejection.status = 200;
                //rejection.data = "

    should log in to the application first

    "; //GOOD IDEA window.location = "/login.html"; } return rejection; } return interceptor; });

    and on app config:

    app.config(['$httpProvider', function ($httpProvider) {
    
       $httpProvider.interceptors.push('authInterceptorService');
    }
    

    Option b)

    make the home template public. After all it should be just html mark-up, without any sensible information.

    this solution is clean...and perhaps is also possible.

提交回复
热议问题