Skip to content

Commit 3f8365a

Browse files
committed
Add logout handler and disable modal dragging
1 parent 8bd0d7c commit 3f8365a

1 file changed

Lines changed: 106 additions & 101 deletions

File tree

src/gui/src/initgui.js

Lines changed: 106 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,9 @@ window.initgui = async function (options) {
644644
is_verified = await UIWindowEmailConfirmationRequired({
645645
stay_on_top: true,
646646
has_head: false,
647+
window_options: {
648+
is_draggable: false,
649+
},
647650
});
648651
}
649652
while ( !is_verified );
@@ -683,6 +686,108 @@ window.initgui = async function (options) {
683686
}
684687
};
685688

689+
/**
690+
* Event handler for a custom 'logout' event attached to the document.
691+
* This function handles the process of logging out, including user confirmation,
692+
* communication with the backend, and subsequent UI updates. It takes special
693+
* precautions if the user is identified as using a temporary account.
694+
*
695+
* @listens Document#event:logout
696+
* @async
697+
* @param {Event} event - The JQuery event object associated with the logout event.
698+
* @returns {Promise<void>} - This function does not return anything meaningful, but it performs an asynchronous operation.
699+
*/
700+
$(document).on('logout', async function (event) {
701+
// is temp user?
702+
if ( window.user && window.user.is_temp && !window.user.deleted ) {
703+
const alert_resp = await UIAlert({
704+
message: '<strong>Save account before logging out!</strong><p>You are using a temporary account and logging out will erase all your data.</p>',
705+
buttons: [
706+
{
707+
label: i18n('save_account'),
708+
value: 'save_account',
709+
type: 'primary',
710+
},
711+
{
712+
label: i18n('log_out'),
713+
value: 'log_out',
714+
type: 'danger',
715+
},
716+
{
717+
label: i18n('cancel'),
718+
},
719+
],
720+
});
721+
if ( alert_resp === 'save_account' ) {
722+
let saved = await UIWindowSaveAccount({
723+
send_confirmation_code: false,
724+
default_username: window.user.username,
725+
});
726+
if ( saved )
727+
{
728+
window.logout();
729+
}
730+
} else if ( alert_resp === 'log_out' ) {
731+
window.logout();
732+
}
733+
else {
734+
return;
735+
}
736+
}
737+
738+
// logout
739+
try {
740+
const resp = await fetch(`${window.gui_origin}/get-anticsrf-token`);
741+
const { token } = await resp.json();
742+
await $.ajax({
743+
url: `${window.gui_origin }/logout`,
744+
type: 'POST',
745+
async: true,
746+
contentType: 'application/json',
747+
headers: {
748+
'Authorization': `Bearer ${ window.auth_token}`,
749+
},
750+
data: JSON.stringify({ anti_csrf: token }),
751+
statusCode: {
752+
401: function () {
753+
},
754+
},
755+
});
756+
} catch (e) {
757+
// Ignored
758+
}
759+
760+
// remove this user from the array of logged_in_users
761+
for ( let i = 0; i < window.logged_in_users.length; i++ ) {
762+
if ( window.logged_in_users[i].uuid === window.user.uuid ) {
763+
window.logged_in_users.splice(i, 1);
764+
break;
765+
}
766+
}
767+
768+
// update logged_in_users in local storage
769+
localStorage.setItem('logged_in_users', JSON.stringify(window.logged_in_users));
770+
771+
// delete this user from local storage
772+
window.user = null;
773+
localStorage.removeItem('user');
774+
window.auth_token = null;
775+
localStorage.removeItem('auth_token');
776+
777+
// close all windows
778+
$('.window').close();
779+
// close all ctxmenus
780+
$('.context-menu').remove();
781+
// remove desktop
782+
$('.desktop').remove();
783+
// remove taskbar
784+
$('.taskbar').remove();
785+
// disable native browser exit confirmation
786+
window.onbeforeunload = null;
787+
// go to home page
788+
window.location.replace('/');
789+
});
790+
686791
// -------------------------------------------------------------------------------------
687792
// Authed
688793
// -------------------------------------------------------------------------------------
@@ -709,6 +814,7 @@ window.initgui = async function (options) {
709814
has_head: false,
710815
logout_in_footer: true,
711816
window_options: {
817+
is_draggable: false,
712818
cover_page: window.is_embedded,
713819
},
714820
});
@@ -1685,107 +1791,6 @@ window.initgui = async function (options) {
16851791
}
16861792
});
16871793

1688-
/**
1689-
* Event handler for a custom 'logout' event attached to the document.
1690-
* This function handles the process of logging out, including user confirmation,
1691-
* communication with the backend, and subsequent UI updates. It takes special
1692-
* precautions if the user is identified as using a temporary account.
1693-
*
1694-
* @listens Document#event:logout
1695-
* @async
1696-
* @param {Event} event - The JQuery event object associated with the logout event.
1697-
* @returns {Promise<void>} - This function does not return anything meaningful, but it performs an asynchronous operation.
1698-
*/
1699-
$(document).on('logout', async function (event) {
1700-
// is temp user?
1701-
if ( window.user && window.user.is_temp && !window.user.deleted ) {
1702-
const alert_resp = await UIAlert({
1703-
message: '<strong>Save account before logging out!</strong><p>You are using a temporary account and logging out will erase all your data.</p>',
1704-
buttons: [
1705-
{
1706-
label: i18n('save_account'),
1707-
value: 'save_account',
1708-
type: 'primary',
1709-
},
1710-
{
1711-
label: i18n('log_out'),
1712-
value: 'log_out',
1713-
type: 'danger',
1714-
},
1715-
{
1716-
label: i18n('cancel'),
1717-
},
1718-
],
1719-
});
1720-
if ( alert_resp === 'save_account' ) {
1721-
let saved = await UIWindowSaveAccount({
1722-
send_confirmation_code: false,
1723-
default_username: window.user.username,
1724-
});
1725-
if ( saved )
1726-
{
1727-
window.logout();
1728-
}
1729-
} else if ( alert_resp === 'log_out' ) {
1730-
window.logout();
1731-
}
1732-
else {
1733-
return;
1734-
}
1735-
}
1736-
1737-
// logout
1738-
try {
1739-
const resp = await fetch(`${window.gui_origin}/get-anticsrf-token`);
1740-
const { token } = await resp.json();
1741-
await $.ajax({
1742-
url: `${window.gui_origin }/logout`,
1743-
type: 'POST',
1744-
async: true,
1745-
contentType: 'application/json',
1746-
headers: {
1747-
'Authorization': `Bearer ${ window.auth_token}`,
1748-
},
1749-
data: JSON.stringify({ anti_csrf: token }),
1750-
statusCode: {
1751-
401: function () {
1752-
},
1753-
},
1754-
});
1755-
} catch (e) {
1756-
// Ignored
1757-
}
1758-
1759-
// remove this user from the array of logged_in_users
1760-
for ( let i = 0; i < window.logged_in_users.length; i++ ) {
1761-
if ( window.logged_in_users[i].uuid === window.user.uuid ) {
1762-
window.logged_in_users.splice(i, 1);
1763-
break;
1764-
}
1765-
}
1766-
1767-
// update logged_in_users in local storage
1768-
localStorage.setItem('logged_in_users', JSON.stringify(window.logged_in_users));
1769-
1770-
// delete this user from local storage
1771-
window.user = null;
1772-
localStorage.removeItem('user');
1773-
window.auth_token = null;
1774-
localStorage.removeItem('auth_token');
1775-
1776-
// close all windows
1777-
$('.window').close();
1778-
// close all ctxmenus
1779-
$('.context-menu').remove();
1780-
// remove desktop
1781-
$('.desktop').remove();
1782-
// remove taskbar
1783-
$('.taskbar').remove();
1784-
// disable native browser exit confirmation
1785-
window.onbeforeunload = null;
1786-
// go to home page
1787-
window.location.replace('/');
1788-
});
17891794
};
17901795

17911796
function requestOpenerOrigin () {

0 commit comments

Comments
 (0)