I try to create simple login form with meteor and autoform but I got exception.
This is my schema for generating form:
FormSchemasLoginUsers = new SimpleSchema({
email: {
type: String,
regEx: SimpleSchema.RegEx.Email
},
password: {
type: String,
min: 6,
autoform: {
type: "password"
}
}
});
And this is my form template:
<template name="Login">
{{> quickForm id="loginUserForm" schema="FormSchemasLoginUsers" type="normal" }}
</template>
I try to handle user login with this hooks:
Template.Login.onRendered(function () {
AutoForm.addHooks('loginUserForm',
{
onSubmit: function (doc) {
console.log(doc);
Meteor.loginWithPassword(doc.email, doc.password, function(err) {
console.log(err);
if (err)
{
this.done(new Error("Login failed"));
}
this.done();
});
return false;
},
onSuccess: function(result) {
Router.go("home_private");
},
onError: function(error) {
console.log("Error: ", error);
}
}
);
});
But I got this error in firebug console:
Exception in delivering result of invoking 'login': .onSubmit/<@http://localhost:5000/app/client/views/login/login.js?b8771614cf48d3759cf0d764e51a0693caf23c81:18:5
Meteor.loginWithPassword/<.userCallback@http://localhost:5000/packages/accounts-password.js?8eae27e32c4d1bc1194f7c6dd2aaed1e33a88499:91:21
Ap.callLoginMethod/loginCallbacks<@http://localhost:5000/packages/accounts-base.js?7dabd814506e384c709f8bf707377955f9814129:612:5
_.once/<@http://localhost:5000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:794:14
Ap.callLoginMethod/loggedInAndDataReadyCallback@http://localhost:5000/packages/accounts-base.js?7dabd814506e384c709f8bf707377955f9814129:720:5
Meteor.bindEnvironment/<@http://localhost:5000/packages/meteor.js?9730f4ff059088b3f7f14c0672d155218a1802d4:999:17
._maybeInvokeCallback@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3500:7
.receiveResult@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3520:5
._livedata_result@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:4631:7
Connection/onMessage@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:3365:7
._launchConnection/self.socket.onmessage/<@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:2734:11
_.forEach@http://localhost:5000/packages/underscore.js?46eaedbdeb6e71c82af1b16f51c7da4127d6f285:149:7
._launchConnection/self.socket.onmessage@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:2733:9
REventTarget.prototype.dispatchEvent@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:173:9
SockJS.prototype._dispatchMessage@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:1158:5
SockJS.prototype._didMessage@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:1216:13
SockJS.websocket/that.ws.onmessage@http://localhost:5000/packages/ddp-client.js?250b63e6c919c5383a0511ee4efbf42bb70a650f:1363:9
meteor....a1802d4 (line 880)
I also check that login finished or not bu using Meteor.user()
and user login finished successfully.
What is the problem guys?? I spend two days for it but I can't find problem.
I think you receive this issue because you are calling this.done();
inside Meteor.loginWithPassword(user, password, [callback])
which results in a wrong binding of the this
value. You could either use ES6 arrow functions or just define var self = this;
and then call self.done();
instead of this.done();
.
For instance:
AutoForm.addHooks('loginUserForm', {
onSubmit: function(doc) {
console.log(doc);
Meteor.loginWithPassword(doc.email, doc.password, (err) => {
console.log(err);
if (err) this.done(new Error("Login failed"));
this.done();
});
return false;
},
onSuccess: function(result) {
Router.go("home_private");
},
onError: function(error) {
console.log("Error: ", error);
}
});