Skip to content

Commit df61e4e

Browse files
authored
hotfix: cannot authorize (#662)
1 parent f6158f6 commit df61e4e

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

common/zod/schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const DaySchema = z.enum([
7878
"other",
7979
]);
8080

81-
export const PeriodSchema = z.number().min(0).max(6);
81+
export const PeriodSchema = z.coerce.number().min(0).max(6);
8282

8383
export const SlotSchema = z.object({
8484
day: DaySchema,

server/src/firebase/auth/lib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type DecodedIdToken = admin.DecodedIdToken;
1010
// REQUIRE: cookieParser middleware before this
1111
// THROWS: if idToken is not present in request cookie, or when the token is not valid.
1212
export async function getGUID(c: Context): Promise<GUID> {
13-
const idToken = c.req.query("token");
13+
const idToken = c.req.header("Authorization");
1414
if (typeof idToken !== "string") error("token not found in query", 401);
1515
return await getGUIDFromToken(idToken);
1616
}

test/server.spec.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ test("/users/exists", async () => {
2929
test("basic auth", async () => {
3030
let res = await GET("/users/me");
3131
expect(res.status).toBe(401);
32-
res = await GET(`/users/me?token=${MOCK_TOKEN}`);
32+
res = await GET("/users/me", {
33+
headers: {
34+
Authorization: MOCK_TOKEN,
35+
},
36+
});
3337
expect(res.status).toBe(200);
3438
const json = await res.json();
3539
expect(json.name).toBe("田中太郎");
@@ -42,15 +46,27 @@ test("send request", async () => {
4246
res = await PUT("/requests/send/102");
4347
expect(res.status).toBe(401);
4448

45-
res = await GET(`/users/pending/from-me?token=${MOCK_TOKEN}`);
49+
res = await GET("/users/pending/from-me", {
50+
headers: {
51+
Authorization: MOCK_TOKEN,
52+
},
53+
});
4654
expect(res.status).toBe(200);
4755
expect(await res.json()).toSatisfy((s) => s.length === 0);
4856
// starting actual request
4957

50-
res = await PUT(`/requests/send/102?token=${MOCK_TOKEN}`);
58+
res = await PUT("/requests/send/102", {
59+
headers: {
60+
Authorization: MOCK_TOKEN,
61+
},
62+
});
5163
expect(res.status).toBe(201);
5264

53-
res = await GET(`/users/pending/from-me?token=${MOCK_TOKEN}`);
65+
res = await GET("/users/pending/from-me", {
66+
headers: {
67+
Authorization: MOCK_TOKEN,
68+
},
69+
});
5470
expect(await res.json()).toSatisfy(
5571
(s) => s.length === 1 && s[0].name === "山田花子",
5672
);

web/api/internal/fetch-func.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ export async function uploadImage(path: string, file: File): Promise<URL> {
88
if (file.size >= MAX_IMAGE_SIZE) {
99
throw new Error("画像のアップロードに失敗しました: 画像が大きすぎます");
1010
}
11-
const res = await fetch(`${path}?token=${await getIdToken()}`, {
11+
const res = await fetch(path, {
1212
method: "POST",
1313
headers: {
1414
"Content-Type": "image/png",
15+
Authorization: await getIdToken(),
1516
},
1617
body: file,
1718
});

web/firebase/auth/lib.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,18 @@ export async function credFetch(
3434
path: string,
3535
body?: unknown,
3636
): Promise<Response> {
37-
let idToken = await getIdToken();
37+
const idToken = await getIdToken();
3838
const init: RequestInit = { method };
3939
if (body) {
4040
init.body = JSON.stringify(body);
4141
init.headers = {
4242
"Content-Type": "application/json",
43+
Authorization: idToken,
44+
};
45+
} else {
46+
init.headers = {
47+
Authorization: idToken,
4348
};
4449
}
45-
let res = await fetch(`${path}?token=${idToken}`, init);
46-
47-
if (res.status === 401) {
48-
idToken = await getIdToken();
49-
res = await fetch(`${path}?token=${idToken}`, init);
50-
}
51-
52-
return res;
50+
return await fetch(path, init);
5351
}

0 commit comments

Comments
 (0)