0
votes

I used normal codes, to signup with Meteor, especially on the client side. Normal code that works, meaning that whenever user created it would redirect the user to a profile page together with a session

Code that works

Template.signup.events({
   'submit form': function(event) {
      event.preventDefault();

      var email = event.target.email.value;
      var username = event.target.username.value;
      var password = event.target.password.value;

      Accounts.createUser({
        email: email,
        username: username,
        password: username;
      });

      Router.go('/profile');
    }
});

The codes above work perfectly, it redirects and show the user's data, but I read discover meteor book, it says that its better to put the logic on the serverside, because Im planning to add roles https://github.com/alanning/meteor-roles

Here's the new code

client/signup.js

Template.signup.events({
    'submit form': function(event) {
      event.preventDefault();

      var signupData = {
        email: event.target.email.value,
        username: event.target.username.value,
        password: event.target.password.value,
        roles: ['customer']
      }

      Meteor.call('signup', signupData, function(error, result) {
        if (error) return alert(error.reason);
        // End
        if (result) {
          Router.go('/profile');
        }
      });
    }
  });

server/collections/user.js

Meteor.methods({
  signup: function(data) {

    id = Accounts.createUser({
      email: data.email,
      password: data.password,
      username: data.username,
    });

    if (data.roles.length > 0) {
      // Need _id of existing user record so this call must come
      // after `Accounts.createUser` or `Accounts.onCreate`
      Roles.addUsersToRoles(id, data.roles, 'default-group');
    }

    return data;
  }
});

The template below, is where Router.go will go to.

client/templates/accounts/profile.html

<template name="profile">
  {{#if currentUser}}

    <h3>Hello: {{ user.username }}</h3>
    <h4>Email: {{ user.email }}</h4>
  {{/if}}
</template>

client/templates/accounts/profile.js

Template.profile.helpers({
  user: function() {

    return {
      email: Meteor.user().emails[0].address,
      username: Meteor.user().username
    }
  }
});

For somehow the code above return no error, but the problem right now is that it doesn't show the user's object on profile html, but it did save the user's data to mongodb database?

So just want to clarify (My logic):

First I click signup -> will do Meteor.call -> then return, if there is no error --> Router.go
1
and the accounts is beign created?, since you are returnign the exact same data you are passing make the return to the Roles.addUsersToRoles and check what the result returns on the client - Ethaan
I check meteor mongo, the account is actually created, so I don't really know what is the actual problem right here. - airsoftFreak
what do you get with this?, the callback is good if (error){alert(error.reason) return;} // End if (result) { console.log(result) Router.go('/profile'); } - Ethaan
basically it returns the form data, that I have entered, let me share you the link of my meteor app. - airsoftFreak

1 Answers

0
votes
  1. Use Accounts.createUser(options) from the client passing in your data.roles variable into the options.profile key.
  2. On the server, use Accounts.onCreateUser():

server:

Accounts.onCreateUser(function(options,user){
  if ( options.profile.length > 0) {
    Roles.addUsersToRoles(user._id, options.profile, 'default-group');
  }
  return user;
});

docs