Jimmy Xu
Jimmy Xu

Reputation: 35

TypeError: authService.isAuthenticated is not a function

I'm trying to call isAuthenticated method in my controller, but it is telling me is not a function. Below a snippet of my code.

the controller

(function() {
'use strict';

angular
    .module('app')
    .controller('NavController', NavController);

NavController.$inject = ['USER_ROLES','AUTH_EVENTS','authService','$http'];

/* @ngInject */
function NavController(authService) {
    var vm = this;
    vm.name = '';

    activate();

    ////////////////

    function activate() {
        authService.isAuthenticated().then(function(response){
            vm.isLoggedin=response;
        });

    }
}})();

and in app.js (the main module) it includes all the dependencies

angular
.module('app', ['admin','app.router','app.auth','app.constants','user'])

authService resides in app.auth.js

(function() {
'use strict';

angular
    .module('app.auth',['LocalStorageModule','app.constants'])
    .factory('authService', authService);

authService.$inject = ['$http','localStorageService','USER_ROLES'];

/* @ngInject */
function authService($http,localStorageService,USER_ROLES) {
    var service = {

        isAuthenticated: isAuthenticated

    };
    return service;

    ////////////////



    function isAuthenticated(){
        return $http({
            method: 'GET',
            url: '/api/v1/isAuthenticated',
            headers: {
                'Authorization': 'Bearer '+localStorageService.get('token')
            }
        }).then(function successCallback(response) {
            return true;
        }, function errorCallback(response) {
            return false;
        });
    }


}})();

does anyone know what i did wrong here? need helps

Upvotes: 0

Views: 1777

Answers (1)

austinthedeveloper
austinthedeveloper

Reputation: 2601

It looks like your'e injecting multiple things and declaring only one. It should look like:

NavController.$inject = ['USER_ROLES','AUTH_EVENTS','authService','$http'];

/* @ngInject */
function NavController(USER_ROLES,AUTH_EVENTS,authService,$http) {

Upvotes: 1

Related Questions