3

i've been following login tutorial with authentication in ionic. but when i try run the program and login, the error is :

ReferenceError: token is not defined at storeUserCredentials (services.js:16) at services.js:46 at Object.login (services.js:44)

anyone can help me please? this is my service :

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
  • Your token variable is local to loadUserCredentials functions scope. You need to make it global Commented Nov 30, 2016 at 7:38

1 Answer 1

4

You have created token inside the function loadUserCredentials so it is not available to other functions.You need to make it global.

var token ="";
function loadUserCredentials() {
  token = window.localStorage.getItem(LOCAL_TOKEN_KEY);
  if (token) {
     useCredentials(token);
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.