0

I would like to make an application in React Native that allows to work in two modes, parent and child. The initial stage is registration with Firebase, then after adding additional information about the role (parent / child), registering both the child and parent, and after logging in both of them, the child will share location and parent will receive it.

I would like to add additional fields such as role (parent / child) in my application in React Native + Firebase, to later create other functionalities of the application based on the role.

Registration:

firebase
    .auth()
    .createUserWithEmailAndPassword(email, password)
    .then(userCredentials => {
        return userCredentials
            .user.updateProfile({
                displayName: name,
            })
            .additionalUserInfo.profile = {
            role: role,
        }
    })

Homescreen

const { displayName } = firebase.auth().currentUser;
const { role } = firebase.additionalUserInfo.profile;

this.setState({displayName, role});

and role returns undefined.

1 Answer 1

4

The properties that you can store on a user profile are defined by Firebase Authentication. You can't just add additional properties to it as you see fit. At best Firebase will simply ignore those, but likely it will also explicitly reject them (throwing an error).

If you want to store additional information about a user, you have two main options:

  1. Store the additional information as custom claims in the user's authentication token.
  2. Store the additional information in an external database, such as the Firestore and Realtime Database that are part of Firebase.

While storing the data as custom claims is pretty close to what you want to accomplish, you'll need to keep a few things in mind:

  • Custom claims are used for authorization purposes, and for that reason can only be set from a trusted environment (such as your development machine, a server you control, or Cloud Functions). So you can't simply set the role from within the app, and will need a separate process to add that claim.
  • After setting a custom claim on a user profile it may take up to an hour before that change is visible in the client. If you need it sooner, you can force the user to sign in again, or refresh their ID token.
  • Custom claims are sent with every request you make to Firebase resources, and for that reason are very limited in size. There's a maximum size of 1000 bytes for custom claims for a user. While your current role will easily fit into that, it may limit what you can add later.

If instead you store user data in an external database, you'll typically combine it with other information about that user into a users node/collection. In here you'd store a document/node for each user based on that user's UID, and then their profile information.

So something like:

users: {
  uidOfAleksandra: {
    username: "Aleksandra",
    displayName: "Aleksandra Lastname",
    role: "parent",
    registrationDate: "2020-02-01"
  },
  uidOfPuf: {
    username: "puf",
    displayName: "Frank van Puffelen",
    role: "child",
    registrationDate: "2015-03-07"
  },
}

Having this list of user profiles does not only allow you to store the additional information for each user, but would also allow you to query that list of users from within your app, something that the Authentication API doesn't allow from within application code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.