Skip to content
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
16 changes: 8 additions & 8 deletions web/app/chat/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<Suspense fallback={<FullScreenCircularProgress />}>
<ChatListContent />
</Suspense>
);
}

function ChatListContent() {
const searchParams = useSearchParams();

Expand All @@ -28,11 +36,3 @@ function ChatListContent() {
<RoomList roomsData={state.data} />
);
}

export default function Chat() {
return (
<Suspense fallback={<FullScreenCircularProgress />}>
<ChatListContent />
</Suspense>
);
}
32 changes: 13 additions & 19 deletions web/firebase/auth/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>((resolve) => {
onAuthStateChanged(auth, async (u: User | null) => {
if (u != null) {
resolve(await u.getIdToken());
}
});
});

export async function getIdToken(): Promise<IDToken> {
if (token) return token;
await refreshToken();
return token;
return await token;
}

type RequestMethod = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
Expand All @@ -39,19 +33,19 @@ export async function credFetch(
path: string,
body?: unknown,
): Promise<Response> {
const idToken = await getIdToken();
let idToken = await getIdToken();
const init: RequestInit = { method };
if (body) {
init.body = JSON.stringify(body);
init.headers = {
"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;
Expand Down