LoginController.js 2.1 KB
angular.module('flashy.LoginController', ['ui.router']).
controller('LoginController', ['$scope', '$state', '$http', 'UserService',
function($scope, $state, $http, UserService) {
'use strict';
$scope.uniqueError = false;
$scope.loginError = false;
$scope.login = function(email, password) {
$http.post('/api/login', JSON.stringify({
'email': email,
'password': password
})).
success(function(data) {
UserService.getUserData();
$state.go('feed');
console.log(data);
}).
error(function(data, status, header, config) {
if (data.detail) { // assume 'invalid email or pass'
$scope.loginError = true;
}
console.log(data);
});
};
$scope.signUp = function(email, password) {
$http.post('/api/register', JSON.stringify({
'email': email,
'password': password
})).
success(function(data) {
$state.go('feed');
console.log(data);
}).
error(function(data, status, headers, config) {
console.log(data.email);
if (data.email == 'This field is required.') {
$scope.invalid = true;
$scope.uniqueError = false;
} else if (data.email) {
// assume 'email not unique' error
$scope.uniqueError = true;
$scope.invalid = false;
}
console.log(data);
});
};
$scope.triggerPasswordReset = function() {
$state.go('requestpasswordreset');
};
}
]);