从服务器端表单进行Waterlocks身份验证

时间:2015-09-06 05:43:31

标签: sails.js waterlock

我遇到了waterlock-local-auth的问题。基本上我整天都在玩水闸,试图弄清楚如何创建一个新用户(使用哈希密码和所有),以及如何从服务器端sails.js视图中的表单验证用户。但是已经完全没有成功。下面是我的登录表单发布到的LoginController中的代码。任何帮助将不胜感激。谢谢!

module.exports = {
login: function(req, res) {

    var isAuthenticated = function(){...} <-- Authenticated by waterlocks

    if(isAuthenticated) {
        res.view('home');        
    }
    else {
        res.view('login', {errorMessage: "Invalid username or password"});
    }
}

};

1 个答案:

答案 0 :(得分:0)

好的,基本上我选择了这里发布的解决方案(Sails.js Waterlock /auth/register causes error 500)。 ; 0)

module.exports = require('waterlock').waterlocked({ 


 // Endpoint for registering new users. Taken from: https://stackoverflow.com/questions/29944905/sails-js-waterlock-auth-register-causes-error-500/29949255#29949255 
 register: function (req, res) { 

     var params = req.params.all(), 
         def = waterlock.Auth.definition, 
         criteria = {}, 
         scopeKey = def.email !== undefined ? 'email' : 'username'; // Determines if the credentials are using username or emailaddess. 


     var attr = { password: params.password } 


     attr[scopeKey] = params[scopeKey]; 
     criteria[scopeKey] = attr[scopeKey]; 


     waterlock.engine.findAuth(criteria, function (err, user) { 
         if (user) 
             return res.badRequest("User already exists"); 
         else 
             waterlock.engine.findOrCreateAuth(criteria, attr, function (err, user) { 
                 if (err) 
                     return res.badRequest(err); 
                 delete user.password; 
                 return res.ok(user); 
             }); 
     }); 
 } 

});