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
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
define('forum/login', ['translator'], function (translator) {
|
2014-10-08 15:36:47 -04:00
|
|
|
var Login = {};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Login.init = function () {
|
2014-10-08 15:36:47 -04:00
|
|
|
var errorEl = $('#login-error-notify'),
|
|
|
|
|
submitEl = $('#login'),
|
|
|
|
|
formEl = $('#login-form');
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
submitEl.on('click', function (e) {
|
2014-10-08 15:36:47 -04:00
|
|
|
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');
|
2016-11-11 16:47:59 -05:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
Set session refresh flag (otherwise the session check will trip and throw invalid session modal)
|
|
|
|
|
We know the session is/will be invalid (uid mismatch) because the user is attempting a login
|
|
|
|
|
*/
|
|
|
|
|
app.flags = app.flags || {};
|
|
|
|
|
app.flags._sessionRefresh = true;
|
|
|
|
|
|
2015-04-07 11:44:45 -04:00
|
|
|
formEl.ajaxSubmit({
|
|
|
|
|
headers: {
|
2017-02-17 19:31:21 -07:00
|
|
|
'x-csrf-token': config.csrf_token,
|
2015-04-07 11:44:45 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
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
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
error: function (data, status) {
|
2016-05-11 14:19:28 -04:00
|
|
|
if (data.status === 403 && data.responseText === 'Forbidden') {
|
2016-05-09 11:40:42 -04:00
|
|
|
window.location.href = config.relative_path + '/login?error=csrf-invalid';
|
|
|
|
|
} else {
|
|
|
|
|
errorEl.find('p').translateText(data.responseText);
|
|
|
|
|
errorEl.show();
|
|
|
|
|
submitEl.removeClass('disabled');
|
2016-11-11 16:47:59 -05:00
|
|
|
app.flags._sessionRefresh = false;
|
2016-08-02 17:48:28 -04:00
|
|
|
|
|
|
|
|
// Select the entire password if that field has focus
|
|
|
|
|
if ($('#password:focus').size()) {
|
|
|
|
|
$('#password').select();
|
|
|
|
|
}
|
2016-05-09 11:40:42 -04:00
|
|
|
}
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-04-07 11:44:45 -04:00
|
|
|
});
|
2014-10-08 15:36:47 -04:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
$('#login-error-notify button').on('click', function (e) {
|
2014-10-08 15:36:47 -04:00
|
|
|
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-08 15:36:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return Login;
|
|
|
|
|
});
|