2015-04-29 81 views
4

我使用Sails.js和Waterlock。Sails.js Waterlock如何覆蓋登錄操作(如何編寫自定義登錄操作)

  1. 在一個地方,我需要能夠通過電話號碼而不是電子郵件來驗證我的用戶(比如說)。
  2. 在其他地方,我應該只有一個唯一的密碼,這將是唯一的驗證字段。

我認爲有可能會覆蓋登錄/註冊行爲與自定義的一個,但沒有找到任何示例。 如何做到這一點,可以請您提供任何Sails.js/Waterlock登錄操作的自定義實現操作示例(教程)嗎?

請簡要描述一下解決方案,不幸的是,我並不是那麼有經驗,只是通過線索來理解一切。提前致謝。

回答

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'}); 
     } 
    }); 
    },