Skip to content

User model without email or without username #1137

Closed

Description

@bajtos

LoopBack should allow developers to configure the User model to use only email+password or only username+password for logging in.

See #791, #820 and "Pluggable authentication providers decoupled from the user model" in the Roadmap

Activity

  1. jarvisji commented on Apr 27, 2015

    @jarvisji

    Hello, could you please give a ETA for this feature will be supported?
    I get the same issue now, I need register/login by cellPhoneNumber + password.
    Thanks.

  2. raymondfeng commented on Apr 27, 2015

    @raymondfeng
    Member

    At this point, you can use username to hold cellPhoneNumber and remove the email requirement as follows in a boot script such as server/boot/user.js:

    module.exports = function(app) {
      delete app.models.User.validations.email;
    };
  3. jarvisji commented on Apr 28, 2015

    @jarvisji

    Thanks Raymond, this is a acceptable workaround.

  4. docoder commented on Jun 5, 2015

    @docoder

    but "login failed as the email has not been verified"

  5. devonatdomandtom commented on Aug 12, 2015

    @devonatdomandtom

    also I get The``User``instance is not valid. Details:emailis null (value: undefined)
    Ack nevermind, I was deleting the validation from the wrong class!

  6. gonzofish commented on Oct 2, 2015

    @gonzofish

    So I looked at the roadmap and noticed it had an (8) next to it...does this represent the effort to complete it? What is the likelihood of this getting done? When is the target for this getting done?

  7. bajtos commented on Oct 8, 2015

    @bajtos
    MemberAuthor

    @gonzofish I am afraid we don't have any target date for this.

  8. codyolsen commented on May 2, 2016

    @codyolsen
    Contributor

    +1

  9. yagobski commented on May 13, 2016

    @yagobski
    Member

    +1

  10. jonathanwoahn commented on May 19, 2016

    @jonathanwoahn

    +1

  11. codyolsen commented on May 25, 2016

    @codyolsen
    Contributor

    Please note that the workaround above by @raymondfeng will remove all validations from the user model for email, including uniqueness, presence, format, and any additional validations defined in common/models/user.js.

    Assuming that user inherits from built-in User, instead of deleting the validations in server/boot/user.js, delete the validations in common/models/user.js first and then you can add needed validations for email. In this case I re-add format and uniqueness as validators.

    In common/models/user.js:

    module.exports = function(user) {
      // Remove existing validations for email
      delete user.validations.email;
    
      // Adds email format validation
      // Note custom validator because validatesFormat with regex will return false on a null value
      var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
      user.validate('email', function (err) { if (!re.test(this.email) && this.email !== undefined) err(); }, {message: 'Email format is invalid'});
    
      // Adds email uniqueness validation
      user.validatesUniquenessOf('email', {message: 'Email already exists'});
    }
  12. jonathanwoahn commented on Jun 3, 2016

    @jonathanwoahn

    @codyolsen +1 - I was struggling with this today, and you just saved me. However, the format one still gives me an error if the email is null. Did this one work for you?

  13. codyolsen commented on Jun 3, 2016

    @codyolsen
    Contributor

    @jonathanwoahn Yeah it's working for me. If you have more troubles you might be able to find more specific help on the loopback gitter.

  14. SinvalMAO commented on Jul 13, 2016

    @SinvalMAO

    Tank you so much @raymondfeng, you save my day :), just me worry about leaving this User email and unique in the database for users not logarem with similar credentials.

  15. buzai commented on Jul 29, 2016

    @buzai

    In your built-in user model inside, a lot of places to use the mailbox this thing, so can not simply delete this thing. Delete will influence to reset the password, and even affect the secret key generation, I think we should provide an optional project, configuration into the phone number or e-mail address, user name is to make configuration options for unique personality, but need to choose a as a unique ID.
    Of course, if you replace the mailbox with a phone number, a lot of places need to change, first of all, you may not need to change the token, but the certification section has to make a lot of changes.

  16. dakotahNorth commented on Aug 24, 2016

    @dakotahNorth

    This is a great workaround ... and when I updated the versions, unfortunately this was no longer a work around so I rolled back to the versions that are part of this example.

    Is there a fix for the later packages?

     -    "loopback-component-passport": "^1.5.0",
     -    "passport": "^0.2.0",
     -    "passport-facebook": "^1.0.3",
     -    "passport-google-oauth": "^0.1.5", 
     +    "loopback-component-passport": "^2.1.0",
     +    "passport": "^0.3.2",
     +    "passport-facebook": "^2.1.1",
     +    "passport-google-oauth": "^1.0.0",
    
  17. amr-alamir commented on Jan 9, 2017

    @amr-alamir

    @codyolsen, thank you for this tip. You can also use isemail library, which is used by loopback at this line for email validation, as shown here:

    'use strict';
    
    var isEmail = require('isemail');
    
    module.exports = function(user) {
    
        delete user.validations.email;
    
        user.validate('email', function (err) { if ( this.email !== undefined && !isEmail(this.email)) err(); }, {message: 'Email format is invalid'});
    
        // Adds email uniqueness validation
        user.validatesUniquenessOf('email', {message: 'Email already exists'});  
    
    };
  18. ataft commented on Jun 30, 2017

    @ataft

    Raymond's workaround is fine for validation, but the built-in User model still has email required as discussed in issue #2125, which requires more workarounds.

  19. stale commented on Aug 29, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

  20. stale commented on Sep 12, 2017

    @stale

    This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

  21. uds214125 commented on Feb 28, 2018

    @uds214125

    what if i have custom method which have required` 'true' ?

    "methods": {
        "registration": {
          "accepts": [
            {
              "arg": "registrationId",
              "type": "string",
              "required": true,
              "description": "Registration collection id.",
              "http": {
                "source": "form"
              }
            }]
    }}
    

    then how to remove its required field.

  22. pookdeveloper commented on Dec 10, 2019

    @pookdeveloper

    Another way ?? i want to delete the field email in the table.
    Thankss

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions