sails.jswaterlock

Waterlocks authentication from server side form


I am having a problem with waterlock-local-auth. Basically I've been playing around with waterlock all day trying to figure out how to create a new user (with hashed password and all), and also how to authenticate the user from a form on a server side sails.js view. But have been completely unsuccessful. Below is the code in my LoginController that my login form is posting to. Any help will be greatly appreciated. Thanks!

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"});
    }
}

};


Solution

  • Ok, so basically I went with the solution posted here (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); 
                 }); 
         }); 
     } 
    

    });