Skip to content

Commit 2b7576d

Browse files
Set proper User-Agent on native iOS/Android API requests
Native gRPC-web API calls previously went out with the generic CFNetwork/Darwin (iOS) user agent. Add a metadata interceptor that sets a recognisable CouchersNative User-Agent on every request. Co-authored-by: Aapeli <aapeliv@users.noreply.github.com>
1 parent 697c464 commit 2b7576d

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

app/mobile/service/client.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { AuthPromiseClient } from "@/proto/auth_grpc_web_pb";
33
import { NotificationsPromiseClient } from "@/proto/notifications_grpc_web_pb";
44

55
import isGrpcError from "@/service/utils/isGrpcError";
6+
import { applicationNameForUserAgent } from "@/utils/userAgent";
67

78
const URL =
89
process.env.NEXT_PUBLIC_API_BASE_URL ||
@@ -60,11 +61,24 @@ class TimeoutInterceptor {
6061
}
6162
}
6263

64+
// Sets a recognisable User-Agent on native API requests. Without this, requests
65+
// go out with the generic CFNetwork/Darwin (iOS) or okhttp (Android) UA.
66+
class UserAgentInterceptor {
67+
async intercept(
68+
request: Request<unknown, unknown>,
69+
invoker: (request: unknown) => unknown,
70+
) {
71+
request.getMetadata()["User-Agent"] = applicationNameForUserAgent;
72+
return invoker(request);
73+
}
74+
}
75+
6376
const authInterceptor = new AuthInterceptor();
6477
const timeoutInterceptor = new TimeoutInterceptor();
78+
const userAgentInterceptor = new UserAgentInterceptor();
6579

6680
const opts = {
67-
unaryInterceptors: [authInterceptor, timeoutInterceptor],
81+
unaryInterceptors: [userAgentInterceptor, authInterceptor, timeoutInterceptor],
6882
// this modifies the behaviour on the API so that it will send cookies on the requests
6983
withCredentials: true,
7084
/// TODO: streaming interceptor for auth https://grpc.io/blog/grpc-web-interceptor/

0 commit comments

Comments
 (0)