# Username does not work for login, only email works

**URL:** <https://community.auth0.com/t/username-does-not-work-for-login-only-email-works/57693>\
**Category:** Get Help\
**Tags:** mongodb, username\
**Created:** [February 10, 2021, 6:44pm UTC](https://community.auth0.com/t/username-does-not-work-for-login-only-email-works/57693 "2021-02-10T18:44:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![raalrwai](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@raalrwai](https://community.auth0.com/u/raalrwai)\
**Post date:** [February 10, 2021, 6:44pm UTC](https://community.auth0.com/t/username-does-not-work-for-login-only-email-works/57693/1 "2021-02-10T18:44:30Z")

</div>

Hello,  
I have a customer mongodb connection. The connection successfully creates users in my mongodb cluster with create user and with require username, however when I attempt to login, I receive a profile not defined error if I sign in with username instead of email. When I use email instead of username the correct user is found.  
The only edition I’ve made to login or get user is to add my custom mongo db connection url. I haven’t altered the actual code at all for those scripts?  
I asked this question previously and did not figure out a solution,  
I am happy to send my account details for someone to take a closer look but please help me get this resolved I’ve been struggling with this,

Here is what the getUser custom function template for mongodb looks like, how should I alter this to get the user based on username?

function getByEmail(email, callback) {  
const MongoClient = require(‘mongodb@3.1.4’).MongoClient;  
const client = new MongoClient(‘mongodb+srv://mern:’+configuration.MONGO\_PASSWORD+’@cluster0-2uci2.mongodb.net/?retryWrites=true&w=majority’);

client.connect(function (err) {  
if (err) return callback(err);

```auto
const db = client.db('test');
const users = db.collection('users');

users.findOne({ email: email }, function (err, user) {
  client.close();

  if (err) return callback(err);
  if (!user) return callback(null, null);

  return callback(null, {
    user_id: user._id.toString(),
    nickname: user.nickname,
    email: user.email
  });
});

```

});  
}

---

<div class="post-metadata">

**Author:** ![sgo](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/sgo/32/13789_2.png) [@sgo](https://community.auth0.com/u/sgo)\
**Post date:** [February 27, 2021, 11:24am UTC](https://community.auth0.com/t/username-does-not-work-for-login-only-email-works/57693/2 "2021-02-27T11:24:20Z")

</div>

This looks to be a duplicate post, please check my response here: [Username does not work for custom mongodb connection](https://community.auth0.com/t/username-does-not-work-for-custom-mongodb-connection/57637/)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 14, 2021, 11:24am UTC](https://community.auth0.com/t/username-does-not-work-for-login-only-email-works/57693/3 "2021-03-14T11:24:56Z")

</div>

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.
