require('../scss/style.scss');
require('../scss/login.scss');
require('./utils/logger');
const $ = require('jquery');
const ui = require('./ui/common');
const modal = require('./utils/modal');
const utils = require('./utils');
const api = require('./utils/api');
const query_string = require('query-string');
const cookies = require('./utils/cookies');
const email = $("#email").on("keydown", function(event) {
if (event.key === "Enter") {
post_login();
}
});
const password = $("#password").on("keydown", function(event) {
if (event.key === "Enter") {
post_login();
}
});
$("#login_button").on("click", post_login);
let qs = query_string.parse(location.search);
logger.log(qs);
if (qs.email) {
$("#email").val(qs.email);
}
if (cookies.get('cookie_consent')) {
logger.log("Cookies accepted!");
// Keep pushing the expires date back since we
// can't actually keep cookies stored "forever"
consent_to_cookies();
let login_message = cookies.get('login_message');
if (login_message) {
logger.log("login_message", login_message);
ui.show_banner(login_message);
cookies.remove('login_message');
}
} else {
let {
close
} = modal($(
`<div class="cookie-consent">
<div class="field-header">Cookie Consent</div>
This website uses functional cookies<br>
only and won't work without them.<br>
I don't track you and never will
</div>`
).append(
$(`<button class="accept-cookies">Sure, Store Cookies</button>`).on("click", function() {
consent_to_cookies();
close();
})
));
}
function consent_to_cookies() {
cookies.set('cookie_consent', true, {
expires: 10000
});
}
function post_login() {
utils.validate.keys({
email: email.val().trim(),
password: password.val()
}, [
["email", "Email cannot be empty"],
["password", "Password cannot be empty"]
]).then(function(data) {
return api.post("/account/login", data, {
add_captcha_token: {}
});
}).then(function() {
let pathname = "/";
if (qs.watchroom) {
pathname += "?" + query_string.stringify({
watchroom: qs.watchroom
});
}
window.location = pathname;
}).catch(api.handle_error_message);
}
$(".forgot-password-link").on("click", function() {
$(this).hide();
$("#email").off("keydown").on("keydown", function(event) {
if (event.key === "Enter") {
post_password_reset();
}
});
$("#password").off("keydown").parent().hide();
$("#login_button").text("Send Password Reset Email").css({
width: "Calc(100% - 20px)",
margin: "10px"
}).off("click").on("click", post_password_reset);
});
function post_password_reset() {
utils.validate.keys({
email: email.val().trim()
}, [
["email", "Email cannot be empty"]
]).then(function(data) {
return api.post("/account/password-reset/request", data, {
add_captcha_token: {}
})
}).then(function(data) {
logger.log(data);
ui.show_banner(data);
}).catch(api.handle_error_message)
}