feat(webui): handle auth via websocket #2226

Merged
Sainan merged 4 commits from webui-auth-redux into main 2025-06-21 07:26:44 -07:00
Showing only changes of commit 21d46d3caa - Show all commits

View File

@ -108,6 +108,23 @@ interface IWsMsgFromClient {
}; };
} }
interface IWsMsgToClient {
ports?: {
http: number | undefined;
https: number | undefined;
};
config_reloaded?: boolean;
auth_succ?: {
id: string;
DisplayName: string;
Nonce: number;
};
auth_fail?: {
isRegister: boolean;
};
logged_out?: boolean;
}
const wsOnConnect = (ws: ws, _req: http.IncomingMessage): void => { const wsOnConnect = (ws: ws, _req: http.IncomingMessage): void => {
// eslint-disable-next-line @typescript-eslint/no-misused-promises // eslint-disable-next-line @typescript-eslint/no-misused-promises
ws.on("message", async msg => { ws.on("message", async msg => {
@ -144,7 +161,7 @@ const wsOnConnect = (ws: ws, _req: http.IncomingMessage): void => {
DisplayName: account.DisplayName, DisplayName: account.DisplayName,
Nonce: account.Nonce Nonce: account.Nonce
} }
}) } satisfies IWsMsgToClient)
); );
} else { } else {
ws.send( ws.send(
@ -152,14 +169,14 @@ const wsOnConnect = (ws: ws, _req: http.IncomingMessage): void => {
auth_fail: { auth_fail: {
isRegister: data.auth.isRegister isRegister: data.auth.isRegister
} }
}) } satisfies IWsMsgToClient)
); );
} }
} }
}); });
}; };
export const sendWsBroadcast = <T>(data: T): void => { export const sendWsBroadcast = (data: IWsMsgToClient): void => {
const msg = JSON.stringify(data); const msg = JSON.stringify(data);
if (wsServer) { if (wsServer) {
for (const client of wsServer.clients) { for (const client of wsServer.clients) {
@ -173,7 +190,7 @@ export const sendWsBroadcast = <T>(data: T): void => {
} }
}; };
export const sendWsBroadcastTo = <T>(accountId: string, data: T): void => { export const sendWsBroadcastTo = (accountId: string, data: IWsMsgToClient): void => {
const msg = JSON.stringify(data); const msg = JSON.stringify(data);
if (wsServer) { if (wsServer) {
for (const client of wsServer.clients) { for (const client of wsServer.clients) {