-
-
Save enieber/70d7f77cbd3c0ad373e59068b8db2ca6 to your computer and use it in GitHub Desktop.
Function js de post para login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getContentForm = () => $('form#conteudo'); | |
const getDadosFormLogin = () => { | |
const usuario = $("input#usuario").val(); | |
const senha = $("input#senha").val(); | |
const form = { | |
usuario, | |
senha, | |
}; | |
return form; | |
} | |
const getConfigAjax = (url, dados, action) => { | |
return { | |
method: "POST", | |
type: "POST", | |
url: url, | |
contentType: "application/json; charset=utf-8", | |
data: JSON.stringify(dados), | |
beforeSend: action, | |
} | |
const postRequestAjax = (dadosForm, success, error) => { | |
const action = () => { | |
loading.style.display = "flex"; | |
} | |
const configAjax = getConfigAjax('/login', dadosForm, action); | |
$.ajax(configAjax, success, error); | |
} | |
const successLogin = () => { | |
event.isDefaultPrevented = () => false; | |
window.location.href = "dados"; | |
} | |
const errorLogin = (err) => { | |
$('input') | |
.removeClass('valid') | |
.addClass('error'); | |
const response = err.responseJSON; | |
const mensagem = $('#mensagem'); | |
mensagem.css('display', 'block'); | |
if(!response) { | |
mensagem.html('Erro inesperado. Tente Novamente!'); | |
return; | |
} | |
switch(response.status) { | |
case 403: | |
return mensagem.html('Usuario ou Senha Incorretos!'); | |
case 400: | |
return mensagem.html('Não foi posssivel realizar login!'); | |
case 401: | |
return mensagem.html(response.mensagem); | |
default: | |
return mensagem.html('Erro inesperado. Tente Novamente!'); | |
} | |
} | |
const submeterLogin = () => { | |
getContentForm() | |
.submit((event) => { | |
event.preventDefault(); | |
const dados = getDadosFormLogin(); | |
postRequestAjax(dados, successLogin, errorLogin); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment