Skip to content

Commit

Permalink
fix: Fix when not registered such user (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikuroXina authored Aug 21, 2023
1 parent 0e62589 commit 5b9c458
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { cors } from "hono/cors";

import { newRepo, withDiscordRepository } from "./adaptors/discord";
import { R2Store } from "./adaptors/r2";
import { APPROVERS_GUILD_ID } from "./consts";
import { AssociatedLinksSchema, checkAppError, type Member } from "./models";
import { Index } from "./pages";
import { Done } from "./pages/done";
Expand Down Expand Up @@ -120,17 +119,24 @@ app.use("/members/:id/associations", async (c, next) => {
if (!auth || !auth.startsWith("Bearer ")) {
return c.text("Unauthorized", 401);
}
const token = auth.substring(6).trim();
const member = await newRepo(token).guildMember(APPROVERS_GUILD_ID);
if (!member || member.roles.length === 0) {
return c.text("Not Found", 404);
}
const token = auth.substring("Bearer ".length).trim();

c.set("oauthToken", token);

const id = c.req.param("id");
const entry = await c.env.ASSOC_BUCKET.get(id);
let entry = await c.env.ASSOC_BUCKET.get(id);
if (!entry) {
const repo = newRepo(token);
const store = new R2Store(c.env.ASSOC_BUCKET);
const result = await patchMembers(repo, store);
if (Result.isErr(result)) {
return c.text("Forbidden", 403);
}
entry = await c.env.ASSOC_BUCKET.get(id);
}
if (!entry) {
return c.text("Not Found", 404);
console.error("insert new member failure");
return c.text("Internal Server Error", 500);
}
const stored = await entry.json<Member>();
c.set("member", stored);
Expand Down

0 comments on commit 5b9c458

Please sign in to comment.