Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Created February 26, 2013 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikermcneil/5040752 to your computer and use it in GitHub Desktop.
Save mikermcneil/5040752 to your computer and use it in GitHub Desktop.
Simplistic login example for Sails
/*---------------------
:: Session
-> controller
---------------------*/
var SessionController = {
login: function(req, res) {
// Get password and username from request
var username = req.param('username');
var password = req.param('password');
// No username/password entered
if(!(username && password)) {
res.send("No username or password specified!",500);
// TODO: redirect, storing an error in the session
}
else {
// Lookup the username/password combination
User.find({
username: username,
password: password // TODO: hash the password first
}).done(function (err, user) {
// Login failed, incorrect username/password combination
if (err || !user) {
res.send("Invalid username and password combination!",500);
// TODO: redirect, storing an error in the session
}
// Login succeeded
if (user) {
req.session.authenticated = true;
req.session.User = user;
// Redirect to protected area
res.redirect('/dashboard');
}
});
}
}
};
module.exports = SessionController;
@frontage
Copy link

It would be great if you included the code that is along side this, like the policies, model, routes anything. Sure it's a great starting point but for a beginner it leaves for a lot of guessing.

Thanks for the example either way.

@hengkiardo
Copy link

Hi @mikermcneil i agree with @frontpage can you include all file to support for this gits ?

and i think you have to create some sample apps using Sails so the beginner with nodeJs can learn fast sails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment