How to change profile name in Meteor?
Asked Answered
D

2

10

I wonder how to change user profile information in Meteor. I created an application using the accounts-base package, so I can quickly manage all related stuff with user accounts. Which is really great.

In official docs it says:

profile: an Object which (by default) the user can create and update with any data.

But how could I let the user to change it?

Regarding the same topic, using the {{loginButtons}} tag by default I get the following image when the user has logged in:

enter image description here

Is there any possibility to add Change profile, Change email or something similar?

Thanks

Darlington answered 26/1, 2013 at 10:11 Comment(1)
you have to write your own user profile feature, just use Meteor.users.update to update your collection.Arlenarlena
R
29

For the moment accounts-ui doesn't have a change profile button built in, you have to make it manually.

For instance if you do

Meteor.users.update({_id:Meteor.user()._id}, {$set:{"profile.name":"Carlos"}})

You could change the screen in accounts-ui above you have to show a name instead of the email you would click to display the dialog above.

The email is a little bit tricker, you have to do this from the server since (in a meteor.methods/call perhaps) you can't modify the email stuff from the client, I would suggest adding a new email and having it verified instead of changing the existing email (since its also their login). Or having it verified first then changing it so as not to change someones email to something where they can't recover their password.

Meteor.users.update({_id:Meteor.user()._id}, {$addToSet:{"emails":{address:"[email protected]","verified":false}}});

Or if you want users to have one email they can change:

Meteor.users.update({_id:Meteor.user()._id}, {$set:{"emails":[{address:"[email protected]"}]});
Roseroseann answered 26/1, 2013 at 12:34 Comment(8)
You forgot the quotes in the Mongodb selector (around profile.name) in your first line of code, this should be : Meteor.users.update({_id:Meteor.user()._id}, {$set:{"profile.name":"Carlos"}})Dogface
~~Where is the meteor mongodb url?~~ ahh this is in the browser gotcha oops thank youNinette
This is out of date and no longer works, as Access denied is shown.Ninette
@Nick, This still works, see : github.com/meteor/meteor/blob/…. Something else is stopping it working based on your app/configuration.Roseroseann
I guess I should be more clear this is my developer account. The user/profile name seems to be locked. Heck I can't even figure out how to delete the account.Ninette
#25474899Ninette
@Ninette I'm not sure about the relevance of the question to this answer. Meteor dev accounts are hosted by MDG on meteor.comRoseroseann
Sorry about the relevance but I got here as I want to delete a dead account of mine. Which appears to be impossible at the moment in doing it myself.Ninette
A
0

To expand this answer it is better to put this code into (Validated) Meteor method.

// imports/api/methods.js

const NO_SPECIAL_CHARACTERS_REGEX = /^[^`~!@#$%^&*()_|+=?;:'"<>{}\[\]\\/]*$/;

export const updateProfileName = new ValidatedMethod({
    name: 'users.updateProfileName',
    mixins: [CallPromiseMixin],
    validate: new SimpleSchema({
        name: { type: String, regEx: NO_SPECIAL_CHARACTERS_REGEX, min: 1, max: 50 },
    }).validator(),
    run({ name }) {
        if (!this.userId) {
            throw new Meteor.Error('User needs to be signed in to call this method');
        }
        return Meteor.users.update({ _id: this.userId }, { $set: { 'profile.name': name } });
    },
});

then on frontend

// imports/ui/changeName.js

Template.changeName.events({
    // ...
    
    async 'submit #change-name-form'(event, instance) {
        event.preventDefault();
        try {
            await updateProfileName.callPromise({ name: instance.$('#profile-name').val() });
        } catch (e) {
            // do something with the error
        }
    },
});

Arawakan answered 21/8, 2021 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.