Not able to login - User has no password set - meteor

I don't want to use accounts-ui or accounts-ui-unstyled, only accounts-password, thus I decided to use the Accounts and Meteor methods.
I create a user with the following code (client):
Accounts.createUser({
email: email,
emails: [{address: email, verified: false}],
password: password,
profile: {
name: name
}
}, function(err) { ... });
which seems to be working fine, however when I try to create do a login with this code (client):
Meteor.loginWithPassword(username, password, function(err){
console.error(err);
});
Doesn't matter if the password is correct or not it always replies: Error: User has no password set [403].
Meteor version: 1.4.2.3

Related

How to add extra attributes to users collection?

I am using Accounts.createUser to add new users to the database, but the problem is that, not all the attributes are added.
Here is my code to add new user:
import {Accounts} from 'meteor/accounts-base';
Template.addingUser.events({
'submit #addUser': function (e, t) {
e.preventDefault();
Session.set('name', t.find('#name').value);
Session.set('email', t.find('#email').value);
Session.set('telephoneOffice', t.find('#telephoneOffice').value);
Session.set('telephoneHouse', t.find('#telephoneHouse').value);
Session.set('salary', t.find('#salary').value);
let userId = Accounts.createUser({
username: Session.get('name'),
password: "123456",
email: Session.get('email'),
telephoneOffice: Session.get('telephoneOffice'),
telephoneHouse: Session.get('telephoneHouse'),
employeeSalary: Session.get('salary'),
annualLeave: 14
}, function (err) {
if (err)
console.log(err);
else
console.log('It worked...');
});
Accounts.sendEnrollmentEmail(userId);
}
});
only the name, email, and password are added.
How do I include the other information as well such as telephoneOffice?
You need to pass the extra data inside the profile object.
Accounts.createUser({
username: Session.get('name'),
password: "123456",
email: Session.get('email'),
profile: {
telephoneOffice: Session.get('telephoneOffice'),
telephoneHouse: Session.get('telephoneHouse'),
employeeSalary: Session.get('salary'),
annualLeave: 14
}
...
Accounts.createUser does not accept custom arguments beyond username, email, password, and profile. The default functionality for passing custom user info is to pass those fields such as telephoneOffice as part of the profile object, which is copied to user.profile in the document inserted to the user collection.
For example:
let userId = Accounts.createUser({
username: Session.get('name'),
password: "123456",
email: Session.get('email'),
profile: {
telephoneOffice: Session.get('telephoneOffice'),
telephoneHouse: Session.get('telephoneHouse'),
employeeSalary: Session.get('salary'),
annualLeave: 14
}
});
Note that the user.profile fields are by default modifiable by users. So it's there by legacy, but Meteor actually recommends avoiding using it for storage.
If you want those fields to be on user instead of user.profile, What you can do is to pass your custom params on the profile object as above, and then override the default behavior using Accounts.onCreateUser. Something like this:
Accounts.onCreateUser(function(options, user) {
if (options.profile)
_.extend(user, options.profile);
return user;
});
See more info here: https://guide.meteor.com/accounts.html#custom-user-data

No response from Jquery validation when creating Meteor account

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");
}
});

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"
});

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