首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sails.js水锁如何覆盖登录操作(如何编写自定义登录操作)

Sails.js水锁如何覆盖登录操作(如何编写自定义登录操作)
EN

Stack Overflow用户
提问于 2015-04-29 13:26:39
回答 1查看 779关注 0票数 4

我使用Sails.js和Waterlock。

  1. 在一个地方,我需要能够认证我的用户(比如说)通过电话号码,而不是电子邮件。
  2. 在任何地方,我应该只有一个唯一的密码,这将是唯一的身份验证字段。

我认为有可能用自定义的登录/注册操作覆盖登录/注册操作,但没有找到任何示例。如何做到这一点,请您向我提供任何示例(教程),为Sails.js/Waterlock登录操作定制实现操作?

请简单地描述一下解决方案,不幸的是,我并不是那么有经验去理解每一件事,仅仅是通过线索。提前谢谢。

EN

回答 1

Stack Overflow用户

发布于 2016-05-05 13:58:24

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

代码语言:javascript
复制
  /* -------------------------------- 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'});
      }
    });
  },
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29945189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档