Skip to content

hotfix: cannot authorize #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/zod/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const DaySchema = z.enum([
"other",
]);

export const PeriodSchema = z.number().min(0).max(6);
export const PeriodSchema = z.coerce.number().min(0).max(6);

export const SlotSchema = z.object({
day: DaySchema,
Expand Down
2 changes: 1 addition & 1 deletion server/src/firebase/auth/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type DecodedIdToken = admin.DecodedIdToken;
// REQUIRE: cookieParser middleware before this
// THROWS: if idToken is not present in request cookie, or when the token is not valid.
export async function getGUID(c: Context): Promise<GUID> {
const idToken = c.req.query("token");
const idToken = c.req.header("Authorization");
if (typeof idToken !== "string") error("token not found in query", 401);
return await getGUIDFromToken(idToken);
}
Expand Down
24 changes: 20 additions & 4 deletions test/server.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ test("/users/exists", async () => {
test("basic auth", async () => {
let res = await GET("/users/me");
expect(res.status).toBe(401);
res = await GET(`/users/me?token=${MOCK_TOKEN}`);
res = await GET("/users/me", {
headers: {
Authorization: MOCK_TOKEN,
},
});
expect(res.status).toBe(200);
const json = await res.json();
expect(json.name).toBe("田中太郎");
Expand All @@ -42,15 +46,27 @@ test("send request", async () => {
res = await PUT("/requests/send/102");
expect(res.status).toBe(401);

res = await GET(`/users/pending/from-me?token=${MOCK_TOKEN}`);
res = await GET("/users/pending/from-me", {
headers: {
Authorization: MOCK_TOKEN,
},
});
expect(res.status).toBe(200);
expect(await res.json()).toSatisfy((s) => s.length === 0);
// starting actual request

res = await PUT(`/requests/send/102?token=${MOCK_TOKEN}`);
res = await PUT("/requests/send/102", {
headers: {
Authorization: MOCK_TOKEN,
},
});
expect(res.status).toBe(201);

res = await GET(`/users/pending/from-me?token=${MOCK_TOKEN}`);
res = await GET("/users/pending/from-me", {
headers: {
Authorization: MOCK_TOKEN,
},
});
expect(await res.json()).toSatisfy(
(s) => s.length === 1 && s[0].name === "山田花子",
);
Expand Down
3 changes: 2 additions & 1 deletion web/api/internal/fetch-func.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ export async function uploadImage(path: string, file: File): Promise<URL> {
if (file.size >= MAX_IMAGE_SIZE) {
throw new Error("画像のアップロードに失敗しました: 画像が大きすぎます");
}
const res = await fetch(`${path}?token=${await getIdToken()}`, {
const res = await fetch(path, {
method: "POST",
headers: {
"Content-Type": "image/png",
Authorization: await getIdToken(),
},
body: file,
});
Expand Down
16 changes: 7 additions & 9 deletions web/firebase/auth/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,18 @@ export async function credFetch(
path: string,
body?: unknown,
): Promise<Response> {
let idToken = await getIdToken();
const idToken = await getIdToken();
const init: RequestInit = { method };
if (body) {
init.body = JSON.stringify(body);
init.headers = {
"Content-Type": "application/json",
Authorization: idToken,
};
} else {
init.headers = {
Authorization: idToken,
};
}
let res = await fetch(`${path}?token=${idToken}`, init);

if (res.status === 401) {
idToken = await getIdToken();
res = await fetch(`${path}?token=${idToken}`, init);
}

return res;
return await fetch(path, init);
}