web-base/js/account.js

214 lines
8.4 KiB
JavaScript
Raw Normal View History

2020-07-01 22:13:50 +02:00
$(document).ready(function () {
2021-11-11 14:25:26 +01:00
function showAlert(type, msg, raw=false) {
2020-07-01 22:13:50 +02:00
let alert = $("#alertMessage");
2021-11-11 14:25:26 +01:00
if (raw) {
alert.html(msg);
} else {
alert.text(msg);
}
2020-07-01 22:13:50 +02:00
alert.attr("class", "mt-2 alert alert-" + type);
alert.show();
}
function hideAlert() {
$("#alertMessage").hide();
}
2020-07-02 00:59:55 +02:00
function submitForm(btn, method, params, onSuccess) {
let textBefore = btn.text();
btn.prop("disabled", true);
btn.html("Submitting… <i class='fas fa-spin fa-spinner'></i>")
jsCore.apiCall(method, params, (res) => {
btn.prop("disabled", false);
btn.text(textBefore);
if (!res.success) {
showAlert("danger", res.msg);
} else {
onSuccess();
}
});
}
2020-07-02 00:47:45 +02:00
// Login
2021-01-07 17:11:23 +01:00
$("#username").keypress(function (e) { if(e.which === 13) $("#password").focus(); });
$("#password").keypress(function (e) { if(e.which === 13) $("#btnLogin").click(); });
2020-07-02 00:47:45 +02:00
$("#btnLogin").click(function() {
const username = $("#username").val();
const password = $("#password").val();
const createdDiv = $("#accountCreated");
const stayLoggedIn = $("#stayLoggedIn").is(":checked");
const btn = $(this);
hideAlert();
btn.prop("disabled", true);
2022-11-30 16:42:24 +01:00
btn.html(L("account.signing_in") + "… <i class=\"fa fa-spin fa-circle-notch\"></i>");
2020-07-02 00:47:45 +02:00
jsCore.apiCall("/user/login", {"username": username, "password": password, "stayLoggedIn": stayLoggedIn }, function(res) {
if (res.success) {
document.location.reload();
} else {
2022-11-30 16:42:24 +01:00
btn.text(L("account.sign_in"));
2020-07-02 00:47:45 +02:00
btn.prop("disabled", false);
$("#password").val("");
createdDiv.hide();
2024-04-07 18:29:33 +02:00
if (res.user.confirmed === false) {
2023-03-05 15:30:06 +01:00
showAlert("danger", res.msg + ' <a href="/resendConfirmEmail">Click here</a> to resend the confirmation mail.', true);
2021-11-11 14:25:26 +01:00
} else {
showAlert("danger", res.msg);
}
2020-07-02 00:47:45 +02:00
}
});
});
2020-07-01 22:13:50 +02:00
$("#btnRegister").click(function (e) {
e.preventDefault();
e.stopPropagation();
let btn = $(this);
let username = $("#username").val().trim();
let email = $("#email").val().trim();
let password = $("#password").val();
let confirmPassword = $("#confirmPassword").val();
if (username === '' || email === '' || password === '' || confirmPassword === '') {
showAlert("danger", "Please fill out every field.");
} else if(password !== confirmPassword) {
2022-11-30 16:42:24 +01:00
showAlert("danger", L("register.passwords_do_not_match"));
2020-07-01 22:13:50 +02:00
} else {
let params = { username: username, email: email, password: password, confirmPassword: confirmPassword };
2022-11-30 16:42:24 +01:00
if (jsCore.isRecaptchaEnabled()) {
2021-03-30 00:27:15 +02:00
let siteKey = $("#siteKey").val().trim();
2020-07-02 00:59:55 +02:00
grecaptcha.ready(function() {
grecaptcha.execute(siteKey, {action: 'register'}).then(function(captcha) {
params["captcha"] = captcha;
submitForm(btn, "user/register", params, () => {
showAlert("success", "Account successfully created, check your emails.");
2021-11-11 14:25:26 +01:00
$("input:not([id='siteKey'])").val("");
2020-07-02 00:59:55 +02:00
});
});
});
} else {
submitForm(btn, "user/register", params, () => {
2020-07-01 22:13:50 +02:00
showAlert("success", "Account successfully created, check your emails.");
2021-11-11 14:25:26 +01:00
$("input:not([id='siteKey'])").val("");
2020-07-02 00:59:55 +02:00
});
}
2020-07-01 22:13:50 +02:00
}
});
2020-07-02 00:47:45 +02:00
$("#btnAcceptInvite").click(function (e) {
e.preventDefault();
e.stopPropagation();
let btn = $(this);
let token = $("#token").val();
let password = $("#password").val();
let confirmPassword = $("#confirmPassword").val();
if(password !== confirmPassword) {
showAlert("danger", "Your passwords did not match.");
} else {
let textBefore = btn.text();
let params = { token: token, password: password, confirmPassword: confirmPassword };
btn.prop("disabled", true);
btn.html("Submitting… <i class='fas fa-spin fa-spinner'></i>")
jsCore.apiCall("user/acceptInvite", params, (res) => {
btn.prop("disabled", false);
btn.text(textBefore);
if (!res.success) {
showAlert("danger", res.msg);
} else {
$("input").val("");
2021-12-08 16:53:43 +01:00
document.location = "/login?success=" + encodeURIComponent("Account successfully created. You may now login.");
2020-07-02 00:47:45 +02:00
}
});
}
});
$("#btnRequestPasswordReset").click(function (e) {
e.preventDefault();
e.stopPropagation();
let btn = $(this);
let email = $("#email").val();
2020-07-02 00:59:55 +02:00
let params = { email: email };
2022-11-30 16:42:24 +01:00
if (jsCore.isRecaptchaEnabled()) {
2021-03-30 00:27:15 +02:00
let siteKey = $("#siteKey").val().trim();
2020-07-02 00:59:55 +02:00
grecaptcha.ready(function() {
grecaptcha.execute(siteKey, {action: 'resetPassword'}).then(function(captcha) {
params["captcha"] = captcha;
submitForm(btn, "user/requestPasswordReset", params, () => {
showAlert("success", "If the e-mail address exists and is linked to a account, you will receive a password reset token.");
2021-11-11 14:25:26 +01:00
$("input:not([id='siteKey'])").val("");
2020-07-02 00:59:55 +02:00
});
});
});
} else {
submitForm(btn, "user/requestPasswordReset", params, () => {
2020-07-02 00:47:45 +02:00
showAlert("success", "If the e-mail address exists and is linked to a account, you will receive a password reset token.");
2021-11-11 14:25:26 +01:00
$("input:not([id='siteKey'])").val("");
2020-07-02 00:59:55 +02:00
});
}
2020-07-02 00:47:45 +02:00
});
$("#btnResetPassword").click(function (e) {
e.preventDefault();
e.stopPropagation();
let btn = $(this);
let token = $("#token").val();
let password = $("#password").val();
let confirmPassword = $("#confirmPassword").val();
if(password !== confirmPassword) {
showAlert("danger", "Your passwords did not match.");
} else {
let textBefore = btn.text();
let params = { token: token, password: password, confirmPassword: confirmPassword };
btn.prop("disabled", true);
btn.html("Submitting… <i class='fas fa-spin fa-spinner'></i>")
jsCore.apiCall("user/resetPassword", params, (res) => {
btn.prop("disabled", false);
btn.text(textBefore);
if (!res.success) {
showAlert("danger", res.msg);
} else {
showAlert("success", "Your password was successfully changed. You may now login.");
2021-11-11 14:25:26 +01:00
$("input:not([id='siteKey'])").val("");
2021-12-08 16:53:43 +01:00
btn.hide();
$("#backToLogin").show();
2020-07-02 00:47:45 +02:00
}
});
}
});
2021-11-11 14:25:26 +01:00
$("#btnResendConfirmEmail").click(function(e) {
e.preventDefault();
e.stopPropagation();
let btn = $(this);
let email = $("#email").val();
let params = { email: email };
2022-11-30 16:42:24 +01:00
if (jsCore.isRecaptchaEnabled()) {
2021-11-11 14:25:26 +01:00
let siteKey = $("#siteKey").val().trim();
grecaptcha.ready(function() {
grecaptcha.execute(siteKey, {action: 'resendConfirmation'}).then(function(captcha) {
params["captcha"] = captcha;
submitForm(btn, "user/resendConfirmEmail", params, () => {
showAlert("success", "If the e-mail address exists and is linked to a account, you will receive a new confirmation email.");
$("input:not([id='siteKey'])").val("");
});
});
});
} else {
submitForm(btn, "user/resendConfirmEmail", params, () => {
showAlert("success", "\"If the e-mail address exists and is linked to a account, you will receive a new confirmation email.");
$("input:not([id='siteKey'])").val("");
});
}
});
2021-01-07 15:54:19 +01:00
});