Meteor Accounts add extra fields - javascript

How do I add extra fields to the Meteor Accounts?
Ex: Profile Picture, Header Picture field etc...
I know how to change the password Accounts.setUsername(userId, username);
But how would I add an extra field to the Accounts?

You need to update the users collection in mongo. The way to access it is by using
Meteor.users.update(Meteor.userId(), {
$set:{
profile: {
picture: 'url-to-picture'
}
}});
According to documentation, anything that need to be updated by user should be stored under the profile key
profile: an Object which the user can create and update with any data.
Do not store anything on profile that you wouldn't want the user to
edit unless you have a deny rule on the Meteor.users collection.

Related

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.

Best approach/design to web application?

I have been taking a Node.js course on Udemy and would like to apply some of the knowledge I have gained to create a simple web application. I would like to have a user register, which leads him to an admin panel, this part I already have.
This user (requester) can then refer users (invitees) to this website using a unique link. For example he would click a button to generate a unique hyperlink (my idea for this link was to be the http://websiteurl/userid of the requester who is sending the link).
The requester can then send this link through email to their friends to invite them to the website, once they click the link they are taken to a sign up form and when they fill in the form they are linked (added to an array under the original user).
How would I go about setting up this "session", as in make sure that the form that the invitees fill out is linked to the original requester? How can those forms be generated dynamically on the requester's hyperlink?
I'm not looking for the exact code to do this, but rather validation if my idea for the url is a good approach or if there are other approaches I should consider.
Thanks in advance!
Well, this would require you changing the schema for your database. A user will need to have a property like:
{
referred: []
}
The referred array should contain ids or some sort of reference to a user's referred users.
On the "/:userid" route, the form should submit to the route where a new user is created and have a parameter with the user ID. In this case, I am using query parameters.
So if a person were to visit /foo, they would get a form that would post to a URL like /new?userid=foo.
In that route, you can do something like this (assuming Express):
app.post("/new", (req, res) => {
const userID = req.query.userid;
const newUser = // create user normally
if(userID) {
// `userID` referred this user
const referrer = getUser(userID);
referrer.referred.push(newUser);
save(referrer);
}
});
The getUser function should returning the current user, and you should modify the referred property with the new user. This code was merely an outline and you should update the user in your database.
In a nutshell, a form should post to /new?userid=foo, and when creating a new user, if this parameter is present, you can update the database entry for the user id in the parameter with the id of the new user.

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

Check for new user

I want to know when a user logs in via a provider(twitter,facebook,etc...) If he is a new user. If so then add his data to my firebase.. Currently i have read this:
https://www.firebase.com/docs/web/guide/user-auth.html
And it tells me to do:
var isNewUser = true;
Then check by saying
if(authData && isNewUser){
//This is a new user
}
But it dosent work... I logged in with the same account but it tells me its a new user...
Generally with Firebase, you would have a /users node where you would store other data about your users
users
user_id_0
name: "Buck Murdock"
email: "buck#moonbasealphabeta"
user_id_1
name: "Ted Striker"
email: "ted#airplane.com"
The user_id is the Firebase assigned user id.
When a user starts to access your App, you would query your /users node to see if, for example, their email already exists. If not, then you create the user and add them to the /users node.
Oh - and refer to the section Storing User Data in the Firebase guide as you may have overlooked the comment
// here we will just simulate this with an isNewUser boolean

Updating Meteor.users from client

I have a form that tried to update meteor.users with extra information about users with the following helper
Template.Profile.events({
'submit form': function(e) {
e.preventDefault();
var post = {
firstName: $(e.target).find('[name=firstname]').val()
};
Meteor.users.update( { _id: Meteor.userId() }, { $set: { 'firstName': post.firstName }} );
}
});
however, i get update failed: Access denied
Another question is, I am wondering whether I should do extra update straight to Meteor.users collection or should I have a seperate collection to store these data.
thanks
Due to the fact that you are trying to set an attribute directly on the base user object, you are receiving the 'Access denied' error. According to the Meteor documentation for Meteor.users:
By default, the current user's username, emails, and profile are
published to the client.
This means that you can update any of those user attributes, but if you want to add additional ones, it is best to add them to one of these already existing fields. I would suggest adding something like `firstName' to the profile attribute. In this case, your code would look something like this:
Meteor.users.update({_id: Meteor.userId()}, {$set: {'profile.firstName': post.firstName}});

Categories