programing

Firebase 사용자의 displayName을 설정하려면 어떻게 해야 합니까?

instargram 2023. 6. 26. 21:02
반응형

Firebase 사용자의 displayName을 설정하려면 어떻게 해야 합니까?

Firebase 웹 사이트의 JS Auth 문서에 따르면 displayName을 가져오는 방법과 displayName을 업데이트하는 방법만 나와 있습니다.그래서 업데이트를 하려고 했습니다.하지만 그것은 논리적이지 않습니다. 왜냐하면 어떻게 무언가를 만들지 않고 업데이트할 수 있을까요?

그래서 여기서 제 질문은 등록하는 동안 사용자의 displayName을 어떻게 설정할 수 있나요?

function createUser(email, password) {
    firebase.auth().createUserWithEmailAndPassword(email, password).catch(function (error) {
        error.message.replace(".", "");
        alert(error.message + " (" + error.code + ")");
        document.getElementById("password").value = "";
    });
    if (firebase.auth().currentUser != null) {
        firebase.auth().currentUser.updateProfile({
            displayName: document.getElementById("name").value
        }).then(function () {
            console.log("Updated");
        }, function (error) {
            console.log("Error happened");
        });
    }
}

이미 시도해 봤는데 효과가 없는 것으로 판명되었습니다.

진심으로, 파루크

요청을 체인으로 연결해야 합니다.

firebase.auth().createUserWithEmailAndPassword(email, password)
.then(function(result) {
  return result.user.updateProfile({
    displayName: document.getElementById("name").value
  })
}).catch(function(error) {
  console.log(error);
});`

이것은 비동기 대기를 사용하여 Firebase v9에서 사용한 것입니다.

// firebase-config.js
import { initializeApp } from "firebase/app";
import { getAuth } from "firebase/auth";

const firebaseConfig = {
  apiKey: ...,
  authDomain: ...,
  projectId: ...,
  storageBucket: ...,
  messagingSenderId: ...,
  appId: ...,
  measurementId: ...,
}

const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);

// register.js

import { auth } from "../../services/firebase-config"; 
import {
  createUserWithEmailAndPassword,
  sendEmailVerification,
  updateProfile,
} from "firebase/auth";


// handleRegister
const register = async (name, email, password) => {
    try {
      await createUserWithEmailAndPassword(auth, email, password).catch((err) =>
        console.log(err)
      );
      await sendEmailVerification(auth.currentUser).catch((err) =>
        console.log(err)
      );
      await updateProfile(auth.currentUser, { displayName: name }).catch(
        (err) => console.log(err)
      );
    } catch (err) {
      console.log(err);
    }
  };

그런 다음 클릭/onSubmit에서 이 기능을 호출합니다.name,email,그리고.password

다음은 제출 시 formik을 사용한 구현입니다.

onSubmit={({ name, email, password }) => {
  register(name, email, password);
}}

또는 Click(클릭)의 기능 입력 버튼을 간단히 호출할 수 있습니다.

<button onClick={() => register(name, email, password)}>submit</button>

못쓰겠어요({displayName: name})직접(편집기의 신택스 오류).그리고 다른 방법을 찾았습니다.

UserUpdateInfo updateInfo = UserUpdateInfo();
updateInfo.displayName = name;
result.user.updateProfile(updateInfo);

이것은 다트에 있습니다(Flutter와 함께 Firebase를 사용하고 있습니다).

firebase
      .auth()
      .createUserWithEmailAndPassword(newUser.email, newUser.password)
      .then((res) => {
        const user = firebase.auth().currentUser;
        return user.updateProfile({
          displayName: newUser.name
        })
      })

이것은 저에게 효과가 있었습니다.

firebase/auth에서 updateProfile을 가져와야 합니다.

import { getAuth, updateProfile } from "firebase/auth";
const auth = getAuth();
updateProfile(auth.currentUser, {
  displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(() => {
  // Profile updated!
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});

나를 위해 일하는 것

출처: https://firebase.google.com/docs/auth/web/manage-users#update_a_users_profile

언급URL : https://stackoverflow.com/questions/40389946/how-do-i-set-the-displayname-of-firebase-user

반응형