From 72239b1fc79d946e9fe56b13f9bc6086b47287c0 Mon Sep 17 00:00:00 2001 From: horsefacts <109845214+horsefacts@users.noreply.github.com> Date: Sun, 3 Mar 2024 17:06:07 -0700 Subject: [PATCH] chore: return response in async handlers (#149) --- .changeset/slow-laws-tap.md | 5 +++++ apps/relay/src/handlers.ts | 38 ++++++++++++++++++++--------------- apps/relay/src/server.test.ts | 6 +++++- apps/relay/src/server.ts | 3 +-- 4 files changed, 33 insertions(+), 19 deletions(-) create mode 100644 .changeset/slow-laws-tap.md diff --git a/.changeset/slow-laws-tap.md b/.changeset/slow-laws-tap.md new file mode 100644 index 0000000..b872ad9 --- /dev/null +++ b/.changeset/slow-laws-tap.md @@ -0,0 +1,5 @@ +--- +"@farcaster/auth-relay": patch +--- + +chore: return response from async handler diff --git a/apps/relay/src/handlers.ts b/apps/relay/src/handlers.ts index 44a8713..8081638 100644 --- a/apps/relay/src/handlers.ts +++ b/apps/relay/src/handlers.ts @@ -58,18 +58,20 @@ export async function createChannel(request: FastifyRequest<{ Body: CreateChanne connectUri: url, }); if (update.isOk()) { - reply.code(201).send({ channelToken, url, connectUri: url, nonce }); + return reply.code(201).send({ channelToken, url, connectUri: url, nonce }); } else { - reply.code(500).send({ error: update.error.message }); + return reply.code(500).send({ error: update.error.message }); } } else { - reply.code(500).send({ error: channel.error.message }); + return reply.code(500).send({ error: channel.error.message }); } } export async function authenticate(request: FastifyRequest<{ Body: AuthenticateRequest }>, reply: FastifyReply) { const authKey = request.headers["x-farcaster-auth-relay-key"] || request.headers["x-farcaster-connect-auth-key"]; - if (authKey !== AUTH_KEY) reply.code(401).send({ error: "Unauthorized" }); + if (authKey !== AUTH_KEY) { + return reply.code(401).send({ error: "Unauthorized" }); + } const channelToken = request.channelToken; const { message, signature, fid, username, displayName, bio, pfpUrl } = request.body; @@ -91,16 +93,18 @@ export async function authenticate(request: FastifyRequest<{ Body: AuthenticateR ...addrs.value, }); if (update.isOk()) { - reply.code(201).send(update.value); + return reply.code(201).send(update.value); } else { - reply.code(500).send({ error: update.error.message }); + return reply.code(500).send({ error: update.error.message }); } } else { - if (channel.error.errCode === "not_found") reply.code(401).send({ error: "Unauthorized " }); - reply.code(500).send({ error: channel.error.message }); + if (channel.error.errCode === "not_found") { + return reply.code(401).send({ error: "Unauthorized " }); + } + return reply.code(500).send({ error: channel.error.message }); } } else { - reply.code(500).send({ error: addrs.error.message }); + return reply.code(500).send({ error: addrs.error.message }); } } @@ -111,27 +115,29 @@ export async function status(request: FastifyRequest, reply: FastifyReply) { if (res.state === "completed") { const close = await request.channels.close(request.channelToken); if (close.isErr()) { - reply.code(500).send({ error: close.error.message }); + return reply.code(500).send({ error: close.error.message }); } - reply.code(200).send(res); + return reply.code(200).send(res); } else { - reply.code(202).send(res); + return reply.code(202).send(res); } } else { - if (channel.error.errCode === "not_found") reply.code(401).send({ error: "Unauthorized" }); - reply.code(500).send({ error: channel.error.message }); + if (channel.error.errCode === "not_found") { + return reply.code(401).send({ error: "Unauthorized" }); + } + return reply.code(500).send({ error: channel.error.message }); } } export async function handleError(error: FastifyError, request: FastifyRequest, reply: FastifyReply) { const { validation, statusCode } = error; if (validation) { - reply.status(400).send({ error: "Validation error", message: error.message }); + return reply.status(400).send({ error: "Validation error", message: error.message }); } else if (statusCode) { reply.code(statusCode); if (statusCode < 500) reply.send({ error: error.message }); } else { request.log.error({ err: error, errMsg: error.message, request }, "Error in http request"); - reply.code(500).send({ error: error.message }); + return reply.code(500).send({ error: error.message }); } } diff --git a/apps/relay/src/server.test.ts b/apps/relay/src/server.test.ts index 662f74e..9bef4f8 100644 --- a/apps/relay/src/server.test.ts +++ b/apps/relay/src/server.test.ts @@ -225,13 +225,17 @@ describe("relay server", () => { }); test("POST with invalid token", async () => { - const response = await http.post(getFullUrl("/v1/channel/authenticate"), authenticateParams, { + let response = await http.post(getFullUrl("/v1/channel/authenticate"), authenticateParams, { headers: { Authorization: "Bearer abc-123-def", "X-Farcaster-Auth-Relay-Key": "some-shared-secret", }, }); expect(response.status).toBe(401); + response = await http.get(getFullUrl("/v1/channel/status"), { + headers: { Authorization: `Bearer ${channelToken}` }, + }); + expect(response.data.state).toBe("pending"); }); test("POST with invalid key", async () => { diff --git a/apps/relay/src/server.ts b/apps/relay/src/server.ts index 151804d..efa6c4c 100644 --- a/apps/relay/src/server.ts +++ b/apps/relay/src/server.ts @@ -80,8 +80,7 @@ export class RelayServer { if (channelToken) { request.channelToken = channelToken; } else { - reply.code(401).send({ error: "Unauthorized " }); - return; + return reply.code(401).send({ error: "Unauthorized " }); } });