4

I use Sails.js and Waterlock.

  1. In one place, I need to be able to authenticate my user (say) by phone number instead email.
  2. In othe place, I shoud have just a unique pin code that will be the only authentication field.

I thought that there could be some possibility to override login/register action with a custom one, but did not find any example. How could it be done, can you please kindly provide me with any example(tutorial) of custom realization action for Sails.js/Waterlock login action?

Please describe solutions briefly, Unfortunately, I'm not so experienced to understand everything just by clues. Thanks in advance.

4

1 回答 1

2

我能够通过以下方式覆盖注册和登录操作:

  /* -------------------------------- Signup ---------------------------------*/
  /**
   * Adds a new user and logs in for the first time
   * Signup normally works out of box but in order to do custom validation and
   * add fields to user we had to add our own
   */
  signup: function( req, res){

    // Perform validation for data that will be used before we store
    var invalid = {};
    if( !UserService.validateUsername( req.param('username'), invalid ) ||
        !UserService.validatePassword( req.param('password'), invalid ) ) {
          return res.badRequest( invalid );
    }

    // Get the autentiacation parameters out
    var params = req.allParams();
    var auth = {
      email: params.email,
      password: params.password
    };

    // Remove password from data
    delete(params.password);

    // Create user and authentication
    User.create(params).exec(function(err,  user){
      if(err){
        return res.negotiate( err );
      }
      waterlock.engine.attachAuthToUser(auth, user, function(err, ua){
        if(err){
          res.json(err);
        }else{
          waterlock.cycle.loginSuccess(req, res, ua);
        }
      });
    });
  },



/* -------------------------------- Login ---------------------------------*/
  /**
   * Logs in a user
   */
  login: function( req, res ) {
    var params = req.allParams();
    var auth = {
      email: params.email,
      password: params.password
    };
    waterlock.engine.findAuth({ email: auth.email }, function(err, user){
      if(err){
        return res.json(err);
      }
      if (user) {
        if(bcrypt.compareSync(auth.password, user.auth.password)){
          waterlock.cycle.loginSuccess(req, res, user);
        }else{
          waterlock.cycle.loginFailure(req, res, user, {error: 'Invalid email or password'});
        }
      } else {
        //TODO redirect to register
        waterlock.cycle.loginFailure(req, res, null, {error: 'user not found'});
      }
    });
  },
于 2016-05-05T13:58:24.357 回答