(function(){
'use strict';
angular.module('ammicoAuth',['ngRoute'])
.controller('authCtrl', function($location, $rootScope, $routeParams, $modal){
$modal.open({
templateUrl: 'auth/' + $routeParams.action + '_modal.html',
controller: 'auth' + $routeParams.action + 'Ctrl'
}).result.then(function (result) {
$location.path(result);
}, function () {
$location.path('/');
});
})
.controller('authloginCtrl', function($modalInstance, $scope, authApi, $window, $http, $rootScope){
$scope.login = function(){
authApi.login.save({email:$scope.email, password:$scope.password}, function(data){
$rootScope.globals.userLogged = true;
localStorage.setItem('token', data.token);
$http.defaults.headers.common.Authorization = 'Token ' + localStorage.getItem('token');
$modalInstance.close('/');
});
};
$scope.register = function () {
$modalInstance.close('/auth/register');
};
$scope.cancel = function () {
$modalInstance.close('/');
};
})
.controller('authlogoutCtrl', function($modalInstance, $scope, $window, $http, $rootScope){
localStorage.removeItem('token');
$rootScope.globals.userLogged = false;
delete $http.defaults.headers.common.Authorization;
$scope.cancel = function () {
$modalInstance.close('/');
};
})
.controller('authregisterCtrl', function($scope, $modalInstance, authApi){
$scope.register = function () {
authApi.user.save({
email: $scope.email,
password: $scope.password,
}, function(){
$modalInstance.close('/auth/login');
});
};
$scope.back = function () {
$modalInstance.close('/auth/login');
};
});
})();