node.js – Sails.js Waterlock如何覆盖登录操作(如何编写自定义登录操作)

我使用Sails.js和Waterlock.

>在一个地方,我需要能够通过电话号码而不是电子邮件来验证我的用户(比如说).
>在其他地方,我只有一个唯一的密码,它将是唯一的身份验证字段.

我认为可能有一些可能用自定义覆盖登录/注册操作,但没有找到任何示例.
怎么可能呢,请你给我提供Sails.js / Waterlock登录动作的自定义实现动作的任何示例(教程)?

请简要描述一下解决方案,不幸的是,我没有经验只是通过线索来理解一切.提前致谢.

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

  /* -------------------------------- 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'});
      }
    });
  },
点赞