Skip to content

Commit

Permalink
chore: return response in async handlers (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
horsefacts committed Mar 4, 2024
1 parent dd66937 commit 72239b1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/slow-laws-tap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farcaster/auth-relay": patch
---

chore: return response from async handler
38 changes: 22 additions & 16 deletions apps/relay/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 });
}
}

Expand All @@ -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 });
}
}
6 changes: 5 additions & 1 deletion apps/relay/src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
3 changes: 1 addition & 2 deletions apps/relay/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 " });
}
});

Expand Down

0 comments on commit 72239b1

Please sign in to comment.