Skip to content

Commit

Permalink
メールアドレスで登録した場合のdisplayNameを修正
Browse files Browse the repository at this point in the history
  • Loading branch information
MurakawaTakuya committed Dec 10, 2024
1 parent e6eee37 commit c2972c1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Components/NameUpdate/NameUpdate.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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({
Expand All @@ -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: {
Expand All @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions src/utils/Auth/signUpWithMail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { updateUser } from "@/utils/UserContext";
import {
createUserWithEmailAndPassword,
sendEmailVerification,
updateProfile,
} from "firebase/auth";

/**
Expand All @@ -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({
Expand Down

0 comments on commit c2972c1

Please sign in to comment.