rsanchez
2014-11-14 79435726ed870d8395154e539ba2d9eb7789e0de
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
(function() {
   'use strict';
   var app = angular.module('securis');
   
   app.controller('LoginCtrl', ['$scope', '$http', '$location', 'toaster', '$L', '$store',
                             function($scope, $http, $location, toaster, $L, $store) {
       
       $('#username').focus();
       
       $scope.submit = function() {
           console.log('Sending user: ' + $scope.username + ' pass: ' + $scope.password);
           $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) {
                 toaster.pop('success', $L.get('Login successful'), $L.get('User {0} has logged in application', $scope.username), 1500);
                 var location = $store.get('location') || '/licenses';
                 $location.path(location);
                 $store.put('username', $scope.username);
                 $store.put('token', data.token);
                 $http.defaults.headers.common['X-SECURIS-TOKEN'] = data.token; 
             }).
             error(function(data, status, headers, config) {
               if (status === 403 /* forbidden */ || status === 401 /* unauthorized */) {
                   toaster.pop('error', $L.get('Login error'), $L.get('Invalid credentials'), 2000);
               } else if (status === 418 /* Teapot */) {
                   toaster.pop('error', $L.get('Login error'), $L.get(headers['X-SECURIS-ERROR-MSG']), 2000);
               } else {
                   console.error(data + " status: "+ status);
                   toaster.pop('error', $L.get('Unexpected Login error'), $L.get('Unexpected error HTTP ({0}) accessing to server. Contact with the administrator.', status), 5000);
               }
                $('#username').focus();
             });
             return false;
           }
   }]);    
   
})();