From fc9b789ad86f8937e07df53b1557482cac295e03 Mon Sep 17 00:00:00 2001 From: Ben Ramey Date: Tue, 10 Jun 2014 19:42:23 -0500 Subject: [PATCH] Complete passport setup --- api/controllers/SessionController.js | 52 ++++++++++++++++++++++++++++ api/policies/isAuthenticated.js | 2 +- config/policies.js | 5 +++ config/routes.js | 11 +++--- 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 api/controllers/SessionController.js diff --git a/api/controllers/SessionController.js b/api/controllers/SessionController.js new file mode 100644 index 0000000..c32344a --- /dev/null +++ b/api/controllers/SessionController.js @@ -0,0 +1,52 @@ +/** + * SessionController + * + * @module :: Controller + * @description :: A set of functions called `actions`. + * + * Actions contain code telling Sails how to respond to a certain type of request. + * (i.e. do stuff, then send some JSON, show an HTML page, or redirect to another URL) + * + * You can configure the blueprint URLs which trigger these actions (`config/controllers.js`) + * and/or override them with custom routes (`config/routes.js`) + * + * NOTE: The code you write here supports both HTTP and Socket.io automatically. + * + * @docs :: http://sailsjs.org/#!documentation/controllers + */ +var passport = require('passport'); + +module.exports = { + + create: function(req,res) { + passport.authenticate('local', function(err, user, info) { + if(err || !user) { + res.redirect('/login'); + return; + } + req.logIn(user, function(err) { + if (err) { + res.redirect('/login'); + } + return res.redirect('/'); + }); + })(req, res); + }, + + destroy: function(req, res) { + req.logout(); + res.send('logout successful'); + }, + + /** + * Overrides for the settings in `config/controllers.js` + * (specific to SessionController) + */ + _config: { + blueprints: { + shortcuts: false, + rest: false + } + + +}; diff --git a/api/policies/isAuthenticated.js b/api/policies/isAuthenticated.js index d6b34e2..dc0e339 100644 --- a/api/policies/isAuthenticated.js +++ b/api/policies/isAuthenticated.js @@ -11,7 +11,7 @@ module.exports = function(req, res, next) { // User is allowed, proceed to the next policy, // or if this is the last policy, the controller - if (req.session.authenticated) { + if (req.isAuthenticated()) { return next(); } diff --git a/config/policies.js b/config/policies.js index ac02324..cfa201d 100644 --- a/config/policies.js +++ b/config/policies.js @@ -31,6 +31,11 @@ module.exports.policies = { 'find': true, 'create': true, 'update': true + }, + + SessionController: { + 'create': true, + 'destroy': 'isAuthenticated' } }; diff --git a/config/routes.js b/config/routes.js index e00cd59..3b37ff9 100644 --- a/config/routes.js +++ b/config/routes.js @@ -35,10 +35,13 @@ module.exports.routes = { '/': { view: 'home/index' }, - '/about': { - controller: 'staticPagesController', - action: 'about' - }, + + // static pages + 'get /about': 'StaticPagesController.about', + + // session + 'post /login': 'SessionController.create', + 'post /logout': 'SessionController.destroy', /* // But what if you want your home page to display