I'm trying to restrict a user's access to a /topics
view and a /question
view. I have a login form on the home page. My HomeCtrl has the following login function:
$scope.doLogin = function() {
var user = {
username: $scope.username,
password: $scope.password
}
$http.post('data/user/users.json',
user
).success(function(data, status, headers, config) {
userService.isAuthenticated = true;
userService.username = data.username;
userService.password = data.password;
$location.path('/topics');
console.log('Yay! You logged in.')
}).error(function(data, status, headers, config) {
userService.isAuthenticated = false;
userService.username = '';
userService.password = '';
$scope.errorMessage = 'Failed to log in. Please check username and password.'
});
}
And I have a factory set up to handle the current state of the data:
angular.module('myApp.userService', []).
factory('userService', function() {
var credentials = {
username: '',
password: '',
isAuthenticated: false
};
return credentials;
});
And this is my $routeProvider:
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/home.html',
controller: 'HomeCtrl'
}).
when('/topics', {
templateUrl: 'partials/topics.html',
controller: 'TopicCtrl',
//resolve: isAuthenticated
}).
when('/question', {
templateUrl: 'partials/question.html',
controller: 'QuestionCtrl',
//resolve: isAuthenticated
}).
when('/terms', {
templateUrl: 'partials/terms.html',
controller: 'TermsCtrl'
}).
otherwise({
redirectTo: '/'
});
}]);
I've tried setting up a resolve like the following:
resolve: {
doLogin: HomeCtrl.doLogin
}
But that returns the following error:
Failed to instantiate module myApp due to: ReferenceError: HomeCtrl is not defined at http:// localhost/2014-02-10/app/js/app.js:33:18 at Object.d [as invoke]
Any help is greatly appreciated - I've been looking at this all day and it's driving me mad.