tellform/public/modules/users/controllers/authentication.client.controller.js

56 lines
1.4 KiB
JavaScript
Raw Normal View History

2015-07-01 23:14:39 +00:00
'use strict';
angular.module('users').controller('AuthenticationController', ['$scope', '$location', '$state', '$rootScope', 'User', 'Auth',
function($scope, $location, $state, $rootScope, User, Auth) {
2015-07-27 18:11:43 +00:00
$scope = $rootScope;
$scope.credentials = {};
$scope.error = '';
2015-07-01 23:14:39 +00:00
2015-07-27 18:11:43 +00:00
$scope.signin = function() {
2015-07-28 22:29:07 +00:00
User.login($scope.credentials).then(
2015-07-27 18:11:43 +00:00
function(response) {
Auth.login(response);
$scope.user = $rootScope.user = Auth.ensureHasCurrentUser(User);
2015-07-01 23:14:39 +00:00
if($state.previous.name !== 'home' && $state.previous.name !== 'verify' && $state.previous.name !== '') {
2015-07-27 18:11:43 +00:00
$state.go($state.previous.name);
} else {
2015-07-28 22:29:07 +00:00
$state.go('listForms');
2015-07-27 18:11:43 +00:00
}
},
function(error) {
$rootScope.user = Auth.ensureHasCurrentUser(User);
$scope.user = $rootScope.user;
2015-07-01 23:14:39 +00:00
2015-07-27 18:11:43 +00:00
$scope.error = error;
console.error('loginError: '+error);
2015-07-27 18:11:43 +00:00
}
);
};
2015-07-01 23:14:39 +00:00
2015-07-27 18:11:43 +00:00
$scope.signup = function() {
if($scope.credentials === 'admin'){
$scope.error = 'Username cannot be \'admin\'. Please pick another username.';
return;
}
2015-07-27 18:11:43 +00:00
User.signup($scope.credentials).then(
function(response) {
$state.go('signup-success');
},
function(error) {
console.error(error);
if(error) {
$scope.error = error;
console.error(error);
} else {
console.error('No response received');
}
}
);
2015-07-27 18:11:43 +00:00
};
2015-07-01 23:14:39 +00:00
}
2016-03-30 01:16:36 +00:00
]);