Skip to content

Commit d58d8fc

Browse files
authored
Next移行に伴う、リロード問題を解決 (#518)
# PRの概要 ## 具体的な変更内容 ## 影響範囲 ## 動作要件 ## 補足 ## レビューリクエストを出す前にチェック! - [ ] 改めてセルフレビューしたか - [ ] 手動での動作検証を行ったか - [ ] server の機能追加ならば、テストを書いたか - 理由: 書いた | server の機能追加ではない - [ ] 間違った使い方が存在するならば、それのドキュメントをコメントで書いたか - 理由: 書いた | 間違った使い方は存在しない - [ ] わかりやすいPRになっているか <!-- レビューリクエスト後は、Slackでもメンションしてお願いすることを推奨します。 -->
1 parent 7b253fb commit d58d8fc

File tree

2 files changed

+21
-27
lines changed

2 files changed

+21
-27
lines changed

web/app/chat/page.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import RoomList from "~/components/chat/RoomList";
88
import { RoomWindow } from "~/components/chat/RoomWindow";
99
import FullScreenCircularProgress from "~/components/common/FullScreenCircularProgress";
1010

11+
export default function Chat() {
12+
return (
13+
<Suspense fallback={<FullScreenCircularProgress />}>
14+
<ChatListContent />
15+
</Suspense>
16+
);
17+
}
18+
1119
function ChatListContent() {
1220
const searchParams = useSearchParams();
1321

@@ -28,11 +36,3 @@ function ChatListContent() {
2836
<RoomList roomsData={state.data} />
2937
);
3038
}
31-
32-
export default function Chat() {
33-
return (
34-
<Suspense fallback={<FullScreenCircularProgress />}>
35-
<ChatListContent />
36-
</Suspense>
37-
);
38-
}

web/firebase/auth/lib.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,19 @@ import { app } from "../config";
55

66
export class ErrUnauthorized extends Error {}
77

8-
let user: User;
9-
let token: string;
10-
118
const auth = getAuth(app);
12-
onAuthStateChanged(auth, async (u: User | null) => {
13-
if (u != null) {
14-
user = u;
15-
token = await user.getIdToken();
16-
}
17-
});
189

19-
async function refreshToken() {
20-
token = await user.getIdToken(true);
21-
}
10+
// 認証状態の完了を待機するためのPromiseを作成
11+
const token = new Promise<string>((resolve) => {
12+
onAuthStateChanged(auth, async (u: User | null) => {
13+
if (u != null) {
14+
resolve(await u.getIdToken());
15+
}
16+
});
17+
});
2218

2319
export async function getIdToken(): Promise<IDToken> {
24-
if (token) return token;
25-
await refreshToken();
26-
return token;
20+
return await token;
2721
}
2822

2923
type RequestMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
@@ -39,19 +33,19 @@ export async function credFetch(
3933
path: string,
4034
body?: unknown,
4135
): Promise<Response> {
42-
const idToken = await getIdToken();
36+
let idToken = await getIdToken();
4337
const init: RequestInit = { method };
4438
if (body) {
4539
init.body = JSON.stringify(body);
4640
init.headers = {
4741
"Content-Type": "application/json",
4842
};
4943
}
50-
5144
let res = await fetch(`${path}?token=${idToken}`, init);
45+
5246
if (res.status === 401) {
53-
await refreshToken();
54-
res = await fetch(`${path}?token=${idToken}`);
47+
idToken = await getIdToken();
48+
res = await fetch(`${path}?token=${idToken}`, init);
5549
}
5650

5751
return res;

0 commit comments

Comments
 (0)