ReferenceError:未定义标记

时间:2016-11-30 07:25:48

标签: javascript angularjs ionic-framework

我一直在使用离子身份验证的登录教程。但是当我尝试运行程序并登录时,错误是:

  

ReferenceError:未定义标记       at storeUserCredentials(services.js:16)       at services.js:46       在Object.login(services.js:44)

有人可以帮我吗?这是我的服务:

app.service('AuthService', function($q, $http, USER_ROLES){
    var LOCAL_TOKEN_KEY = 'yourTokenKey';
    var username = '';
    var isAuthenticated = false;
    var role = '';
    var authToken;

    function loadUserCredentials() {
        var token = window.localStorage.getItem(LOCAL_TOKEN_KEY);
        if (token) {
            useCredentials(token);
        }
    }

    function storeUserCredentials(){
        window.localStorage.setItem(LOCAL_TOKEN_KEY, token);
        useCredentials(token);
    }

    function useCredentials(token){
        username = token.split('.')[0];
        isAuthenticated = true;
        authToken = token;

        if (username == 'admin') {
            role = USER_ROLES.admin
        }
        if (username == 'user') {
            role = USER_ROLES.public
        }

        $http.defaults.header.common['X-Auth-Token'] = token;
    }

    function destroyUserCredentials(){
        authToken = undefined;
        username = '';
        isAuthenticated = false;
        $http.defaults.header.common['X-Auth-Token'] = undefined;
        window.localStorage.removeItem(LOCAL_TOKEN_KEY);
    }

    var login = function(name, pw){
        return $q(function(resolve,reject){
            if ((name == 'admin' && pw == '1') || (name =='user' && pw == '1')){
                storeUserCredentials(name +'.yourServerToken');
                resolve('Login Success');
            } else {
                reject('Login Failed');
            }
        });
    };

    var logout = function(){
        destroyUserCredentials();
    };

    var isAuthorized = function(authorizedRoles){
        if (!angular.isArray(authorizedRoles)){
            authorizedRoles = [authorizedRoles];
        }
        return (isAuthenticated && authorizedRoles.indexOf(role))
    };

    loadUserCredentials();

    return {
        login : login,
        logout : logout,
        isAthorized : isAuthorized,
        isAuthenticated : function() {return isAuthenticated;},
        username : function() {return username;},
        role : function(){return role;}
    };
})

.factory('AuthInterceptor', function ($rootScope, $q, AUTH_EVENTS) {
  return {
    responseError: function (response) {
      $rootScope.$broadcast({
        401: AUTH_EVENTS.notAuthenticated,
        403: AUTH_EVENTS.notAuthorized
      }[response.status], response);
      return $q.reject(response);
    }
  };
})

.config(function ($httpProvider) {
  $httpProvider.interceptors.push('AuthInterceptor');
});

1 个答案:

答案 0 :(得分:0)

您已在函数token中创建了loadUserCredentials,因此other functions无法使用var token =""; function loadUserCredentials() { token = window.localStorage.getItem(LOCAL_TOKEN_KEY); if (token) { useCredentials(token); } } 。您需要将其设为全局。

sudo sysctl -a | grep "\.rp_filter" | awk '{print $1 "=0"}' | xargs sudo sysctl