LoginController.js
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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');
};
}
]);