Шаблон формы авторизации:
<form id="fnt" name="system_auth_form<?=$arResult['RND']?>" method="post" target="_top" action="#" data-ajax-href="/include/auth.php">
<input type="hidden" name="mode" value="login" />
<?if (strlen($arParams["BACKURL"]) > 0 || strlen($arResult["BACKURL"]) > 0):?>
<input type="hidden" name="backurl" value="<?=($arParams["BACKURL"] ? $arParams["BACKURL"] : $arResult["BACKURL"])?>" />
<?endif?>
<input type="text" name="USER_LOGINt" maxlength="255" required placeholder="+7">
<input type="password" name="USER_PASSWORD" maxlength="255">
<input type="checkbox" name="USER_REMEMBER" id="USER_REMEMBER" checked>
<input type="submit" name="Login" class="" value="вход" />
</form>
Ajax скрипт:
$(document).ready( function(){
var form_item5 = $('#fnt');
form_item5.validate({
rules: {
USER_LOGIN: {required: true}
},
messages: {USER_LOGIN: 'Ошибка в форме'},
errorClass: 'error',
validClass: 'valid',
invalidHandler: function(event, validator) {},
errorPlacement: function(error, element) {
var cur = element;
error.addClass('error-message').insertAfter(cur);
},
submitHandler: function(form,event) {
var msgdata=$("#fnt").serialize();
$.ajax({
type: 'POST',
url: form_item5.attr('data-ajax-href'),
data: msgdata,
dataType: 'json',
success: function(result){
if(result.status){
$.fancybox.open('<div id="popup-success">\
<div class="title">Здравствуйте '+result.message+'!</div>\
</div>',{wrapCSS: 'fancybox-popup', padding: ['0','0','0','0'], closeClick : false,'beforeClose': function() { location.href = result.backurl; }, hideOnContentClick: false });
form_item5.get(0).reset();
form_item5.find('.valid').removeClass('valid');
}
}
});
event.preventDefault();
}
});
});
Обработчик /include/auth.php
<?
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
global $USER;
if(!is_object($USER)) $USER = new CUser;
$result = array();
$result['status'] = false;
$result['message'] = '';
if (isset($_POST['mode'])){
$mode=htmlspecialcharsbx($_POST['mode']);
switch($mode)
{
case 'login':
if(!$USER->IsAuthorized())
{
$user_tel = htmlspecialcharsbx($_POST['USER_LOGINt']);
$filter = Array("PERSONAL_PHONE" => $user_tel);
$sql = CUser::GetList(($by="id"), ($order="desc"), $filter);
if($sql->NavNext(true, "f_"))
{
if($_POST['USER_REMEMBER']=="on") $rem="Y"; else $rem="N";
$arAuthResult = $USER->Login($f_LOGIN, $_POST['USER_PASSWORD'], $rem);
$result['status']=true;
$result['message']=$f_NAME;
$result['backurl']=$_POST['backurl'];
$APPLICATION->arAuthResult = $arAuthResult;
}
}
break;
}
}
exit(json_encode($result));require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_after.php");
Комментариев нет :
Отправить комментарий