app.js
This file
Path
examples/auth/app.js
127 lines · 3.2 KiB · JavaScript
Last changed by
remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128/** * Module dependencies. */
var express = require('../..') , hash = require('./pass').hash , bodyParser = require('body-parser') , cookieParser = require('cookie-parser') , session = require('express-session')
var app = module.exports = express();
// config
app.set('view engine', 'ejs');app.set('views', __dirname + '/views');
// middleware
app.use(bodyParser());app.use(cookieParser('shhhh, very secret'));app.use(session());
// Session-persisted message middleware
app.use(function(req, res, next){ var err = req.session.error , msg = req.session.success; delete req.session.error; delete req.session.success; res.locals.message = ''; if (err) res.locals.message = '<p class="msg error">' + err + '</p>'; if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>'; next();});
// dummy database
var users = { tj: { name: 'tj' }};
// when you create a user, generate a salt// and hash the password ('foobar' is the pass here)
hash('foobar', function(err, salt, hash){ if (err) throw err; // store the salt & hash in the "db" users.tj.salt = salt; users.tj.hash = hash;});
// Authenticate using our plain-object database of doom!
function authenticate(name, pass, fn) { if (!module.parent) console.log('authenticating %s:%s', name, pass); var user = users[name]; // query the db for the given username if (!user) return fn(new Error('cannot find user')); // apply the same algorithm to the POSTed password, applying // the hash against the pass / salt, if there is a match we // found the user hash(pass, user.salt, function(err, hash){ if (err) return fn(err); if (hash == user.hash) return fn(null, user); fn(new Error('invalid password')); })}
function restrict(req, res, next) { if (req.session.user) { next(); } else { req.session.error = 'Access denied!'; res.redirect('/login'); }}
app.get('/', function(req, res){ res.redirect('login');});
app.get('/restricted', restrict, function(req, res){ res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');});
app.get('/logout', function(req, res){ // destroy the user's session to log them out // will be re-created next request req.session.destroy(function(){ res.redirect('/'); });});
app.get('/login', function(req, res){ res.render('login');});
app.post('/login', function(req, res){ authenticate(req.body.username, req.body.password, function(err, user){ if (user) { // Regenerate session when signing in // to prevent fixation req.session.regenerate(function(){ // Store the user's primary key // in the session store to be retrieved, // or in this case the entire user object req.session.user = user; req.session.success = 'Authenticated as ' + user.name + ' click to <a href="/logout">logout</a>. ' + ' You may now access <a href="/restricted">/restricted</a>.'; res.redirect('back'); }); } else { req.session.error = 'Authentication failed, please check your ' + ' username and password.' + ' (use "tj" and "foobar")'; res.redirect('login'); } });});
if (!module.parent) { app.listen(3000); console.log('Express started on port 3000');}