Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 65 additions & 27 deletions htdocs/js/loris-scripts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
/* eslint new-cap: ["error", {capIsNewExceptions: ["DynamicTable", "FileUpload"]}]*/

/**
* Display the login modal when a request returns a 401 response.
*/
function handleUnauthorized() {
if (!window.$ || !window.loris) {
return;
}

if (!$('#login-modal').length) {
return;
}

if ($('#login-modal').hasClass('in')) {
$('#login-modal-error').show();
return;
}

$('#login-modal').modal('show');
$('#modal-login')
.off('click.lorisFetch')
.on('click.lorisFetch', function(e) {
e.preventDefault();
let data = {
username: $('#modal-username').val(),
password: $('#modal-password').val(),
login: 'Login',
};

window.lorisFetch(window.loris.BaseURL, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
body: new URLSearchParams(data),
})
.then((response) => {
if (!response.ok) {
throw new Error('request_failed');
}
$('#login-modal-error').hide();
$('#login-modal').modal('hide');
})
.catch(() => {
$('#login-modal-error').show();
});
});
}

if (!window.lorisFetch) {
window.lorisFetch = function(input, init) {
const options = Object.assign(
{
credentials: 'same-origin',
},
init || {}
);
return fetch(input, options).then((response) => {
if (response.status === 401) {
handleUnauthorized();
}
return response;
});
};
}

$(document).ready(function() {
$('#menu-toggle').click(function(e) {
e.preventDefault();
Expand All @@ -8,30 +73,3 @@ $(document).ready(function() {
$('.dynamictable').DynamicTable();
$('.fileUpload').FileUpload();
});

$(document).ajaxError(function(event, jqxhr, settings, thrownError) {
if (jqxhr.status === 401) {
if ($('#login-modal').hasClass('in')) {
$('#login-modal-error').show();
} else {
$('#login-modal').modal('show');
$('#modal-login').click(function(e) {
e.preventDefault();
let data = {
username: $('#modal-username').val(),
password: $('#modal-password').val(),
login: 'Login',
};
$.ajax({
type: 'post',
url: loris.BaseURL,
data: data,
success: function() {
$('#login-modal-error').hide();
$('#login-modal').modal('hide');
},
});
});
}
}
});
81 changes: 81 additions & 0 deletions jslib/lorisFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Display the login modal when a request returns a 401 response.
*/
function handleUnauthorized() {
if (typeof window === 'undefined') {
return;
}
if (!window.$ || !window.loris) {
return;
}

const $ = window.$;
if (!$('#login-modal').length) {
return;
}

if ($('#login-modal').hasClass('in')) {
$('#login-modal-error').show();
return;
}

$('#login-modal').modal('show');
$('#modal-login')
.off('click.lorisFetch')
.on('click.lorisFetch', function(e) {
e.preventDefault();
let data = {
username: $('#modal-username').val(),
password: $('#modal-password').val(),
login: 'Login',
};

fetch(window.loris.BaseURL, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
body: new URLSearchParams(data),
credentials: 'same-origin',
})
.then((response) => {
if (!response.ok) {
throw new Error('request_failed');
}
$('#login-modal-error').hide();
$('#login-modal').modal('hide');
})
.catch(() => {
$('#login-modal-error').show();
});
});
}

/**
* Wrapper around fetch that keeps credentials and handles 401s.
*
* @param {*} input
* @param {object=} init
* @return {Promise<Response>}
*/
function lorisFetch(input, init) {
const options = Object.assign(
{
credentials: 'same-origin',
},
init || {}
);

return fetch(input, options).then((response) => {
if (response.status === 401) {
handleUnauthorized();
}
return response;
});
}

if (typeof window !== 'undefined') {
window.lorisFetch = lorisFetch;
}

export default lorisFetch;
Loading