Add token verify in websocket request

Signed-off-by: YoungSoo Shin <shinys000114@gmail.com>
This commit is contained in:
2025-09-26 11:53:17 +09:00
parent 6be0512146
commit bfc2b17ed4
3 changed files with 59 additions and 11 deletions

View File

@@ -238,14 +238,17 @@ function initialize() {
// Always attach login form listener
loginForm.addEventListener('submit', handleLogin);
if (checkAuth()) { // Check authentication status
// If authenticated, initialize main content
initializeMainAppContent();
} else {
// If not authenticated, show login form
if (!checkAuth()) { // If NOT authenticated
// Show login form
loginContainer.style.setProperty('display', 'flex', 'important');
mainContent.style.setProperty('display', 'none', 'important');
console.log('Not authenticated. Login form displayed. Main app content NOT initialized.');
return; // IMPORTANT: Stop execution here if not authenticated
}
// If authenticated, initialize main content
console.log('Authenticated. Initializing main app content.');
initializeMainAppContent();
}
// --- Start Application ---

View File

@@ -9,7 +9,7 @@
export let websocket;
// The WebSocket server address, derived from the current page's host (hostname + port).
const gateway = `ws://${window.location.host}/ws`;
const baseGateway = `ws://${window.location.host}/ws`;
/**
* Initializes the WebSocket connection and sets up event handlers.
@@ -19,6 +19,13 @@ const gateway = `ws://${window.location.host}/ws`;
* @param {function} callbacks.onMessage - Called when a message is received from the server.
*/
export function initWebSocket({onOpen, onClose, onMessage}) {
const token = localStorage.getItem('authToken');
let gateway = baseGateway;
if (token) {
gateway = `${baseGateway}?token=${token}`;
}
console.log(`Trying to open a WebSocket connection to ${gateway}...`);
websocket = new WebSocket(gateway);
// Set binary type to arraybuffer to handle raw binary data from the UART.