angular.module('keyringLogin', ['auth'])
.component('keyringLogin', {
	templateUrl	: 'users/login',
	controllerAs: 'login',
	controller: ['$scope', '$http', '$location', '$httpParamSerializerJQLike', 'auth', function($scope, $http, $location, $httpParamSerializerJQLike, auth) {
		this.username = '';
		this.password = '';
		

		this.submit = function(e, url) {
			var url = e.target.action;
			e.preventDefault();
			e.stopPropagation();

			$http({
				method: 'POST',
				url: url,
				data: $httpParamSerializerJQLike({
					username: this.username,
					password: this.password
				}),
				headers: { 
					'Content-Type': 'application/x-www-form-urlencoded'
				}
			}).success(function(data) {
				auth.setToken(data.token);
				$location.path('/');
			}).error(function(e) {
				this.error = e;
			}.bind(this));

			return false;
		};
	}]
})