Skip to content

Commit

Permalink
Create login controller, template and 401 error page
Browse files Browse the repository at this point in the history
  • Loading branch information
lmarkus committed Dec 31, 2013
1 parent bb0ee06 commit 984c064
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
37 changes: 33 additions & 4 deletions controllers/login.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
'use strict';


var LoginModel = require('../models/login');
var LoginModel = require('../models/login'),
passport = require('passport');


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('/');
});

};
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
6 changes: 6 additions & 0 deletions public/templates/errors/401.dust
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{>"layouts/master" /}

{<body}
<h1>{@pre type="content" key="header"/}</h1>
<p>{@pre type="content" key="description"/}</p>
{/body}
34 changes: 33 additions & 1 deletion public/templates/login.dust
Original file line number Diff line number Diff line change
@@ -1,5 +1,37 @@
{>"layouts/master" /}

{<body}
<h1>{@pre type="content" key="greeting"/}</h1>
<div>
<form id="loginForm" method="post">
<fieldset>
<h2>Login</h2>
{?messages}
<ul>
{#messages}
<li>{.}</li>
{/messages}

</ul>
{/messages}
<table>
<tr>
<td><label for="username">Login: </label></td>
<td><input id="username" name="username" type="text"/></td>
</tr>
<tr>
<td><label for="password">Password: </label></td>
<td><input id="password" name="password" type="password"/></td>
</tr>
<tr>
<td>
<input type="submit" value="Login"/>
<input type="hidden" name="_csrf" value="{_csrf}"/>
</td>
<td></td>
</tr>
</table>

</fieldset>
</form>
</div>
{/body}

0 comments on commit 984c064

Please sign in to comment.