(function (angular) {
'use strict';
angular.module('authApp', ['ngRoute'])
.controller('MainController', function ($scope, $http) {
$scope.param = function (obj) {
var parts = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(obj[key]));
}
}
return parts.join('&');
};
})
.controller('authController', function ($scope, $http, $q) {
$scope.form = {
email : '',
password: '',
spinner: '',
sendAuthForm: function(form){
var self = this;
self.spinner = true;
if (self.email != '' && self.password != ''){
self.checkValidAuth(form);
}else{
Materialize.toast('Заполните все поля!', 4000);
self.spinner = false;
}
},
checkAuthUser: function(email, password){
//Делаем запрос на сервер для проверки валидности пары email, password
var deferred = $q.defer();
$http({
method: 'post',
data: $scope.param({
email : email,
password : password
}),
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
url: '/index.php?route=checkout/login/checkFastAuth'
}).
success(function (response, status, headers, config) {
deferred.resolve(response, status, headers, config);
}).
error(function (response, status, headers, config) {
deferred.reject(response, status, headers, config);
});
return deferred.promise;
},
checkValidAuth: function(form){
var self = this;
var myPromise = self.checkAuthUser(self.email, self.password);
myPromise.then(function(resolve){
if (resolve.ans){
location.reload();
//или переход ко второму шагу
}else{
if (resolve.email === '1'){
//email есть
Materialize.toast('Неверная пара email/пароль!', 4000);
self.spinner = false;
}else{
//email нет
Materialize.toast('Данного email не существует!', 4000);
self.spinner = false;
}
}
}, function(reject){
Materialize.toast('Произошла ошибка, обовите страницу!', 4000);
});
}
};
})
KGZ1bmN0aW9uIChhbmd1bGFyKSB7CiAgICAndXNlIHN0cmljdCc7CiAgICBhbmd1bGFyLm1vZHVsZSgnYXV0aEFwcCcsIFsnbmdSb3V0ZSddKQogICAgCiAgICAgICAgLmNvbnRyb2xsZXIoJ01haW5Db250cm9sbGVyJywgZnVuY3Rpb24gKCRzY29wZSwgJGh0dHApIHsKCiAgICAgICAgICAgICRzY29wZS5wYXJhbSA9IGZ1bmN0aW9uIChvYmopIHsKICAgICAgICAgICAgICAgIHZhciBwYXJ0cyA9IFtdOwogICAgICAgICAgICAgICAgZm9yICh2YXIga2V5IGluIG9iaikgewogICAgICAgICAgICAgICAgICAgIGlmIChvYmouaGFzT3duUHJvcGVydHkoa2V5KSkgewogICAgICAgICAgICAgICAgICAgICAgICBwYXJ0cy5wdXNoKGVuY29kZVVSSUNvbXBvbmVudChrZXkpICsgJz0nICsgZW5jb2RlVVJJQ29tcG9uZW50KG9ialtrZXldKSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgcmV0dXJuIHBhcnRzLmpvaW4oJyYnKTsKICAgICAgICAgICAgfTsKICAgICAgICB9KSAKICAgICAgICAuY29udHJvbGxlcignYXV0aENvbnRyb2xsZXInLCBmdW5jdGlvbiAoJHNjb3BlLCAkaHR0cCwgJHEpIHsKICAgICAgICAgICAgCiAgICAgICAgICAgICRzY29wZS5mb3JtID0gewogICAgICAgICAgICAgICAgZW1haWwgOiAnJywKICAgICAgICAgICAgICAgIHBhc3N3b3JkOiAnJywKICAgICAgICAgICAgICAgIHNwaW5uZXI6ICcnLAogICAgICAgICAgICAgICAgc2VuZEF1dGhGb3JtOiBmdW5jdGlvbihmb3JtKXsKICAgICAgICAgICAgICAgICAgICAKICAgICAgICAgICAgICAgICAgICB2YXIgc2VsZiA9IHRoaXM7CiAgICAgICAgICAgICAgICAgICAgc2VsZi5zcGlubmVyID0gdHJ1ZTsKICAgICAgICAgICAgICAgICAgICBpZiAoc2VsZi5lbWFpbCAhPSAnJyAmJiBzZWxmLnBhc3N3b3JkICE9ICcnKXsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNlbGYuY2hlY2tWYWxpZEF1dGgoZm9ybSk7CiAgICAgICAgICAgICAgICAgICAgICAgIAogICAgICAgICAgICAgICAgICAgIH1lbHNlewogICAgICAgICAgICAgICAgICAgICAgICBNYXRlcmlhbGl6ZS50b2FzdCgn0JfQsNC/0L7Qu9C90LjRgtC1INCy0YHQtSDQv9C+0LvRjyEnLCA0MDAwKTsKICAgICAgICAgICAgICAgICAgICAgICAgc2VsZi5zcGlubmVyID0gZmFsc2U7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgfSwKICAgICAgICAgICAgICAgIGNoZWNrQXV0aFVzZXI6IGZ1bmN0aW9uKGVtYWlsLCBwYXNzd29yZCl7CiAgICAgICAgICAgICAgICAgICAgLy/QlNC10LvQsNC10Lwg0LfQsNC/0YDQvtGBINC90LAg0YHQtdGA0LLQtdGAINC00LvRjyDQv9GA0L7QstC10YDQutC4INCy0LDQu9C40LTQvdC+0YHRgtC4INC/0LDRgNGLIGVtYWlsLCBwYXNzd29yZAogICAgICAgICAgICAgICAgICAgIHZhciBkZWZlcnJlZCA9ICRxLmRlZmVyKCk7CiAgICAgICAgICAgICAgICAgICAgJGh0dHAoewogICAgICAgICAgICAgICAgICAgICAgICBtZXRob2Q6ICdwb3N0JywKICAgICAgICAgICAgICAgICAgICAgICAgZGF0YTogJHNjb3BlLnBhcmFtKHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGVtYWlsIDogZW1haWwsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICBwYXNzd29yZCA6IHBhc3N3b3JkCiAgICAgICAgICAgICAgICAgICAgICAgIH0pLAogICAgICAgICAgICAgICAgICAgICAgICBoZWFkZXJzOiB7J0NvbnRlbnQtVHlwZSc6ICdhcHBsaWNhdGlvbi94LXd3dy1mb3JtLXVybGVuY29kZWQnfSwKICAgICAgICAgICAgICAgICAgICAgICAgdXJsOiAnL2luZGV4LnBocD9yb3V0ZT1jaGVja291dC9sb2dpbi9jaGVja0Zhc3RBdXRoJwogICAgICAgICAgICAgICAgICAgIH0pLgogICAgICAgICAgICAgICAgICAgICAgICBzdWNjZXNzKGZ1bmN0aW9uIChyZXNwb25zZSwgc3RhdHVzLCBoZWFkZXJzLCBjb25maWcpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRlZmVycmVkLnJlc29sdmUocmVzcG9uc2UsIHN0YXR1cywgaGVhZGVycywgY29uZmlnKTsKICAgICAgICAgICAgICAgICAgICAgICAgfSkuCiAgICAgICAgICAgICAgICAgICAgICAgIGVycm9yKGZ1bmN0aW9uIChyZXNwb25zZSwgc3RhdHVzLCBoZWFkZXJzLCBjb25maWcpIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRlZmVycmVkLnJlamVjdChyZXNwb25zZSwgc3RhdHVzLCBoZWFkZXJzLCBjb25maWcpOwogICAgICAgICAgICAgICAgICAgICAgICB9KTsKICAgICAgICAgICAgICAgICAgICByZXR1cm4gZGVmZXJyZWQucHJvbWlzZTsKICAgICAgICAgICAgICAgIH0sCiAgICAgICAgICAgICAgICBjaGVja1ZhbGlkQXV0aDogZnVuY3Rpb24oZm9ybSl7CgogICAgICAgICAgICAgICAgICAgIHZhciBzZWxmID0gdGhpczsKCiAgICAgICAgICAgICAgICAgICAgdmFyIG15UHJvbWlzZSA9IHNlbGYuY2hlY2tBdXRoVXNlcihzZWxmLmVtYWlsLCBzZWxmLnBhc3N3b3JkKTsKCiAgICAgICAgICAgICAgICAgICAgbXlQcm9taXNlLnRoZW4oZnVuY3Rpb24ocmVzb2x2ZSl7CgogICAgICAgICAgICAgICAgICAgICAgICBpZiAocmVzb2x2ZS5hbnMpewogICAgICAgICAgICAgICAgICAgICAgICAgICAgbG9jYXRpb24ucmVsb2FkKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAvL9C40LvQuCDQv9C10YDQtdGF0L7QtCDQutC+INCy0YLQvtGA0L7QvNGDINGI0LDQs9GDCiAgICAgICAgICAgICAgICAgICAgICAgIH1lbHNlewoKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGlmIChyZXNvbHZlLmVtYWlsID09PSAnMScpewogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIC8vZW1haWwg0LXRgdGC0YwKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBNYXRlcmlhbGl6ZS50b2FzdCgn0J3QtdCy0LXRgNC90LDRjyDQv9Cw0YDQsCBlbWFpbC/Qv9Cw0YDQvtC70YwhJywgNDAwMCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgc2VsZi5zcGlubmVyID0gZmFsc2U7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICB9ZWxzZXsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvL2VtYWlsINC90LXRggogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIE1hdGVyaWFsaXplLnRvYXN0KCfQlNCw0L3QvdC+0LPQviBlbWFpbCDQvdC1INGB0YPRidC10YHRgtCy0YPQtdGCIScsIDQwMDApOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIHNlbGYuc3Bpbm5lciA9IGZhbHNlOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfSwgZnVuY3Rpb24ocmVqZWN0KXsKICAgICAgICAgICAgICAgICAgICAgICAgTWF0ZXJpYWxpemUudG9hc3QoJ9Cf0YDQvtC40LfQvtGI0LvQsCDQvtGI0LjQsdC60LAsINC+0LHQvtCy0LjRgtC1INGB0YLRgNCw0L3QuNGG0YMhJywgNDAwMCk7CiAgICAgICAgICAgICAgICAgICAgfSk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH07CgogICAgICAgIH0p