app.js
This file
Path
examples/auth/app.js
123 lines · 3.4 KiB · JavaScript
Last changed by
Silence while NODE_ENV=testRobert Sköldbefore adoption9bd3ad8
History of this fileTake it
The walk stopped after 200 commits: lines older than 7a24021 point at it.
more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 1–8why, one step back12/**3 * Module dependencies.4 */56var express = require('../../lib/express')7 , crypto = require('crypto');8Updated auth example for express 3.x. With a test.Robert Sköldbefore adoptiona8c7364
line 9why, one step back9var app = module.exports = express()more res.redirect() testsTj Holowaychukbefore adoption7a24021
line 10why, one step back10Updated auth example for express 3.x. With a test.Robert Sköldbefore adoptiona8c7364
lines 11–14why, one step back11app.use(express.bodyParser())12app.use(express.cookieParser('shhhh, very secret'))13app.use(express.session({ secret: 'keyboard cat' }))14more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 15–18why, one step back15app.set('views', __dirname + '/views');1617// Message helper, ideally we would use req.flash()18// however this is more light-weight for an exampleUpdated auth example for express 3.x. With a test.Robert Sköldbefore adoptiona8c7364
lines 19–27why, one step back19app.locals.use(function(req,res){20 var err = req.session.error21 , msg = req.session.success;22 delete req.session.error;23 delete req.session.success;24 res.locals.message = '';25 if (err) res.locals.message = '<p class="msg error">' + err + '</p>';26 if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';27})more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 28–69why, one step back2829// Generate a salt for the user to prevent rainbow table attacks30// for better security take a look at the bcrypt c++ addon:31// https://github.com/ncb000gt/node.bcrypt.js3233var users = {34 tj: {35 name: 'tj'36 , salt: 'randomly-generated-salt'37 , pass: hash('foobar', 'randomly-generated-salt')38 }39};4041// Used to generate a hash of the plain-text password + salt4243function hash(msg, key) {44 return crypto.createHmac('sha256', key).update(msg).digest('hex');45}46// Authenticate using our plain-object database of doom!4748function authenticate(name, pass, fn) {49 var user = users[name];50 // query the db for the given username51 if (!user) return fn(new Error('cannot find user'));52 // apply the same algorithm to the POSTed password, applying53 // the hash against the pass / salt, if there is a match we54 // found the user55 if (user.pass == hash(pass, user.salt)) return fn(null, user);56 // Otherwise password is invalid57 fn(new Error('invalid password'));58}5960function restrict(req, res, next) {61 if (req.session.user) {62 next();63 } else {64 req.session.error = 'Access denied!';65 res.redirect('/login');66 }67}6869function accessLogger(req, res, next) {Updated auth example for express 3.x. With a test.Robert Sköldbefore adoptiona8c7364
lines 70–71why, one step back70 if( process.env.NODE_ENV !== 'test' )71 console.log('/restricted accessed by %s', req.session.user.name);more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 72–75why, one step back72 next();73}7475app.get('/', function(req, res){Updated auth example for express 3.x. With a test.Robert Sköldbefore adoptiona8c7364
line 76why, one step back76 res.redirect('login');more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 77–86why, one step back77});7879app.get('/restricted', restrict, accessLogger, function(req, res){80 res.send('Wahoo! restricted area');81});8283app.get('/logout', function(req, res){84 // destroy the user's session to log them out85 // will be re-created next request86 req.session.destroy(function(){Updated auth example for express 3.x. With a test.Robert Sköldbefore adoptiona8c7364
line 87why, one step back87 res.redirect('/');more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 88–96why, one step back88 });89});9091app.get('/login', function(req, res){92 if (req.session.user) {93 req.session.success = 'Authenticated as ' + req.session.user.name94 + ' click to <a href="/logout">logout</a>. '95 + ' You may now access <a href="/restricted">/restricted</a>.';96 }Updated auth example for express 3.x. With a test.Robert Sköldbefore adoptiona8c7364
line 97why, one step back97 res.render('login.ejs');more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 98–109why, one step back98});99100app.post('/login', function(req, res){101 authenticate(req.body.username, req.body.password, function(err, user){102 if (user) {103 // Regenerate session when signing in104 // to prevent fixation 105 req.session.regenerate(function(){106 // Store the user's primary key 107 // in the session store to be retrieved,108 // or in this case the entire user object109 req.session.user = user;Updated auth example for express 3.x. With a test.Robert Sköldbefore adoptiona8c7364
line 110why, one step back110 res.redirect('restricted');more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 111–115why, one step back111 });112 } else {113 req.session.error = 'Authentication failed, please check your '114 + ' username and password.'115 + ' (use "tj" and "foobar")';Updated auth example for express 3.x. With a test.Robert Sköldbefore adoptiona8c7364
line 116why, one step back116 res.redirect('login');more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 117–121why, one step back117 }118 });119});120121app.listen(3000);Silence while NODE_ENV=testRobert Sköldbefore adoption9bd3ad8
lines 122–123why, one step back122if( process.env.NODE_ENV !== 'test' )123 console.log('Express started on port 3000');