Roberto Sánchez
2013-12-26 6e28963da25edf94a84468423f74acc381699542
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
(function() {
   'use strict';
   var app = angular.module('app', ['ngRoute']);
   
   app.controller('LoginCtrl', ['$scope', '$http', '$window',
                             function($scope, $http, $window) {
       
       $scope.$loginerror = false;
       $('#loginError').removeClass('hide');
       
       $scope.hideAlert = function() {
           $scope.$loginerror = false;
            $('#user').focus();
       }
       $scope.submit = function() {
           console.log('Sending user: ' + $scope.username + ' pass: ' + $scope.password);
           $scope.hideAlert();
           $http({    method: 'POST', 
                   url: '/user/login',
                   headers: {
                       "Content-Type": "application/x-www-form-urlencoded"
                   }, 
                   data: $.param({
                       username: $scope.username,
                       password: $scope.password
                   })
           }).
             success(function(data, status, headers, config) {
               $window.location.href = "/main.html";
             }).
             error(function(data, status, headers, config) {
               console.error(data + " status: "+ status);
               $scope.$loginerror = true;
               if (status === 403 /* forbidden */) {
                   $scope.$errormsg = 'Invalid credentials'
               } else {
                   $scope.$errormsg = 'Unexpected error HTTP ' + status + ' accessing to server. Contact with the administrator.'
               }
                $('#user').focus();
             });
             return false;
           }
   }]);    
   
})();