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

Code before:
	let access_token: string | undefined, refresh_token: string | undefined;

	try {
		if ('email' in message && 'password' in message) {
			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);
		}

Review comment (suggestion):
Obsolete explicit type since `createDefaultAccountability` always returns an `Accountability` object
```suggestion
		const defaultAccountability = createDefaultAccountability(accountabilityOverrides);
```

Code after:
	let access_token: string | undefined, refresh_token: string | undefined;

	try {
		if ('email' in message && 'password' in message) {
			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;