Repository: directus/directus
File: api/src/websocket/controllers/base.ts (TypeScript)

Code before:

	protected parseMessage(data: string): WebSocketMessage {
		let message: WebSocketMessage;

		try {
			message = WebSocketMessage.parse(parseJSON(data));
		} catch {
			throw new WebSocketError('server', 'INVALID_PAYLOAD', 'Unable to parse the incoming message.');
		}

		return message;
	}

	protected async handleAuthRequest(client: WebSocketClient, message: WebSocketAuthMessage) {
		try {
			const { accountability, expires_at, refresh_token } = await authenticateConnection(message);
			this.checkUserRequirements(accountability);

			/**
			 * Re-use the existing ip, userAgent and origin accountability properties.
			 * They are only sent in the original connection request
			 */
			if (accountability && client.accountability) {
				accountability.ip = client.accountability.ip;
				accountability.userAgent = client.accountability.userAgent;
				accountability.origin = client.accountability.origin;
			}

			client.accountability = accountability;
			client.expires_at = expires_at;
			this.setTokenExpireTimer(client);
			emitter.emitAction('websocket.auth.success', { client });
			client.send(authenticationSuccess(message.uid, refresh_token));
			logger.trace(`WebSocket#${client.uid} authenticated as ${JSON.stringify(client.accountability)}`);
		} catch (error) {
			logger.trace(`WebSocket#${client.uid} failed authentication`);
			emitter.emitAction('websocket.auth.failure', { client });

			client.accountability = null;
			client.expires_at = null;

			const _error =
				error instanceof WebSocketError
					? error
					: new WebSocketError('auth', 'AUTH_FAILED', 'Authentication failed.', message.uid);

			handleWebSocketError(client, _error, 'auth');

			if (this.authentication.mode !== 'public') {
				client.close();
			}

Review comment (suggestion):
TIL: Apparently you can do this to make it a 1 liner, but I'd rather not. 😅
```suggestion
                ({ip: accountability.ip, userAgent: accountability.userAgent, origin: accountability.origin} = client.accountability);
```

Code after:
		try {
			message = WebSocketMessage.parse(parseJSON(data));
		} catch {
			throw new WebSocketError('server', 'INVALID_PAYLOAD', 'Unable to parse the incoming message.');
		}

		return message;
	}

	protected async handleAuthRequest(client: WebSocketClient, message: WebSocketAuthMessage) {
		try {
			const { accountability, expires_at, refresh_token } = await authenticateConnection(message);
			this.checkUserRequirements(accountability);

			/**
			 * Re-use the existing ip, userAgent and origin accountability properties.
			 * They are only sent in the original connection request
			 */
			if (accountability && client.accountability) {
				Object.assign(accountability, {
					ip: client.accountability.ip,
					userAgent: client.accountability.userAgent,
					origin: client.accountability.origin,
				});
			}

			client.accountability = accountability;
			client.expires_at = expires_at;
			this.setTokenExpireTimer(client);
			emitter.emitAction('websocket.auth.success', { client });
			client.send(authenticationSuccess(message.uid, refresh_token));
			logger.trace(`WebSocket#${client.uid} authenticated as ${JSON.stringify(client.accountability)}`);
		} catch (error) {
			logger.trace(`WebSocket#${client.uid} failed authentication`);
			emitter.emitAction('websocket.auth.failure', { client });

			client.accountability = null;
			client.expires_at = null;

			const _error =
				error instanceof WebSocketError
					? error
					: new WebSocketError('auth', 'AUTH_FAILED', 'Authentication failed.', message.uid);

			handleWebSocketError(client, _error, 'auth');

			if (this.authentication.mode !== 'public') {
				client.close();
			}
		}
	}