Auth0: How to add custom properties to UserObject? - javascript

Hello dear StackOverFlow community,
i don't know much about Auth0 and need help. And I wonder how to add my own properties when creating a user? just for info i have connected Auth0 to my own MongoDB atlas database.
i want to add a customId when creating a user, and this should be a 16-digit random number.
And my userObj should then look something like this:
{
_id: ObjectId("5f720126054c87001662a138"),
connection:"MongoDB"
client_id:"zoClZ3gZE56iwblHXQ1vgwEcLfYr81Bx"
email:"gustek#gustek.com"
username:"gustek"
password:"$2b$10$dE69gGsDqVfWtmnXZ6EaKetILUmEju8N9PVjtDpgzzAp4jYNQbe8G"
tenant:"dev-test"
email_verified:false
customId:"9829539769841530"
}
I mean I found something in the documentation but I do not know how to implement it:
https://auth0.com/docs/users/set-metadata-properties-on-creation
Do I have to do it over this surface?
As I said before, I have no idea how I could achieve this. I am grateful for every answer!

You can use an app_metadata property in the user object to store data that is not part of the normalized user profile. This would include a custom UUID like you described here.
The user object would look like this:
{
client_id: "<ID of creating client (application)>",
tenant: "<name of creating Auth0 tenant>",
email: "<email address for the user>",
password: "<password for the user>",
username: "<name associated with the user>",
user_metadata: {
"language": "en"
},
app_metadata: {
"custom_id": "1234567890"
}
}
While user_metadata and app_metadata are optional, if supplied, they do not need to be stored in the legacy identity store; Auth0 automatically stores these values as part of the user profile record created internally. These values are (optionally) provided as a reference: their contents potentially being influential to legacy identity creation.

Related

Query stored values that contain specific string

I have a small realtime firebase database that's set up like this:
database
-messages
--XXXXXXXXXXXX
---id : "XXX-XXX"
---content : "Hello world!"
It's a very simple message system, the id field is basically a combination of users id from my mysql database. I'm trying to return all messages that match one of the ids, either sender or receiver. But I can't do it, seems like firebase only support exacts querys. Could you give me some guidanse?
Here's the code I'm working with
firebase.database().ref("messages").orderByChild("id").equalTo(userId).on("value", function(snapshot)
I'm looking for something like ".contains(userId)"
Firebase supports exact matches (with equalTo) and so-called prefix queries where the value starts with a certain value (by combining startAt and endAt). It does not support querying for values that end with a certain value.
I recommend keeping a mapping from each user IDs to their messages nodes, somewhere separately in their database.
So say that you have:
messages: {
"XXXXXXXXXXXX": {
id : "YYY-ZZZ",
content : "Hello world!"
}
}
You also have the following mappings:
userMessages: {
"YYY": {
"XXXXXXXXXXXX": true
},
"ZZZ": {
"XXXXXXXXXXXX": true
}
}
Now with this information you can look up the messages for each user based on their ID.
For more on the modeling of this type of data, I recommend:
Best way to manage Chat channels in Firebase
Many to Many relationship in Firebase
this artcle on NoSQL data modeling

I want to make unique usernames in firebase/firestore

app : {
users: {
"some-user-uid": {
email: "test#test.com"
username: "myname"
}
},
usernames: {
"myname": "some-user-uid"
}
}
I want to make unique usernames, like this: I would store all the usernames in a collection in firebase and than check if that doc with that username exists. This works perfectly fine but when I make a username with the same name on 2 accounts at the same time, user 1 makes the file and then user 2 overwrites that file.
How can I get around this?
This is not a copy of an asked question, all the answers answer how to make unique usernames but this bug is in all of them.
To prevent somebody being able to overwrite an existing username document, you'd use Firebase's security rules:
service cloud.firestore {
match /databases/{database}/documents {
// Allow only authenticated content owners access
match /usernames/{username} {
allow create: if !exists(/databases/$(database)/documents/usernames/$(username))
}
}
}
The above example allows creating the document if it doesn't exist yet. So with this the second write will be rejected.
You'll probably want to expand this to allow a user to only claim with their own UID, and unclaim a name too. To learn more on how to do that, I recommend reading the Firebase documentation on security rules.

How to get all users having custom claim as admin/moderator using firebase admin?

I want to send welcome message to all admins of my user database. Is there any method in firebase so that I can get list of all users with their custom claim as admin set to be true.
admin.auth().getUsers([
{ uid: 'uid1' },
{ email: 'user2#example.com' },
{ phoneNumber: '+15555550003' },
{ providerId: 'google.com', providerUid: 'google_uid4' },
])
Would above method applicable? If yes, then what parameter should I pass?
There is no built-in way to query Firebase Auth users based on custom claims on a user. If you want to be able to query, you would need to mirror the information into another database (like Firestore) and query that.
Otherwise, you will need to fetch every user and filter the results, which presumably is not an ideal solution.

Firebase - Allow user access to parts of database based on database information

I know how you can give a user read/write access to parts of the database using his UID, but I want to do something that is a little but more complicated, and I don't know how to do it. If I have a database structure like this:
Users:
oixMNpuyaaTTntNVl17MC74wL8e2:
accesTo:
Some Name: 'some value'
Some Other Name: 'some other value'
Stuff
Some Name
Information: 'More information'
InformationMore: 'Some more information'
In this case, I need user oixMNpuyaaTTntNVl17MC74wL8e2 to be able to access all the data under Some Name, which was specified somewhere else in the database. I hope I was able to explain my issue properly, and that this is possible! Also, it is not okay to set the Some Name child of Stuff to oixMNpuyaaTTntNVl17MC74wL8e2, because other users might also have access to Some Name.
Note: I'm using Firebase Web and Admin as well, using Python (Flask) and JavaScript.
I'm having a bit of trouble understanding your problem, if this isn't what you're looking for, please edit your question and explain what you're looking for in more depth.
If you want to limit access to "Some Name" to only users who have that access granted under the "Users" table, then you can write a rule that looks for the "Some Name" permission under that user. This rule would apply to all users of your app, so any user that has the access granted will be able to access that data.
Your rule would look like:
{
"rules": {
"Stuff": {
"$someName": {
".read": "root.child('Users').child(auth.uid).child('accesTo').val() == $someName"
}
},
// ...etc... other rules for other tables in your database
}
There are plenty more examples of this kind of thing in the documentation.

Meteor: Two types of user, the way to go with complexe UserAccounts?

I'm using Meteor/react and flowRouter and I would like to create an app with two kinds of users; let's say the stay simple:
Client
Provider
In fact Client has some "UserAccounts" fields (name, password, email ...) but I would like to add many of others: Idk like Passport Number, Social Security Number, Bank Account etc ...
The Provider could fill this fields if he wants too (so could have the same "role") but its main goal is to have other fields like idk: Job title, Category Job, Other Password etc ..
So Is some of you used the package UserAccounts to do this or you created a system by yourself ?
Of course I wish using email validation etc ... The application Ui would like to have a search component searching for Providers and only in Providers.
Also, I will use two Registration form (same page but two forms) but one Login modal for both types.
My question is, is it a good thing to use userAccount, and probably I will need roles for the two roles, but do UserAccount accept many others custom fields, do I have to set in in 'Profile' and won't it slow too much the application with only one user collection ?
Thanks guys for the mind blowing help :)
Yes, you can use userAccount for your case & No, it won't slow down to any noticeable amount.
To manage different roles you can use alaning:roles package. It helps you limit access to certain parts of code to roles and groups.
UPDATE: (Edit)
As Pointed out in comments, it is not advisable to use profile as it is automatically publishes. Instead try this:
Regarding, second part of adding more fields in user account, you can add any number of fields in the profile section. The document will look something like this:
{
_id: "bbca5d6a-2156-41c4-89da-0329e8c99a4f", // Meteor.userId()
username: "cool_kid_13", // unique name
emails: [
// each email address can only belong to one user.
{ address: "cool#example.com", verified: true },
{ address: "another#different.com", verified: false }
],
createdAt: Wed Aug 21 2013 15:16:52 GMT-0700 (PDT),
profile: {
//profile data
},
passportNo:'PASSPORT',
ssn:'SSSN',
oneMoreField: 'OneMoreField',
}
By default, here only email, username and profile will be published and your other data will not be visible to client unless published and subscribed.
You can read more about accounts here

Categories