feat(webui): handle auth via websocket #2226
@ -97,7 +97,7 @@ export const stopWebServer = async (): Promise<void> => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
interface IWsCustomData extends ws {
|
interface IWsCustomData extends ws {
|
||||||
accountId: string;
|
accountId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IWsMsgFromClient {
|
interface IWsMsgFromClient {
|
||||||
@ -106,6 +106,7 @@ interface IWsMsgFromClient {
|
|||||||
password: string;
|
password: string;
|
||||||
isRegister: boolean;
|
isRegister: boolean;
|
||||||
};
|
};
|
||||||
|
logout?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IWsMsgToClient {
|
interface IWsMsgToClient {
|
||||||
@ -173,6 +174,9 @@ const wsOnConnect = (ws: ws, _req: http.IncomingMessage): void => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (data.logout) {
|
||||||
|
(ws as IWsCustomData).accountId = undefined;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -37,7 +37,7 @@
|
|||||||
<li class="nav-item dropdown user-dropdown">
|
<li class="nav-item dropdown user-dropdown">
|
||||||
<button class="nav-link dropdown-toggle displayname" data-bs-toggle="dropdown" aria-expanded="false"></button>
|
<button class="nav-link dropdown-toggle displayname" data-bs-toggle="dropdown" aria-expanded="false"></button>
|
||||||
<ul class="dropdown-menu dropdown-menu-end">
|
<ul class="dropdown-menu dropdown-menu-end">
|
||||||
<li><a class="dropdown-item" href="/webui/" onclick="logout();" data-loc="navbar_logout"></a></li>
|
<li><a class="dropdown-item" href="/webui/" onclick="doLogout();" data-loc="navbar_logout"></a></li>
|
||||||
<li><hr class="dropdown-divider"></li>
|
<li><hr class="dropdown-divider"></li>
|
||||||
<li><a class="dropdown-item" href="#" onclick="event.preventDefault();renameAccount();" data-loc="navbar_renameAccount"></a></li>
|
<li><a class="dropdown-item" href="#" onclick="event.preventDefault();renameAccount();" data-loc="navbar_renameAccount"></a></li>
|
||||||
<li><a class="dropdown-item" href="#" onclick="event.preventDefault();deleteAccount();" data-loc="navbar_deleteAccount"></a></li>
|
<li><a class="dropdown-item" href="#" onclick="event.preventDefault();deleteAccount();" data-loc="navbar_deleteAccount"></a></li>
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
let auth_pending = false,
|
let auth_pending = false,
|
||||||
did_initial_auth = false;
|
did_initial_auth = false;
|
||||||
const sendAuth = isRegister => {
|
const sendAuth = isRegister => {
|
||||||
|
if (localStorage.getItem("email") && localStorage.getItem("password")) {
|
||||||
auth_pending = true;
|
auth_pending = true;
|
||||||
window.ws.send(
|
window.ws.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
@ -21,14 +22,13 @@ const sendAuth = isRegister => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function openWebSocket() {
|
function openWebSocket() {
|
||||||
window.ws = new WebSocket("/custom/ws");
|
window.ws = new WebSocket("/custom/ws");
|
||||||
window.ws.onopen = () => {
|
window.ws.onopen = () => {
|
||||||
if (localStorage.getItem("email") && localStorage.getItem("password")) {
|
|
||||||
sendAuth(false);
|
sendAuth(false);
|
||||||
}
|
|
||||||
};
|
};
|
||||||
window.ws.onmessage = e => {
|
window.ws.onmessage = e => {
|
||||||
const msg = JSON.parse(e.data);
|
const msg = JSON.parse(e.data);
|
||||||
@ -115,6 +115,14 @@ function logout() {
|
|||||||
did_initial_auth = false;
|
did_initial_auth = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function doLogout() {
|
||||||
|
logout();
|
||||||
|
if (window.ws) {
|
||||||
|
// Unsubscribe from notifications about nonce invalidation
|
||||||
|
window.ws.send(JSON.stringify({ logout: true }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function renameAccount() {
|
function renameAccount() {
|
||||||
const newname = window.prompt(loc("code_changeNameConfirm"));
|
const newname = window.prompt(loc("code_changeNameConfirm"));
|
||||||
if (newname) {
|
if (newname) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user