2014-10-08 15:36:47 -04:00
|
|
|
"use strict";
|
2015-03-31 15:11:38 -04:00
|
|
|
/* global define, app, config, RELATIVE_PATH */
|
2014-10-08 15:36:47 -04:00
|
|
|
|
2015-03-31 15:11:38 -04:00
|
|
|
define('forum/login', ['csrf', 'translator'], function(csrf, translator) {
|
2014-10-08 15:36:47 -04:00
|
|
|
var Login = {};
|
|
|
|
|
|
|
|
|
|
Login.init = function() {
|
|
|
|
|
var errorEl = $('#login-error-notify'),
|
|
|
|
|
submitEl = $('#login'),
|
|
|
|
|
formEl = $('#login-form');
|
|
|
|
|
|
|
|
|
|
submitEl.on('click', function(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
if (!$('#username').val() || !$('#password').val()) {
|
2015-04-07 11:44:45 -04:00
|
|
|
errorEl.find('p').translateText('[[error:invalid-username-or-password]]');
|
|
|
|
|
errorEl.show();
|
2014-10-08 15:36:47 -04:00
|
|
|
} else {
|
|
|
|
|
errorEl.hide();
|
|
|
|
|
|
2015-04-07 11:44:45 -04:00
|
|
|
if (submitEl.hasClass('disabled')) {
|
|
|
|
|
return;
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
2015-04-07 11:44:45 -04:00
|
|
|
|
|
|
|
|
submitEl.addClass('disabled');
|
|
|
|
|
formEl.ajaxSubmit({
|
|
|
|
|
headers: {
|
|
|
|
|
'x-csrf-token': csrf.get()
|
|
|
|
|
},
|
|
|
|
|
success: function(data, status) {
|
2015-09-18 10:58:32 -04:00
|
|
|
window.location.href = data + '?loggedin';
|
2015-04-07 11:44:45 -04:00
|
|
|
},
|
|
|
|
|
error: function(data, status) {
|
2016-05-09 11:40:42 -04:00
|
|
|
if (data.status === 403 && data.statusText === 'Forbidden') {
|
|
|
|
|
window.location.href = config.relative_path + '/login?error=csrf-invalid';
|
|
|
|
|
} else {
|
|
|
|
|
errorEl.find('p').translateText(data.responseText);
|
|
|
|
|
errorEl.show();
|
|
|
|
|
submitEl.removeClass('disabled');
|
|
|
|
|
}
|
2015-04-07 11:44:45 -04:00
|
|
|
}
|
|
|
|
|
});
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$('#login-error-notify button').on('click', function(e) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
errorEl.hide();
|
2015-04-07 11:44:45 -04:00
|
|
|
return false;
|
2014-10-08 15:36:47 -04:00
|
|
|
});
|
|
|
|
|
|
2016-05-03 19:13:10 +03:00
|
|
|
if ($('#content #username').attr('readonly')) {
|
2016-05-10 18:34:41 +03:00
|
|
|
$('#content #password').val('').focus();
|
2016-05-03 19:13:10 +03:00
|
|
|
} else {
|
|
|
|
|
$('#content #username').focus();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-14 11:48:16 -04:00
|
|
|
|
|
|
|
|
// Add "returnTo" data if present
|
|
|
|
|
if (app.previousUrl) {
|
|
|
|
|
var returnToEl = document.createElement('input');
|
|
|
|
|
returnToEl.type = 'hidden';
|
|
|
|
|
returnToEl.name = 'returnTo';
|
2014-12-03 22:29:56 -05:00
|
|
|
returnToEl.value = app.previousUrl;
|
2014-10-14 11:48:16 -04:00
|
|
|
$(returnToEl).appendTo(formEl);
|
|
|
|
|
}
|
2014-10-08 15:36:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Login;
|
|
|
|
|
});
|