How to Create a User with Multiple Attributes in Firebase with Swift

How can I create a user with multiple attributes in Firebase with Swift?

Traditionally, you would create a /users node in Firebase that contains any other info you want to store about that user.

Use the uid (user_id) as the node name for each user.

users
uid_0
name: "Bill"
food: "Pizza"
uid_1
name: "Ted"
food: "Tacos"

This question has been asked before so please try searching for questions similar to yours before asking.

A bit more info here Firebase Users Node

Adding Custom Attributes to Firebase Auth

You can't add custom attributes to Firebase Auth. Default attributes have been made available to facilitate access to user information, especially when using a provider (such as Facebook).

If you need to store more information about a user, use the Firebase realtime database. I recommend having a "Users" parent, that will hold all the User children. Also, have a userId key or an email key in order to identify the users and associate them with their respective accounts.

Hope this helps.

Ensuring unique username on creating a new user in Firebase (Swift)

I'm currently investigating the same thing. My issue is that I also want persistence enabled, so I need to be aware of what can be accessed offline. If you're using persistence, I would also recommend disallowing particular operations such as checking username existence if your client is offline, which you can do by listening to ".info/connected" as further detailed here:

https://firebase.google.com/docs/database/ios/offline-capabilities#section-connection-state

My personal workflow for this is as follows:

  1. Login to user's account
  2. Check if the user already has a username

Check the firebase database to see if their user details includes a username:

DB/users/*userUID*/username != nil

  1. If they don't have a username, then prompt them to set a username.

When they set their username, check if the username exists in:

DB/usernames/*username*/ != nil

If it doesn't exist, then write the username and userId in the two database locations checked above.

eg.

user.uid = wewe32323

username = scuba_steve

DB/usernames/scuba_steve = wewe32323
DB/users/wewe32323/username = scuba_steve

So now you have the DB/usernames reference that you can check quickly to see if anyone has a username already, and you also have DB/users/ where you can quickly find a username for a given user.

I won't say this is fool-proof, as I still a few concerns around concurrent requests. My current issue I'm investigating is that lets say you delete the username association to a particular user, the user can depend on their local copy of the database to incorrectly assert that they are still assigned to that username.

You could look into the database write rules to disallow anyone to modify existing data (enforcing that you can only write to the DB/usernames directory if there is no existing data. This would prevent overriding of whoever sets the username first, which I think is an important step.

It may also be worth investigating Transactions:

https://firebase.google.com/docs/database/ios/save-data#save_data_as_transactions

But I believe correct write rules as mentioned in the paragraph above should allow dependable writing.

Where to set User Properties in iOS for Firebase Analytics?

Go to UserProperty tab in Firebase console of your project. Click NEW USER PROPERTY.
Enter a name and description for the user property, then click CREATE.

Let's suppose you have set USER_TYPE as Firebase property .

Now you can fire this property using below code .

Analytics.setUserProperty(USER_TYPE, forName: "A/B/C")

It will take some time to reflect on your Events tab of your Firebase project console.

How to create different user groups in Firebase?

This is an incredibly broad topic and lots of it depends on how you implement the various parts of your app. Below is one of the many possible answers, just in an effort to get you started with some links.

If your app stores its data in one of Firebase database offerings (Realtime Database, or Cloud Firestore), or if it stores files in Cloud Storage through Firebase, then you'll likely want to store the role of each user as a custom claim in the profile of that user.

The Firebase Authentication documentation shows how to set such custom claims, for example how to set the admin property of a user to true from a Node.js script:

admin.auth().getUserByEmail('user@admin.example.com').then((user) => {
// Confirm user is verified.
if (user.emailVerified) {
// Add custom claims for additional privileges.
// This will be picked up by the user on token refresh or next sign in on new device.
return admin.auth().setCustomUserClaims(user.uid, {
admin: true
});
}
}).catch((error) => {
console.log(error);
});

Similarly you could set your user roles in a role property. Then you can check in the server-side security rules of the Realtime Database, Cloud Firestore, or Cloud Storage if the user has a role that allows them access to the specific data they're trying to access.

In the client-side code you can then decode the user's token to get access to the same claims and optimize the UI for them

Firebase: setting additional user properties

You're almost there. In the legacy Firebase documentation, we had a section on storing such additional user data.

The key is to store the additional information under the user's uid:

    let newUser = [
"provider": authData.provider,
"displayName": authData.providerData["displayName"] as? NSString as? String
]
// Create a child path with a key set to the uid underneath the "users" node
// This creates a URL path like the following:
// - https://<YOUR-FIREBASE-APP>.firebaseio.com/users/<uid>
ref.childByAppendingPath("users")
.childByAppendingPath(authData.uid).setValue(newUser)

I've added a note that we should add this information in the new documentation too. We just need to find a good spot for it.

How to add additional information to firebase.auth()

As far as I know, you have to manage the users profiles by yourself if you want to have more fields than the default user provided by Firebase.

You can do this creating a reference in Firebase to keep all the users profiles.

users: {
"userID1": {
"name":"user 1",
"gender": "male"
},
"userID2": {
"name":"user 2",
"gender": "female"
}
}

You can use onAuthStateChanged to detect when the user is logged in, and if it is you can use once() to retrieve user's data

firebaseRef.child('users').child(user.uid).once('value', callback)

Hope it helps



Related Topics



Leave a reply



Submit