/* Page d'authentification (index.php) - login en ajax : serveur.php mode=login (code navigateur compris) - messages temporaires (PNotify) - autologin "Se souvenir de moi" : serveur.php mode=autologin - proposition d'enregistrement / mise à jour des identifiants par le navigateur */ var auth_url="/zinc/serveur.php"; var auth_welcome="/welcome.php"; var auth_code_ouvert=false; function auth_notice(type,titre,texte) { new PNotify({title:titre,text:texte,type:type,icon:(type=='error'?'fas fa-exclamation-circle':'fas fa-check-circle'),hide:true,delay:5000,mouse_reset:false,stack:stack,addclass:"stack-bottom-right"}); } // Chrome / Edge / Android : demande explicitement l'enregistrement ou la mise à jour des identifiants // (les autres navigateurs s'appuient sur le formulaire + la navigation vers welcome.php) function auth_memoriser() { try { if(window.PasswordCredential&&navigator.credentials&&navigator.credentials.store) { var u=$.trim($('#user').val()); navigator.credentials.store(new PasswordCredential({id:u,name:u,password:$.trim($('#password').val())})); } } catch(e){} } function auth_reponse(xdata) { if(!xdata){auth_notice('error','Erreur',"Réponse vide du serveur.");return;} if(xdata.dwn){save_localstorage('dwn',xdata.dwn);} if(xdata.erreurarendre=='ajax_success') { auth_memoriser(); auth_notice('success',xdata.form_titre,xdata.form_message); setTimeout(function(){window.location.assign(auth_welcome);},1000); return; } if(xdata.erreurarendre=='ajax_success_navigateur') { // Nouveau navigateur : un code vient d'être envoyé par mail auth_code_ouvert=true; $('#mailc').html(xdata.mailc); $('#navigateur_win').window('open'); $('#navigateur_code').val('').focus(); return; } // Erreur : champs en rouge + message temporaire $.each(xdata.form_erreur||{},function(key,value){if(value.length>0){$('#'+key).addClass('form-control-danger');}}); auth_notice('error',xdata.form_titre||'Erreur',xdata.form_message||"Echec de l'authentification"); if(auth_code_ouvert){$('#navigateur_code').val('').focus();}else{$('#password').focus();} } function auth_appel() { var bt=$('#auth_formsubmit'); var libelle=bt.html(); $('#auth_form .form-control-danger').removeClass('form-control-danger'); bt.html('Veuillez patienter...').prop('disabled',true); $.ajax( { type:'POST', url:auth_url, dataType:'json', timeout:20000, data:{ mode:'login', user:$('#user').val(), password:$('#password').val(), sesouvenir:($('#sesouvenir').is(':checked')?1:0), dw_n:(load_localstorage('dwn')||''), navigateur_code:(auth_code_ouvert?$.trim($('#navigateur_code').val()):'') }, success:auth_reponse, error:function(jqXHR,textStatus) { auth_notice('error','Erreur',(textStatus=='timeout')?"Le serveur ne répond pas, merci de réessayer.":"Communication impossible avec le serveur."); }, complete:function(){bt.html(libelle).prop('disabled',false);} }); } // Formulaire principal $('#auth_form').on('submit',function(e) { e.preventDefault(); if($('#auth_formsubmit').prop('disabled')){return false;} if($.trim($('#user').val())==''||$('#password').val()=='') {auth_notice('error','Erreur',"Merci de renseigner votre compte et votre mot de passe.");return false;} auth_code_ouvert=false; auth_appel(); return false; }); // Code reçu par mail (nouveau navigateur) $('#login_navigateur').on('submit',function(e) { e.preventDefault(); if($('#auth_formsubmit').prop('disabled')){return false;} if($.trim($('#navigateur_code').val())==''){auth_notice('error','Erreur',"Merci de saisir le code reçu par mail.");return false;} auth_code_ouvert=true; auth_appel(); return false; }); // Autologin ("Se souvenir de moi", 7 jours) - sauf juste après une déconnexion (?to=1) if(!/[?&]to=1(&|$)/.test(window.location.search)) { $.ajax( { type:'POST', url:auth_url, dataType:'json', timeout:15000, data:{mode:'autologin'}, success:function(xdata){if(xdata&&xdata.autologin==1){window.location.replace(auth_welcome);}} }); }