From c2972c19a91aaf486a4a6c5c440b0a73656ad033 Mon Sep 17 00:00:00 2001 From: MurakawaTakuya Date: Tue, 10 Dec 2024 15:42:46 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=A1=E3=83=BC=E3=83=AB=E3=82=A2=E3=83=89?= =?UTF-8?q?=E3=83=AC=E3=82=B9=E3=81=A7=E7=99=BB=E9=8C=B2=E3=81=97=E3=81=9F?= =?UTF-8?q?=E5=A0=B4=E5=90=88=E3=81=AEdisplayName=E3=82=92=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Components/NameUpdate/NameUpdate.tsx | 13 ++++++++++++- src/utils/Auth/signUpWithMail.ts | 9 +++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Components/NameUpdate/NameUpdate.tsx b/src/Components/NameUpdate/NameUpdate.tsx index 47a2219..a916584 100644 --- a/src/Components/NameUpdate/NameUpdate.tsx +++ b/src/Components/NameUpdate/NameUpdate.tsx @@ -1,5 +1,5 @@ "use client"; -import { appCheckToken, functionsEndpoint } from "@/app/firebase"; +import { appCheckToken, auth, functionsEndpoint } from "@/app/firebase"; import { useUser } from "@/utils/UserContext"; import { DialogContent, @@ -10,6 +10,7 @@ import { } from "@mui/joy"; import Button from "@mui/material/Button"; import Stack from "@mui/material/Stack"; +import { updateProfile } from "firebase/auth"; import React, { useState } from "react"; export default function NameUpdate({ @@ -23,6 +24,7 @@ export default function NameUpdate({ const { user } = useUser(); const handleNameUpdate = async (event: React.FormEvent) => { event.preventDefault(); + const response = await fetch(`${functionsEndpoint}/user/${user?.uid}`, { method: "PUT", headers: { @@ -31,6 +33,15 @@ export default function NameUpdate({ }, body: JSON.stringify({ name: newName }), }); + + // Firebase Authentication の displayName を更新 + if (auth.currentUser) { + await updateProfile(auth.currentUser, { displayName: newName }); + console.log("Firebase displayName updated successfully"); + } else { + console.error("Failed to update displayName: No authenticated user"); + } + if (!response.ok) { console.error("Failed to update name"); } else { diff --git a/src/utils/Auth/signUpWithMail.ts b/src/utils/Auth/signUpWithMail.ts index bd9ef2f..b0b58f9 100644 --- a/src/utils/Auth/signUpWithMail.ts +++ b/src/utils/Auth/signUpWithMail.ts @@ -4,6 +4,7 @@ import { updateUser } from "@/utils/UserContext"; import { createUserWithEmailAndPassword, sendEmailVerification, + updateProfile, } from "firebase/auth"; /** @@ -24,6 +25,14 @@ export const signUpWithMail = ( // Signed up const user = userCredential.user; + // Firebase AuthのdisplayNameを設定 + try { + await updateProfile(user, { displayName: name }); + console.log("ユーザー名を設定しました:", name); + } catch (profileUpdateError) { + console.error("プロファイル更新に失敗しました:", profileUpdateError); + } + // uidとdocument IDを一致させる await createUser(name, user.uid); updateUser({