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
if (error){alert(error.reason) return;} // End if (result) { console.log(result) Router.go('/profile'); }- Ethaan