Parse-server: Login with email or username

Created on 3 Jan 2017  路  15Comments  路  Source: parse-community/parse-server

I guess this is more a feature request than an issue.

It would be great to have by default or as a configuration the ability to make login work with username or email. As uniqueness is checked for both field, it may not imply that much change.

I think it should auto detect if email is used with an email regex. And then check the email/password combination. If the regex is not valid, then username/password combination should be checked.

I know there is a workaround for using email for login (putting email address in the username field and email field). But I don't think there is anything easy for accepting both at the same time.

Is there any plan to implement something like this ?

enhancement up for grabs

Most helpful comment

You should be able to either pass email or username to the JS login method, and it would work with the latest parse server version

All 15 comments

do you want to take a crack at it?

I'd love to ! But I would need a whole lot of explanations because I'm completely lost in the project. If you have time to explain things to me, I could try to dig into it. Why not a Skype ? I've been wanting to collaborate on Parse Server or Parse iOS SDK for a long time.

@AmbroiseCollon I'll plan something out before the end of the week, like a hangout with other contributors so we can onboard you and others correctly.

@flovilmart I'd be interested in getting a more in depth overview of the project as well if you don't mind another listening in!

let's sync up on Gitter https://gitter.im/ParsePlatform/Chat

Is there any news? :)

I just found this issue since I'm looking for a way to allow users to log in using username or email. If this hasn't been touched yet, I'd love to jump on it and possibly have this included in a future (next?) release.

@AmbroiseCollon, did you start working on this yet?

Thanks

I'm going to grab this issue as it doesn't seem to have been worked on yet (no update since February 13th) and I need it for my app. I'll submit a PR soon.

There's already 2 PR open for supporting that, with 2 approaches. Let's discuss pros/cons of those approaches on the PR's

Oh, good to know! Thanks for saving me the trouble. I'll take a look at the PRs.

Any updates?

I haven't yet looked at the PRs, but here's what I'm about to do:

  1. query users for an email address
  2. if user found with the email, grab user's username
  3. use that username behind scenes to auth with password

This is back of the napkin code, but should be nearly functional, if not just working.

var userQuery = new Parse.Query(Parse.User); userQuery.equalTo('email', '[email protected]'); userQuery.first().then(function(_user) { var username = _user.toJSON().username; Parse.User.logIn(username, password).then(function(_user) { console.info("Logged in via email"); }, function(_error) { console.error("There was an error", _error); }) }, function(_error) { console.error("There was an error", _error); });

Worked. Here's the code put into a function for reusability:
/**

  • Login with email
  • @param {String} _email
  • @param {String} _password
  • @param {Function} _success
  • @parma {Function} _error
    */
    function loginWithEmail(_email, _password, _success, _error) {
    var userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo('email', _email);
    userQuery.first().then(function(_user) {
    if (!user) {
    _error && _error({message: "No user found with with email address of: " + _email});
    }
    var username = _user.toJSON().username;
    Parse.User.logIn(username, password).then(function(_user) {
    console.info("Logged in via email");
    _success && _success(_user);
    }, function(_error) {
    console.error("There was an error logging in", _error);
    _error && _error(_error);
    })
    }, function(_error) {
    console.error("There was an error with the query", _error);
    _error && _error(_error);
    });
    }

Is there any update on this?

You should be able to either pass email or username to the JS login method, and it would work with the latest parse server version

Was this page helpful?
0 / 5 - 0 ratings