app.js
This file
Path
examples/auth/app.js
124 lines · 3.1 KiB · JavaScript
Last changed by
Merge branch 'examples'TJ Holowaychukbefore adoptionc492cde
History of this fileTake it
The walk stopped after 200 commits: lines older than 8ac3e80 point at it.
Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 1–4why, one step back1/**2 * Module dependencies.3 */4Merge branch 'examples'TJ Holowaychukbefore adoptionc492cde
line 5why, one step back5var express = require('../..')Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 6–51why, one step back6 , hash = require('./pass').hash;78var app = module.exports = express();910// config1112app.set('view engine', 'ejs');13app.set('views', __dirname + '/views');1415// middleware1617app.use(express.bodyParser());18app.use(express.cookieParser('shhhh, very secret'));19app.use(express.session());2021// Session-persisted message middleware2223app.use(function(req, res, next){24 var err = req.session.error25 , msg = req.session.success;26 delete req.session.error;27 delete req.session.success;28 res.locals.message = '';29 if (err) res.locals.message = '<p class="msg error">' + err + '</p>';30 if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';31 next();32});3334// dummy database3536var users = {37 tj: { name: 'tj' }38};3940// when you create a user, generate a salt41// and hash the password ('foobar' is the pass here)4243hash('foobar', function(err, salt, hash){44 if (err) throw err;45 // store the salt & hash in the "db"46 users.tj.salt = salt;47 users.tj.hash = hash;48});495051// Authenticate using our plain-object database of doom!Merge branch 'examples'TJ Holowaychukbefore adoptionc492cde
line 52why, one step back52Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 53–81why, one step back53function authenticate(name, pass, fn) {54 if (!module.parent) console.log('authenticating %s:%s', name, pass);55 var user = users[name];56 // query the db for the given username57 if (!user) return fn(new Error('cannot find user'));58 // apply the same algorithm to the POSTed password, applying59 // the hash against the pass / salt, if there is a match we60 // found the user61 hash(pass, user.salt, function(err, hash){62 if (err) return fn(err);63 if (hash == user.hash) return fn(null, user);64 fn(new Error('invalid password'));65 })66}6768function restrict(req, res, next) {69 if (req.session.user) {70 next();71 } else {72 req.session.error = 'Access denied!';73 res.redirect('/login');74 }75}7677app.get('/', function(req, res){78 res.redirect('login');79});8081app.get('/restricted', restrict, function(req, res){Merge branch 'examples'TJ Holowaychukbefore adoptionc492cde
line 82why, one step back82 res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 83–106why, one step back83});8485app.get('/logout', function(req, res){86 // destroy the user's session to log them out87 // will be re-created next request88 req.session.destroy(function(){89 res.redirect('/');90 });91});9293app.get('/login', function(req, res){94 res.render('login');95});9697app.post('/login', function(req, res){98 authenticate(req.body.username, req.body.password, function(err, user){99 if (user) {100 // Regenerate session when signing in101 // to prevent fixation 102 req.session.regenerate(function(){103 // Store the user's primary key 104 // in the session store to be retrieved,105 // or in this case the entire user object106 req.session.user = user;Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoptioneeef763
lines 107–108why, one step back107 req.session.success = 'Authenticated as ' + user.name108 + ' click to <a href="/logout">logout</a>. 'Merge branch 'examples'TJ Holowaychukbefore adoptionc492cde
line 109why, one step back109 + ' You may now access <a href="/restricted">/restricted</a>.';Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption8ac3e80
lines 110–124why, one step back110 res.redirect('back');111 });112 } else {113 req.session.error = 'Authentication failed, please check your '114 + ' username and password.'115 + ' (use "tj" and "foobar")';116 res.redirect('login');117 }118 });119});120121if (!module.parent) {122 app.listen(3000);123 console.log('Express started on port 3000');124}