diff --git a/web/app/chat/page.tsx b/web/app/chat/page.tsx
index 8546f988..b6ced666 100644
--- a/web/app/chat/page.tsx
+++ b/web/app/chat/page.tsx
@@ -8,6 +8,14 @@ import RoomList from "~/components/chat/RoomList";
import { RoomWindow } from "~/components/chat/RoomWindow";
import FullScreenCircularProgress from "~/components/common/FullScreenCircularProgress";
+export default function Chat() {
+ return (
+ }>
+
+
+ );
+}
+
function ChatListContent() {
const searchParams = useSearchParams();
@@ -28,11 +36,3 @@ function ChatListContent() {
);
}
-
-export default function Chat() {
- return (
- }>
-
-
- );
-}
diff --git a/web/firebase/auth/lib.ts b/web/firebase/auth/lib.ts
index 0d0e22b3..4d6e852d 100644
--- a/web/firebase/auth/lib.ts
+++ b/web/firebase/auth/lib.ts
@@ -5,25 +5,19 @@ import { app } from "../config";
export class ErrUnauthorized extends Error {}
-let user: User;
-let token: string;
-
const auth = getAuth(app);
-onAuthStateChanged(auth, async (u: User | null) => {
- if (u != null) {
- user = u;
- token = await user.getIdToken();
- }
-});
-async function refreshToken() {
- token = await user.getIdToken(true);
-}
+// 認証状態の完了を待機するためのPromiseを作成
+const token = new Promise((resolve) => {
+ onAuthStateChanged(auth, async (u: User | null) => {
+ if (u != null) {
+ resolve(await u.getIdToken());
+ }
+ });
+});
export async function getIdToken(): Promise {
- if (token) return token;
- await refreshToken();
- return token;
+ return await token;
}
type RequestMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
@@ -39,7 +33,7 @@ export async function credFetch(
path: string,
body?: unknown,
): Promise {
- const idToken = await getIdToken();
+ let idToken = await getIdToken();
const init: RequestInit = { method };
if (body) {
init.body = JSON.stringify(body);
@@ -47,11 +41,11 @@ export async function credFetch(
"Content-Type": "application/json",
};
}
-
let res = await fetch(`${path}?token=${idToken}`, init);
+
if (res.status === 401) {
- await refreshToken();
- res = await fetch(`${path}?token=${idToken}`);
+ idToken = await getIdToken();
+ res = await fetch(`${path}?token=${idToken}`, init);
}
return res;