No response from Jquery validation when creating Meteor account - meteor

Basically, I'm using Jquery validation package as a way to alert errors to users when creating and registering accounts on Meteor, since the boilerplate interface doesn't work in my case.
Anyway, when a user tries to sign up for an account, I get no response at all on client. The user is just created with no message or redirection like it's supposed to have.
Here is the particular code:
Template.createAccount.onRendered(function(){
var validator = $('.register').validate({
submitHandler: function(){
user = {
email: document.getElementById("email").value,
password: document.getElementById("password").value
};
Accounts.createUser({
email: user.email,
password: user.password,
function(error){
if(error){
if(error.reason == "Email already exists."){
validator.showErrors({
email: "The email belongs to a registered user."
});
}
} else{
console.log("Account successfully created.");
Router.go("starter");
}
}
});
}
});
})
I'm using the same code logic for account logins with the only exception being a different meteor accounts function (Meteor.loginWithPassword() for login and Accounts.createUser() for account creation).
No response at all, so it probably has to do something with the callback function, since the user account is created, but no message displayed on client.

You're including your callback as part of your options object when it should be a separate argument. It should look more like this:
Accounts.createUser({
email: user.email,
password: user.password
}, function(error){
if(error){
if(error.reason == "Email already exists."){
validator.showErrors({
email: "The email belongs to a registered user."
});
}
} else{
console.log("Account successfully created.");
Router.go("starter");
}
});

Related

How do I send a Success message to user when account is created successfully in Meteor JS

I am creating an application that requires a simple user registration form. I am using account:core package to create users but my challenge is that the user gets created without any form of reply to the user that the account was successfully created. Here is my code
Template.register.events({
'submit form': function (event) {
event.preventDefault();
Accounts.createUser({
email: $('[name=email]').val(),
password: $('[name=password]').val(),
profile: {
first_name: $('[name=firstname]').val(),
last_name: $('[name=lastname]').val(),
current_location: $('[name=currentlocation]').val(),
date: $('[name=date]').val(),
phone_number: $('[name=phonenumber]').val(),
}
});
},
});
Accounts.createUser(options, [callback]) accepts a callback as the second parameter, which gets called once the account creation is completed. In this callback you can post your message to the user.
See the docs for more here.

optional setting in angularfire authwithpassword

I am using AngularFire $authWithPassword(credentials[, options]) for login. I assume [options] is "remember" with value of default, sessionOnly, none, as described here.
I tried all 3, I can not see any difference in my mobile app. I logged in my app, then kill/close my app, restart it. All 3 options let my app persist/remember username and password and logged in. Should at least None forget authentication data?
This is the code I use for authentication:
var _remember="default";
if (!$scope.isAutoLogin){ _
remember="none";
//"sessionOnly";
};
auth.$authWithPassword({ email: user.email, password: user.password, remember:_remember })
.then(function (authData) {
console.log("Logged in as:" + authData.uid);
}).catch(function (error) {
If I see it correctly it is just a matter of syntax, your login should be like this:
auth.$authWithPassword({
email : user.email,
password : user.password
}, {
remember: "sessionOnly"
});

resetPassword issues in meteor

I sent enrollment email to the user and when he enters password and other details I'm trying to reset the password but it is throwing error
uncaught error extpected to find a document to change
As you can see in the mage
I've subscribed to the user record
my code
this.route('enroll', {
path: '/enroll-account/:token',
template: 'enroll_page',
onBeforeAction: function() {
Meteor.logout();
Session.set('_resetPasswordToken', this.params.token);
s = this.subscribe('enrolledUser', this.params.token).wait();
}
}),
After I'm displaying form and on the submit event
onSubmit: function(creds) {
var options = {
_id: Meteor.users.findOne()._id,
name: creds.name
}
var token=Session.get('_resetPasswordToken');
Meteor.call('updateUser', options, function(error, result) {
if(!error) {
Accounts.resetPassword(token, creds.password, function(error) {
if (error) {
toastr.error("Sorry we could not update your password. Please try again.");
return false;
}
else{
toastr.error("Logged In");
Router.go('/');
}
});
} else {
toastr.error("Sorry we could not update your password. Please try again.");
return false;
}
});
this.resetForm();
this.done();
return false;
}
Everything is working fine but resetpassword callback is not triggering and the above error is displaying in console.
my token is get deleted from the user record and I'm able to login using login form but
From the docs
Reset the password for a user using a token received in email. Logs the user in afterwards.
I'm not able to automatically login after resetting the password,above error is throwing
What am I missing here?
this.subscribe('enrolledUser', this.params.token).wait();
here you're subscribing using resetPassword token
when you call Accounts.resetPassword method the method will reset the password and delete the token from user record.
So your subscription is lost and there are no records available in client side to modify
(That is waht the error Expected to find a document to change)
Instead on first subscription save the user Id and subscribe to the user record using Id
so the subscription will not be lost
path: '/enroll-account/:token',
template: 'enroll_page',
onBeforeAction: function() {
Meteor.logout();
Session.set('_resetPasswordToken', this.params.token);
s = this.subscribe('enrolledUser', this.params.token).wait();
},
onAfterAction:function(){
if(this.ready()){
var userid=Meteor.users.findOne()._id;
Meteor.subscribe("userRecord",userid);
}
}
Alternatively, you could do something like as follows in your publication. This worked for me (but mine was a slightly more involved query than this).
Meteor.publish('enrolledUser', function (token) {
check(token, String);
return Meteor.users.find({
$or: [{
_id: this.userId
}, {
'services.password.reset.token': token
}]
});
});
From the docs, it says
Reset the password for a user using a token received in email. Logs the user in afterwards.
So basically, you have to subscribe to the logged in user after the fact as well. A little silly, but whatever.

How would I use Accounts.createUser in Meteor once, at first load, and then persist

I have thought of somehow using session, but that doesn't make much sense to me with a user that I always want to exist, and not be created over and over.
My issue is after a page refresh I get an error saying user already exists, I would like to know how to create a permanent user once.
This is in my server code.
Accounts.createUser({
username: "admin",
password: "password"
});
Try this.
Meteor.startup(function () {
if (Meteor.users.find().count() === 0) {
Accounts.createUser({
username: 'admin',
email: 'admin#admin.com',
password: 'password'
});
console.log('created user');
}
});

New user can't change password in Meteor

When I add a new user, and open the link in the received verification email, the new user is logged in but can't change password. When I go to "Change password", I leave "current password" blank, type a password and click "Change password" and get the error "Match failed".
The user is created with a Meteor.call from the client to the following method:
Meteor.methods({
createUser: function(user) {
var userID = Accounts.createUser({
username: user.username,
email: user.email,
profile: {
firstName: user.firstName,
lastName: user.lastName,
}
});
Accounts.sendVerificationEmail(userID);
}
});
I have the following settings for Accounts.config and Accounts.ui.config:
Accounts.ui.config({
passwordSignupFields: 'USERNAME_AND_EMAIL'
})
Accounts.config({
forbidClientAccountCreation: false,
sendVerificationEmail: true
})
Thanks :-)
Why do you leave current password blank?
When creating an account you must specify a password otherwise it is considered null.
Try passing in password: "" when creating it if you intend to change it this way.
I would recommend you write a method to change it if you want them to enter a password after they have verified their account. Something like this:
Meteor.methods('changeMyPassword':function(newPassword) {
Accounts.setPassword(this.userId, newPassword);
});
The intention of the account verification email is that you create the account with a specified password and verify it after you've created the account.
Example of how to use Accounts.createUser

Resources