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 d9aea70 point at it.
removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
lines 1–4why, one step back1/**2 * Module dependencies.3 */4Merge branch 'examples'TJ Holowaychukbefore adoptionc492cde
line 5why, one step back5var express = require('../..')update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a
line 6why, one step back6 , hash = require('./pass').hash;removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
lines 7–9why, one step back78var app = module.exports = express();9change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837
line 10why, one step back10// configremoved file heading commentsTJ Holowaychukbefore adoptiond9aea70
lines 11–14why, one step back1112app.set('view engine', 'ejs');13app.set('views', __dirname + '/views');14change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837
lines 15–20why, one step back15// middleware1617app.use(express.bodyParser());18app.use(express.cookieParser('shhhh, very secret'));19app.use(express.session());20removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
lines 21–22why, one step back21// Session-persisted message middleware22change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837
line 23why, one step back23app.use(function(req, res, next){removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
lines 24–30why, one step back24 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>';change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837
lines 31–32why, one step back31 next();32});removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
line 33why, one step back33update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a
lines 34–35why, one step back34// dummy database35removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
line 36why, one step back36var users = {update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a
line 37why, one step back37 tj: { name: 'tj' }removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
lines 38–39why, one step back38};39update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a
lines 40–41why, one step back40// when you create a user, generate a salt41// and hash the password ('foobar' is the pass here)removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
line 42why, one step back42update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a
lines 43–50why, one step back43hash('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});4950removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
line 51why, one step back51// Authenticate using our plain-object database of doom!Merge branch 'examples'TJ Holowaychukbefore adoptionc492cde
line 52why, one step back52removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
lines 53–60why, 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 userupdate auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a
lines 61–65why, one step back61 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 })removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
lines 66–81why, one step back66}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>');removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
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>.';removed file heading commentsTJ Holowaychukbefore adoptiond9aea70
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}