128 lines
3.7 KiB
JavaScript
128 lines
3.7 KiB
JavaScript
import {
|
|
showError,
|
|
showSuccess,
|
|
} from './common.js';
|
|
|
|
export function val(api) {
|
|
let form = document.getElementById("login-form");
|
|
|
|
if (form.checkValidity() === false) {
|
|
this.event.preventDefault();
|
|
this.event.stopPropagation();
|
|
form.classList.add("was-validated");
|
|
return;
|
|
}
|
|
form.classList.add("was-validated");
|
|
|
|
let payload = {
|
|
email: document.getElementById("email").value,
|
|
password: document.getElementById("password").value,
|
|
};
|
|
|
|
const requestOptions = {
|
|
method: 'post',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(payload),
|
|
};
|
|
|
|
fetch(api + "/api/authenticate", requestOptions)
|
|
.then(response => response.json())
|
|
.then(response => {
|
|
console.log(response)
|
|
if (response.error === false) {
|
|
localStorage.setItem("token", response.authentication_token.token);
|
|
localStorage.setItem("token_expiry", response.authentication_token.expiry);
|
|
showSuccess("login-messages", "Login successful.")
|
|
// location.href = "/";
|
|
document.getElementById("login-form").submit()
|
|
} else {
|
|
showError("login-messages", response.message)
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
export function forgot(api) {
|
|
let form = document.getElementById("forgot-form");
|
|
|
|
if (form.checkValidity() === false) {
|
|
// this.event.preventDefault();
|
|
// this.event.stopPropagation();
|
|
form.classList.add("was-validated");
|
|
return;
|
|
}
|
|
form.classList.add("was-validated");
|
|
|
|
let payload = {
|
|
email: document.getElementById("email").value,
|
|
};
|
|
|
|
const requestOptions = {
|
|
method: 'post',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(payload),
|
|
};
|
|
|
|
fetch(api + "/api/forgot-password", requestOptions)
|
|
.then(response => response.json())
|
|
.then(response => {
|
|
console.log(response)
|
|
if (response.error === false) {
|
|
showSuccess("forgot-messages", "Password reset email sent")
|
|
} else {
|
|
showError("forgot-messages", response.message)
|
|
}
|
|
});
|
|
}
|
|
|
|
export function reset(api, email) {
|
|
let form = document.getElementById("reset-form");
|
|
|
|
if (form.checkValidity() === false) {
|
|
// this.event.preventDefault();
|
|
// this.event.stopPropagation();
|
|
form.classList.add("was-validated");
|
|
return;
|
|
}
|
|
form.classList.add("was-validated");
|
|
|
|
if (document.getElementById("password").value !== document.getElementById("verify-password").value) {
|
|
showError("reset-messages", "Passwords do not match.")
|
|
return
|
|
}
|
|
|
|
let payload = {
|
|
password: document.getElementById("password").value,
|
|
email: email,
|
|
};
|
|
|
|
const requestOptions = {
|
|
method: 'post',
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify(payload),
|
|
};
|
|
|
|
fetch(api + "/api/reset-password", requestOptions)
|
|
.then(response => response.json())
|
|
.then(response => {
|
|
console.log(response)
|
|
if (response.error === false) {
|
|
showSuccess("reset-messages", "Password reset")
|
|
setTimeout(function () {
|
|
location.href = "/login"
|
|
}, 2000)
|
|
} else {
|
|
showError("reset-messages", response.message)
|
|
}
|
|
});
|
|
}
|