Search code examples
javascriptmeteormeteor-blazemeteor-accounts

How to change password, email and meteor profile


Hello friends some can point me some material like make the change of password, email and in the profile add new fields like sex, age city add photos. Without using the collection or aldeed-autoform.   All doing with my own code so having a little more control some alded-autoform select functions like select or datepicker do not look great in the design.

Something like the image

Template.update.events({
  'submit form' ( event, template ) {
    event.preventDefault();

    const target = event.target;

    var oldPass = $('#password');
    var newPass = $('#password-new');
    var verifyPass = $('#password-confirm');

    if(newPass.val() === verifyPass.val()){
            Accounts.changePassword(oldPass.val(), verifyPass.val(), function(err){
                if(err) {
                    console.log(err);
                } else {
                    let profile = {
                      address: {
                        city: target.city.value,
                        street: target.street.value
                      },
                      gender: target.gender.value,
                      favoriteColor: target.favoriteColor.value,
                    }
                      console.log("datos actualizados");
                }
            });

            oldPass.val('');
            verifyPass.val('');
            newPass.val('');

        } else {
            console.log("password no match");
        }
    //}
    console.log(profile);

    Meteor.call('updateUser', profile);
  }
});

{
  _id: "bbca5d6a-2156-41c4-89da-0329e8c99a4f",  
  emails: [
    { address: "[email protected]", verified: true },
  ],
  createdAt: Wed Aug 21 2013 15:16:52 GMT-0700 (PDT),
  profile: {
    name: "Joe Schmoe",
    phone: "5555-555-555",
    gender: "male",
    city: "DC",
    country: "USA",
      birthday: "01-01-2017"
  }

Solution

  • Marcelrama,

    I will share how I am doing this. For my project I am storing things like a skype Id and time zone. So when I go to create a new user in Meteor Method

        Accounts.createUser({
            email: '[email protected]',
            password: 'testing',
            profile: {
                name: "Jack",
                gender: "Male",
                skypeid: "jack123",
                timezone: {
                    name: "UTC",
                    offset: "UTC"
                }
            }
        });
    

    You can also do something like this.

    let newUser = { ... };
    let user = Accounts.createUser(newUser);
    

    Storing it in a variable returns the Mongo Id.

    Then when you go to update profiles, you can do it like this.

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

    If you use Meteor.users.update on a field that does not exist yet, it will create that field with the value you specify, but make sure you use the "quote marks" around the keys.

    Meteor.users.update({ _id: user }, 
      { 
         $set: { "profile.address.city": "Orlando" } 
      }
    );
    

    Here is the official documentation. http://docs.meteor.com/api/accounts.html

    Here is a very Basic example

    Packages ---

    meteor add accounts-base
    meteor add accounts-password
    npm install --save bcrypt
    

    Template ---

        <template name="signup">
         <form>
          <input type="email" name="email"><br>
          <input type="password" name="password"><br>
          <input type="text" name="name"><br>
          <input type="submit" value="submit">
        </form>
       </template>
    

    Event ---

    import { Meteor } from 'meteor/meteor';    
    
       Template.signup.events({
          'submit form' ( event, template ) {
            event.preventDefault();
    
            const target = event.target;
    
            let newUser = {
              email: target.email.value,
              password: target.password.value,
              profile: {
                name: target.name.value
              }
            }
    
            Meteor.call('createNewUser', newUser);
          }
        });
    

    Method ---

    import { Meteor } from 'meteor/meteor';
    import { Accounts } from 'meteor/accounts-base';
    
    Meteor.methods({
      'createNewUser' ( userObj ) {
        let user = Accounts.createUser(userObj);
    
        user = Accounts.users.findOne(user);
    
        console.log(user);
      }
    });