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

Code before:
			const authenticationService = new AuthenticationService({ schema: await getSchema() });
			const { accessToken, refreshToken } = await authenticationService.login(DEFAULT_AUTH_PROVIDER, message);
			access_token = accessToken;
			refresh_token = refreshToken;
		}

		if ('refresh_token' in message) {
			const authenticationService = new AuthenticationService({ schema: await getSchema() });
			const { accessToken, refreshToken } = await authenticationService.refresh(message.refresh_token);
			access_token = accessToken;
			refresh_token = refreshToken;
		}

		if ('access_token' in message) {
			access_token = message.access_token;
		}

		if (!access_token) throw new Error();
		const expires_at = getExpiresAtForToken(access_token);

		const database = getDatabase();
		const defaultAccountability: Accountability = createDefaultAccountability(accountabilityOverrides);

		const authenticationState = {
			accountability: defaultAccountability,
			expires_at,
			refresh_token,
		} as AuthenticationState;

		const customAccountability = await emitter.emitFilter(
			'websocket.authenticate',
			defaultAccountability,
			{
				message,
			},
			{
				database,
				schema: null,
				accountability: null,
			},
		);

		if (customAccountability && isEqual(customAccountability, defaultAccountability) === false) {
			authenticationState.accountability = customAccountability;
		} else {
			authenticationState.accountability = await getAccountabilityForToken(access_token, defaultAccountability);
		}

		return authenticationState;
	} catch {
		throw new WebSocketError('auth', 'AUTH_FAILED', 'Authentication failed.', message['uid']);

Review comment (suggestion):
No need to have `expires_at` as separate variable since its only used here.
```suggestion
			expires_at: getExpiresAtForToken(access_token),
```

Code after:
			const authenticationService = new AuthenticationService({ schema: await getSchema() });
			const { accessToken, refreshToken } = await authenticationService.login(DEFAULT_AUTH_PROVIDER, message);
			access_token = accessToken;
			refresh_token = refreshToken;
		}

		if ('refresh_token' in message) {
			const authenticationService = new AuthenticationService({ schema: await getSchema() });
			const { accessToken, refreshToken } = await authenticationService.refresh(message.refresh_token);
			access_token = accessToken;
			refresh_token = refreshToken;
		}

		if ('access_token' in message) {
			access_token = message.access_token;
		}

		if (!access_token) throw new Error();

		const defaultAccountability = createDefaultAccountability(accountabilityOverrides);

		const authenticationState = {
			accountability: defaultAccountability,
			expires_at: getExpiresAtForToken(access_token),
			refresh_token,
		} as AuthenticationState;

		const customAccountability = await emitter.emitFilter(
			'websocket.authenticate',
			defaultAccountability,
			{
				message,
			},
			{
				database: getDatabase(),
				schema: null,
				accountability: null,
			},
		);

		if (customAccountability && isEqual(customAccountability, defaultAccountability) === false) {
			authenticationState.accountability = customAccountability;
		} else {
			authenticationState.accountability = await getAccountabilityForToken(access_token, defaultAccountability);
		}

		return authenticationState;
	} catch {
		throw new WebSocketError('auth', 'AUTH_FAILED', 'Authentication failed.', message['uid']);
	}
}