From 984c0647e6bc72f387ef9da79185ec7b05d0f61c Mon Sep 17 00:00:00 2001 From: lmarkus Date: Mon, 30 Dec 2013 21:48:40 -0800 Subject: [PATCH] Create login controller, template and 401 error page --- controllers/login.js | 37 ++++++++++++++++++++++++++++---- index.js | 1 - public/templates/errors/401.dust | 6 ++++++ public/templates/login.dust | 34 ++++++++++++++++++++++++++++- 4 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 public/templates/errors/401.dust diff --git a/controllers/login.js b/controllers/login.js index a657f7b..5bca586 100644 --- a/controllers/login.js +++ b/controllers/login.js @@ -1,7 +1,7 @@ 'use strict'; - -var LoginModel = require('../models/login'); +var LoginModel = require('../models/login'), + passport = require('passport'); module.exports = function (app) { @@ -9,10 +9,39 @@ module.exports = function (app) { var model = new LoginModel(); + /** + * Display the login page. We also want to display any error messages that result from a failed login attempt. + */ app.get('/login', function (req, res) { - + + //Include any error messages that come from the login process. + model.messages = req.flash('error'); res.render('login', model); - + }); + + /** + * Receive the login credentials and authenticate. + * Successful authentications will go to /profile or if the user was trying to access a secured resource, the URL + * that was originally requested. + * + * Failed authentications will go back to the login page with a helpful error message to be displayed. + */ + app.post('/login', function (req, res) { + + passport.authenticate('local', { + successRedirect: req.session.goingTo || '/profile', + failureRedirect: "/login", + failureFlash: true + })(req, res); + + }); + + /** + * Allow the users to log out + */ + app.get('/logout', function (req, res) { + req.logout(); + res.redirect('/'); }); }; diff --git a/index.js b/index.js index 7be32b2..0abfb2d 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,6 @@ var kraken = require('kraken-js'), app.configure = function configure(nconf, next) { // Async method run on startup. db.config(nconf.get('databaseConfig')); - //Tell passport to use our newly created local strategy for authentication passport.use(auth.localStrategy()); diff --git a/public/templates/errors/401.dust b/public/templates/errors/401.dust new file mode 100644 index 0000000..a67e78a --- /dev/null +++ b/public/templates/errors/401.dust @@ -0,0 +1,6 @@ +{>"layouts/master" /} + +{{@pre type="content" key="header"/} +

{@pre type="content" key="description"/}

+{/body} diff --git a/public/templates/login.dust b/public/templates/login.dust index 2e49d77..10a4856 100644 --- a/public/templates/login.dust +++ b/public/templates/login.dust @@ -1,5 +1,37 @@ {>"layouts/master" /} {{@pre type="content" key="greeting"/} +
+
+
+

Login

+ {?messages} +
    + {#messages} +
  • {.}
  • + {/messages} + +
+ {/messages} + + + + + + + + + + + + + +
+ + +
+ +
+
+
{/body}