From ce5e99747b3450a7cd93e604a18f4374fa7f430c Mon Sep 17 00:00:00 2001 From: MurakawaTakuya Date: Thu, 12 Dec 2024 10:16:04 +0900 Subject: [PATCH] =?UTF-8?q?uid=E3=82=92useId=E3=81=AB=E7=B5=B1=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/src/routers/userRouter.ts | 14 ++++++++------ src/Components/GoalModal/GoalModal.tsx | 2 +- src/Components/NameUpdate/NameUpdate.tsx | 2 +- src/Components/PostModal/PostModal.tsx | 2 +- src/Components/Progress/Progress.tsx | 2 +- src/app/mycontent/page.tsx | 4 ++-- src/types/types.ts | 2 +- src/utils/API/createUser.ts | 8 ++++---- src/utils/API/fetchUser.ts | 6 +++--- src/utils/Auth/signUpWithGoogleAccount.ts | 2 +- src/utils/Auth/signUpWithMail.ts | 6 +++--- src/utils/UserContext.tsx | 4 ++-- 12 files changed, 28 insertions(+), 26 deletions(-) diff --git a/functions/src/routers/userRouter.ts b/functions/src/routers/userRouter.ts index 97d9600..2126ecf 100644 --- a/functions/src/routers/userRouter.ts +++ b/functions/src/routers/userRouter.ts @@ -90,29 +90,31 @@ router.get("/name/:userName", async (req: Request, res: Response) => { // POST: 新しいユーザーを登録 router.post("/", async (req: Request, res: Response) => { - let uid: string; + let userId: string; let name: User["name"]; let streak: User["streak"]; let fcmToken: User["fcmToken"]; try { - ({ name, uid, streak = 0, fcmToken = "" } = req.body); + ({ name, userId, streak = 0, fcmToken = "" } = req.body); } catch (error) { return res.status(400).json({ message: "Invalid request body" }); } - if (!name || !uid) { - return res.status(400).json({ message: "name and uid are required" }); + if (!name || !userId) { + return res.status(400).json({ message: "name and userId are required" }); } try { - await db.collection("user").doc(uid).set({ + await db.collection("user").doc(userId).set({ name: name, streak: streak, fcmToken: fcmToken, }); - return res.status(201).json({ message: "User created successfully", uid }); + return res + .status(201) + .json({ message: "User created successfully", userId }); } catch (error) { return res.status(500).json({ message: "Error creating user" }); } diff --git a/src/Components/GoalModal/GoalModal.tsx b/src/Components/GoalModal/GoalModal.tsx index 2f2eadf..b951fbb 100644 --- a/src/Components/GoalModal/GoalModal.tsx +++ b/src/Components/GoalModal/GoalModal.tsx @@ -25,7 +25,7 @@ export default function GoalModal() { event.preventDefault(); const postData: Goal = { - userId: user?.uid as string, + userId: user?.userId as string, text: text, deadline: new Date(dueDate), }; diff --git a/src/Components/NameUpdate/NameUpdate.tsx b/src/Components/NameUpdate/NameUpdate.tsx index a916584..e747acd 100644 --- a/src/Components/NameUpdate/NameUpdate.tsx +++ b/src/Components/NameUpdate/NameUpdate.tsx @@ -25,7 +25,7 @@ export default function NameUpdate({ const handleNameUpdate = async (event: React.FormEvent) => { event.preventDefault(); - const response = await fetch(`${functionsEndpoint}/user/${user?.uid}`, { + const response = await fetch(`${functionsEndpoint}/user/${user?.userId}`, { method: "PUT", headers: { "X-Firebase-AppCheck": appCheckToken, diff --git a/src/Components/PostModal/PostModal.tsx b/src/Components/PostModal/PostModal.tsx index 11fc9a8..dbc08fc 100644 --- a/src/Components/PostModal/PostModal.tsx +++ b/src/Components/PostModal/PostModal.tsx @@ -54,7 +54,7 @@ export default function PostModal({ goalId }: { goalId: string }) { (errorMsg) => setError(errorMsg), async (url) => { const postData: Post = { - userId: user?.uid as string, + userId: user?.userId as string, storedId: url, text: text, goalId: goalId, diff --git a/src/Components/Progress/Progress.tsx b/src/Components/Progress/Progress.tsx index a9f9b4b..54bf8a7 100644 --- a/src/Components/Progress/Progress.tsx +++ b/src/Components/Progress/Progress.tsx @@ -192,7 +192,7 @@ const pendingStep = (result: GoalWithId, userName: string, user: UserData) => { resultType="pending" /> {/* 自分の作成した目標の場合のみ投稿可能にする */} - {result.userId === user?.uid && } + {result.userId === user?.userId && } ); diff --git a/src/app/mycontent/page.tsx b/src/app/mycontent/page.tsx index e4d798f..1f4f6cb 100644 --- a/src/app/mycontent/page.tsx +++ b/src/app/mycontent/page.tsx @@ -40,9 +40,9 @@ export default function MyContent() { {value == "pending" ? ( - + ) : ( - + )} ); diff --git a/src/types/types.ts b/src/types/types.ts index f79bb22..da6204e 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -1,5 +1,5 @@ export interface UserData { - uid: string; + userId: string; name: string; streak: number; loginType: LoginType; diff --git a/src/utils/API/createUser.ts b/src/utils/API/createUser.ts index 66acbb5..a3d182d 100644 --- a/src/utils/API/createUser.ts +++ b/src/utils/API/createUser.ts @@ -2,19 +2,19 @@ import { appCheckToken, functionsEndpoint } from "@/app/firebase"; /** * Cloud FunctionsのAPIを呼び出して、ユーザー情報をFirestoreに登録する - * Authenticationのuidと/userのdocument IDは同じにする + * AuthenticationのuserIdと/userのdocument IDは同じにする * * @param {string} name - * @param {string} uid + * @param {string} userId */ -export const createUser = async (name: string, uid: string) => { +export const createUser = async (name: string, userId: string) => { const response = await fetch(`${functionsEndpoint}/user/`, { method: "POST", headers: { "X-Firebase-AppCheck": appCheckToken, "Content-Type": "application/json", }, - body: JSON.stringify({ uid, name }), + body: JSON.stringify({ userId, name }), }); if (!response.ok) { diff --git a/src/utils/API/fetchUser.ts b/src/utils/API/fetchUser.ts index ede47b7..2b55a24 100644 --- a/src/utils/API/fetchUser.ts +++ b/src/utils/API/fetchUser.ts @@ -4,11 +4,11 @@ import { UserData } from "@/types/types"; /** * Cloud FunctionsのAPIを呼び出して、ユーザー情報をFirestoreから取得する * - * @param {string} uid + * @param {string} userId * @return {*} {Promise} */ -export const fetchUserById = async (uid: string): Promise => { - const response = await fetch(`${functionsEndpoint}/user/id/${uid}`, { +export const fetchUserById = async (userId: string): Promise => { + const response = await fetch(`${functionsEndpoint}/user/id/${userId}`, { method: "GET", headers: { "X-Firebase-AppCheck": appCheckToken, diff --git a/src/utils/Auth/signUpWithGoogleAccount.ts b/src/utils/Auth/signUpWithGoogleAccount.ts index 707597c..c105073 100644 --- a/src/utils/Auth/signUpWithGoogleAccount.ts +++ b/src/utils/Auth/signUpWithGoogleAccount.ts @@ -16,7 +16,7 @@ export const signUpWithGoogleAccount = async () => { // uidとdocument IDを一致させる await createUser(result.user.displayName ?? "no name", result.user.uid); updateUser({ - uid: result.user.uid, + userId: result.user.uid, name: result.user.displayName ?? "no name", streak: 0, loginType: "Google", diff --git a/src/utils/Auth/signUpWithMail.ts b/src/utils/Auth/signUpWithMail.ts index b0b58f9..db83006 100644 --- a/src/utils/Auth/signUpWithMail.ts +++ b/src/utils/Auth/signUpWithMail.ts @@ -8,7 +8,7 @@ import { } from "firebase/auth"; /** - * Firebase Authenticationでユーザーを作成し、生成されたuidをドキュメントIDとしてFirestoreにユーザー情報を登録する + * Firebase Authenticationでユーザーを作成し、生成されたuserIdをドキュメントIDとしてFirestoreにユーザー情報を登録する * * @param {string} email * @param {string} password @@ -33,10 +33,10 @@ export const signUpWithMail = ( console.error("プロファイル更新に失敗しました:", profileUpdateError); } - // uidとdocument IDを一致させる + // userIdとdocument IDを一致させる await createUser(name, user.uid); updateUser({ - uid: user.uid, + userId: user.uid, name: name, streak: 0, loginType: "Mail", diff --git a/src/utils/UserContext.tsx b/src/utils/UserContext.tsx index 45e5ab4..ec77b19 100644 --- a/src/utils/UserContext.tsx +++ b/src/utils/UserContext.tsx @@ -75,7 +75,7 @@ export const UserProvider = ({ children }: Props) => { // ゲスト以外のログインの場合はユーザーデータを取得しuseContextで保持 if (loginType === "Guest") { const guestData: UserData = { - uid: firebaseUser.uid, + userId: firebaseUser.uid, name: "Guest", streak: 0, loginType: "Guest", @@ -89,7 +89,7 @@ export const UserProvider = ({ children }: Props) => { const userData = await fetchUserById(firebaseUser.uid); // ユーザーデータを作成する前にfetchしようとして"User not found"になるので、postした場所でsetさせている // "User not found"ではない(= 初回ログイン直後ではない)場合のみsetする - if (userData.uid) { + if (userData.userId) { setUser({ ...userData, loginType,