(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; } }]); })();