Skip to content Skip to sidebar Skip to footer

How Do I Add More Fields In Firebase-auth?

After a long research on firebase-auth with email and password, I found that we can't give more than two fields (email and password). In my assignment, they supposed to give three

Solution 1:

Firebase Auth object has a displayName property which you use to store user's name. However, if you are using Client SDK then you would have to create the user first and then update the name. This can be done in a single step if you use Admin SDK with Cloud functions or a server.

import { useAuth } from"firebase/auth"const auth = getAuth();

const createNewUser = async () => {
  const { user } = awaitcreateUserWithEmailAndPassword(auth, email, password)
  awaitupdateProfile(user, { displayName: "Jane Q. User" })
  console.log('New user', user.uid)
}

If the full name consists of a first name and a last name, then you can add a - or any symbol between them e.g. first-last in displayName. Then you can read the name as shown below:

const { displayName } = auth.currentUserconst [firstName, lastName] = displayName.split("-")

If you need to access the data from a server environment or any user management page, then it'll be best to store details of users in a database answered by @TarikHuber.

Solution 2:

You have basicaly two options for doing that:

  1. as mentioned in the comments you could use the database to store additional data for each user uid
  2. use customClaims to store such additional data there

I would recommend the first one for more data if you need to store a lot of data and if you have less you could use the customClaims. They are limited in size so be carefull how much you save inside.

Post a Comment for "How Do I Add More Fields In Firebase-auth?"