Sending verification email with Meteor causing error - meteor

I have the following:
Template.joinTemplate.events({
"submit #Signup-Form": function(event, template) {
event.preventDefault();
var email = template.find('#Email').value;
Accounts.createUser({
username: template.find('#Email').value,
emails: {
address: template.find('#Email').value
},
password: template.find('#Password').value,
profile: {
firstname: template.find("#Firstname").value,
lastname: template.find("#Lastname").value
}
}, function(error) {
if (error) {
// Deal with errors
} else {
Accounts.sendVerificationEmail(this.userId, email);
Router.go('/');
}
});
}
});
But get the following error:
Error logging in with token: Error: You've been logged out by the server. Please login again. [403] debug.js:41
Exception in delivering result of invoking 'createUser': TypeError: Object #<Object> has no method 'sendVerificationEmail'
at http://localhost:3000/client/views/application/authentication/join_template.js?0ca175e5dc0f3b4596ed33e260d5636f8f9cc69b:28:26
at http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:801:19
at loggedInAndDataReadyCallback (http://localhost:3000/packages/accounts-base.js?efdcc57c69f7e2ccbb61f1e963da216b1729ac72:455:5)
at null._callback (http://localhost:3000/packages/meteor.js?2b578107b8239ff9bc64200e6af2a24001461b30:801:22)
at _.extend._maybeInvokeCallback (http://localhost:3000/packages/livedata.js?418d88f2513ae6bf0ff1447759a5c590923456bd:3502:12)
at _.extend.receiveResult (http://localhost:3000/packages/livedata.js?418d88f2513ae6bf0ff1447759a5c590923456bd:3522:10)
at _.extend._livedata_result (http://localhost:3000/packages/livedata.js?418d88f2513ae6bf0ff1447759a5c590923456bd:4452:9)
at onMessage (http://localhost:3000/packages/livedata.js?418d88f2513ae6bf0ff1447759a5c590923456bd:3376:12)
at http://localhost:3000/packages/livedata.js?418d88f2513ae6bf0ff1447759a5c590923456bd:2705:11
at Array.forEach (native)
I have all accounts-base, accounts-ui, accounts-password installed.
Not sure what I'm doing wrong :(

As you can read in the documentation, Accounts.sendVerificationEmail is only available on the server, and you're trying to use it on the client.
I'm not sure that you can use the Accounts.onCreateUser function to send verification emails: in this function, the user has not been added to the Meteor.users collection yet, and I guess Accounts.sendVerificationEmail requires the user to be in that collection. I don't know the best way to solve this, but you can always use a cursor selecting all users, and then observe users added to the collection (although, this is not a good solution).
It's not enough to use the sendVerificationEmail field in the Accounts.config function?

Related

Meteor Account.createUser TypeError: Cannot read property 'accessToken' of undefined

I try to create User Registration. I have install
accounts-base 1.2.2* A user account system
accounts-password 1.1.4* Password support for accounts
On client side :
var userNew = {
password: textPassword,
username: textUserName,
profile: {
address: textAddress
}
};
Accounts.createUser(userNew, function (err) {
if (err) {
alert(err.message);
} else {
Router.go('/');
}
});
But show error :
I20160423-17:47:07.299(7)? Exception while invoking method 'createUser' TypeError: Cannot read property 'accessToken' of undefined
Also i have set on server side :
Accounts.config({
forbidClientAccountCreation : false
});
I done with update & recreate PROJECT. I dont figure whats the problems. but recreate project solved this problems.
Hopelly its helps others

Trying to publish users in Meteor

I'm quite new in Meteor and I got an issue: I added some information to users and I'd like to be able to see it in a view.
So I create my users, here is my event:
Accounts.createUser({
username: username,
email: email,
password: password,
firstname:firstname,
lastname:lastname,
chief:chief
},
function(error){
if(error){
alert(error.reason)
}else{
Router.go('home');
}
});
Accounts.onCreateUser(function(options,user){
user.firstname = options.firstname;
user.lastname = options.lastname;
user.chief = options.chief;
return user;
});
then I publish my collection to get access on my client side of the app:
Meteor.publish("personaldata", function () {
return Meteor.users.find({_id: this.userId});
});
And without doing anything else I got an issue on my app: there is no error message on my command prompt, but when I open my app I got an Ironn:Router message. Here is the message:
'organize your application'
Router.route('/', function () {
this.render('Home', {
data: function () {
return Items.findOne({_id: this.params._id});
}
});
});
Yes the whole part is the message. I tried to subscribe to the collection in 'home' to settle the issue but it doesn't work, the same message is displayed.Does someone know why this message is displayed ? How can I have access to those data? Isn't it the proper way?
Hope you guys can figure out what the problem is, thanks.
Found the problem...
As I'm stupid I put my publish in the lib directory, so this was on both the server and the client side... Meteor didn't know what to do with it on the client side.

Setting data that belongs to a User in iron router

Router.route('/settings', {
name: 'settings',
data: function() {
return Settings.findOne({userId: Meteor.user()._id});
}
});
It's showing an error in the browser:
Uncaught TypeError: Cannot read property '_id' of undefined
Any suggestions on how to grab the settings record for a logged in user?
Meteor logging in process usually takes a few ms to get ready, meanwhile Meteor.user() will return undefined and the first execution of your route data method will fail.
You can use Meteor.userId() to avoid this from happening until the user is connected for real.
Router.route('/settings', {
name: 'settings',
data: function() {
return Settings.findOne({
userId: Meteor.userId()
});
}
});

Accounts.createUser() is returning undefined on client

I have a SignUp/SignIn Process on my meteor site. I have a form to register new users and a form to login users.
I init users on serverside via
Accounts.createUser({username: "myusername", password: "mypassword"});
and I can login into my dummyusers via my loginform on the client with
Meteor.loginWithPassword(username, password)
I dont know why but for some reason I canĀ“t create new users on clientside. If I call
Accounts.createUser({username: username, password: password});
it returns undefined (safari)
I checked the input strings (username, password) and its not generating the error even pure strings like
Accounts.createUser({username: "test", password: "pass"});
returns undefined on client console.
I published and subscribed my users via:
Server
Meteor.publish('users', function () {
console.log("Server Log: publishing all users");
return Meteor.users.find();
});
Client
Meteor.subscribe("users");
I also can count() users on the client console but I cant use Accounts.createUser() so the createUser() function is not even working in my console.
Why I cant use Accounts.createUser() on client/console?
Accounts.createUser is an asynchronous function on the client because it needs to query the server to know if it actually succeeded (the username or email could already be in use). The return value of an asynchronous function will always be undefined, so the result is expected.
If you start with an empty project and an empty database, you could do:
Accounts.createUser({username: 'dave', password: 'password'});
which will return undefined, however a subsequent call to Meteor.userId() should return a string.
In order to determine if the call succeeded, you will need to supply a callback:
Accounts.createUser({username: 'dave', password: 'password'}, function(err) {
if (err)
console.log(err);
else
console.log('success!');
});

Extending the Meteor loginWithPassword method

I may be totally off line here, but I'm trying to extend the loginWithPassword method in Meteor to handle only returning users with a few parameters set in their profile.
I'm creating the users fine and once created they login as that user type and all is good, but, when I try and login again I hit a wall.
I've tried implementing my own login handler as follows...
Accounts.registerLoginHandler(function(loginRequest) {
console.log("Got to Accounts.registerLoginHandler");
console.log(loginRequest);
var userId = null;
var user = Meteor.loginWithPassword(loginRequest.email, loginRequest.password, function(error){
if(error !== undefined){
setAlert('error', 'Error in processing login. ' + error.reason + '.');
}
});
var userWithType;
if(user){ // we have the right username and password
console.log("Found a user and logged them in");
userWithType = Meteor.users.findOne({'id': user._id, 'profile.type': loginRequest.type});
}
if(userWithType){
console.log("Found User of that type")
userId = user._id;
}
console.log("UserId", userId);
return {
id: userId
}
});
But am getting an error when I get to this code that says
Got to Accounts.registerLoginHandler
{ email: 'blah2#blah', password: 'blha', type: 'user' }
Exception while invoking method 'login' TypeError: Object #<Object> has no method 'loginWithPassword'
at app/server/login.js:8:23
at tryAllLoginHandlers (app/packages/accounts-base/accounts_server.js:53:18)
at Meteor.methods.login (app/packages/accounts-base/accounts_server.js:73:18)
at maybeAuditArgumentChecks (app/packages/livedata/livedata_server.js:1367:12)
at _.extend.protocol_handlers.method.exception (app/packages/livedata/livedata_server.js:596:20)
at _.extend.withValue (app/packages/meteor/dynamics_nodejs.js:31:17)
at app/packages/livedata/livedata_server.js:595:44
at _.extend.withValue (app/packages/meteor/dynamics_nodejs.js:31:17)
at _.extend.protocol_handlers.method (app/packages/livedata/livedata_server.js:594:48)
at _.extend.processMessage.processNext (app/packages/livedata/livedata_server.js:488:43)
I'm obviously missing a this pointer or something like that, but don't know enough about this framework to know if I'm totally off track here even trying to get this to work.
Ta
P.
I am not too familiar with it but from http://docs.meteor.com, Meteor.loginWithPassword () can only be called on the client. You have written it into the server side code from the tutorial.
That is throwing the error you see. If you move it to the client you will also see that it only returns to the callback function so your variable user will remain undefined.
Meteor.user().profile is available on the client so you can just check the type there in the callback of loginWithPassword to check the information at login.

Resources