admin管理员组

文章数量:1325236

Hello how can I set username on Signup for Firebase User. I use firebase Authentication API. I thought about try / catch block and setting User properties like

const user = auth.currentUser;
user.name = 'John123'

Is it possible?

const onSubmitHandler = (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault()
        createUserWithEmailAndPassword(auth, email, password)
        
        
    }

Hello how can I set username on Signup for Firebase User. I use firebase Authentication API. I thought about try / catch block and setting User properties like

const user = auth.currentUser;
user.name = 'John123'

Is it possible?

const onSubmitHandler = (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault()
        createUserWithEmailAndPassword(auth, email, password)
        
        
    }
Share Improve this question edited Feb 5, 2022 at 23:51 Frank van Puffelen 600k85 gold badges889 silver badges859 bronze badges Recognized by Google Cloud Collective asked Feb 5, 2022 at 23:39 KayKay 8914 gold badges13 silver badges35 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

Firebase Authentication has a display name for each user, which can be whatever you want it to be. To set it, call updateProfile as shown in the documentation on updating the user's profile:

await createUserWithEmailAndPassword(auth, email, password);
updateProfile(auth.currentUser, {
  displayName: "John123"
})
   const result = await createUserWithEmailAndPassword(
    auth,
    email,
    password
  );

  await updateProfile(result.user, {
    displayName: username,
  });

It is full solution

 const onSubmitHandler = (e: React.FormEvent<HTMLFormElement>) => {
        e.preventDefault()
        const createUser = async ( ) => {
            await createUserWithEmailAndPassword(auth, email, password)
            updateProfile(auth.currentUser!, {
            displayName: "John123" // it can be a value of an input
          })
        }
        createUser()
    }

本文标签: javascriptHow to set username in Firebase authStack Overflow