Meteor loginWithPassword not working with username

I have a user in database with following credentials:

{
    "_id": "zTHv8yqPSm3pmi4So",
    "emails": [{"address": "someemail@example.com", "verified": true}],
    "username": "someusername",
    "profile": {
        "firstName": "Example",
        "lastName": "User",
    }
}

When I try to login user with username it says user not found:

Meteor.loginWithPassword("someusername", "123456", function(error){ 

	console.log(error.reason); 
});

It works fine for email but not for username.

I want flexibility to login user with either of email or username

Maybe you use ‘@’ (at) in username ?
If is it, Meteor use email method to authenticate, so if you want to keep a ‘@’ in username you must have identical username and email, or don’t use a ‘@’ in username.

1 Like

Yes I use @ in usernames, is there no work around for that?

For keep username with ‘@’ and email address not equal ? (‘usern@me’ and ‘pippo@email.it’) ?

Please check my updated user object:

{
    "_id": "zTHv8yqPSm3pmi4So",
    "emails": [{"address": "someemail@example.com", "verified": true}],
    "username": "some@username",
    "profile": {
        "firstName": "Example",
        "lastName": "User",
    }
}

Ok, is clear, you can’t have a ‘@’ in username or you must have identical user and email address.
So:

{
    "_id": "zTHv8yqPSm3pmi4So",
    "emails": [{"address": "someemail@example.com", "verified": true}],
    "username": "someusername",
    "profile": {
        "firstName": "Example",
        "lastName": "User",
    }
}

or

{
    "_id": "zTHv8yqPSm3pmi4So",
    "emails": [{"address": "someemail@example.com", "verified": true}],
    "username": "someemail@example.com",
    "profile": {
        "firstName": "Example",
        "lastName": "User",
    }
}

That’s pretty odd don’t you think? What if someone wants to keep special characters in their usernames?

Yes, actually I didn’t understand it myself, but the code is like that:

/**
 * @summary Log the user in with a password.
 * @locus Client
 * @param {Object | String} selector
 *   Either a string interpreted as a username or an email; or an object with a
 *   single key: `email`, `username` or `id`. Username or email match in a case
 *   insensitive manner.
 * @param {String} password The user's password.
 * @param {Function} [callback] Optional callback.
 *   Called with no arguments on success, or with a single `Error` argument
 *   on failure.
 * @importFromPackage meteor
 */
Meteor.loginWithPassword = (selector, password, callback) => {
  if (typeof selector === 'string')
    if (!selector.includes('@'))
      selector = {username: selector};
    else
      selector = {email: selector};
[cut]

https://github.com/meteor/meteor/blob/devel/packages/accounts-password/password_client.js

Maybe somebody with more experience can explain it to us…