app.js
This file
Path
examples/auth/app.js
126 lines · 3.2 KiB · JavaScript
Last changed by
deps: update example dependenciesDouglas Christopher Wilsonbefore adoption6ee9433
History of this fileTake it
The walk stopped after 200 commits: lines older than 34c83d7 point at it.
3.4.7Jonathan Ongbefore adoption34c83d7
lines 1–4why, one step back1/**2 * Module dependencies.3 */4Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
lines 5–8why, one step back5var express = require('../..');6var hash = require('./pass').hash;7var bodyParser = require('body-parser');8var session = require('express-session');3.4.7Jonathan Ongbefore adoption34c83d7
lines 9–18why, one step back910var app = module.exports = express();1112// config1314app.set('view engine', 'ejs');15app.set('views', __dirname + '/views');1617// middleware18deps: update example dependenciesDouglas Christopher Wilsonbefore adoption6ee9433
lines 19–20why, one step back19app.use(bodyParser.urlencoded({ extended: false }));20app.use(session({ secret: 'shhhh, very secret' }));3.4.7Jonathan Ongbefore adoption34c83d7
lines 21–24why, one step back2122// Session-persisted message middleware2324app.use(function(req, res, next){Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
lines 25–26why, one step back25 var err = req.session.error;26 var msg = req.session.success;3.4.7Jonathan Ongbefore adoption34c83d7
lines 27–65why, one step back27 delete req.session.error;28 delete req.session.success;29 res.locals.message = '';30 if (err) res.locals.message = '<p class="msg error">' + err + '</p>';31 if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';32 next();33});3435// dummy database3637var users = {38 tj: { name: 'tj' }39};4041// when you create a user, generate a salt42// and hash the password ('foobar' is the pass here)4344hash('foobar', function(err, salt, hash){45 if (err) throw err;46 // store the salt & hash in the "db"47 users.tj.salt = salt;48 users.tj.hash = hash;49});505152// Authenticate using our plain-object database of doom!5354function authenticate(name, pass, fn) {55 if (!module.parent) console.log('authenticating %s:%s', name, pass);56 var user = users[name];57 // query the db for the given username58 if (!user) return fn(new Error('cannot find user'));59 // apply the same algorithm to the POSTed password, applying60 // the hash against the pass / salt, if there is a match we61 // found the user62 hash(pass, user.salt, function(err, hash){63 if (err) return fn(err);64 if (hash == user.hash) return fn(null, user);65 fn(new Error('invalid password'));Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
line 66why, one step back66 });3.4.7Jonathan Ongbefore adoption34c83d7
lines 67–78why, one step back67}6869function restrict(req, res, next) {70 if (req.session.user) {71 next();72 } else {73 req.session.error = 'Access denied!';74 res.redirect('/login');75 }76}7778app.get('/', function(req, res){Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 79why, one step back79 res.redirect('/login');3.4.7Jonathan Ongbefore adoption34c83d7
lines 80–101why, one step back80});8182app.get('/restricted', restrict, function(req, res){83 res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');84});8586app.get('/logout', function(req, res){87 // destroy the user's session to log them out88 // will be re-created next request89 req.session.destroy(function(){90 res.redirect('/');91 });92});9394app.get('/login', function(req, res){95 res.render('login');96});9798app.post('/login', function(req, res){99 authenticate(req.body.username, req.body.password, function(err, user){100 if (user) {101 // Regenerate session when signing inMerge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
line 102why, one step back102 // to prevent fixation3.4.7Jonathan Ongbefore adoption34c83d7
line 103why, one step back103 req.session.regenerate(function(){Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
line 104why, one step back104 // Store the user's primary key3.4.7Jonathan Ongbefore adoption34c83d7
lines 105–116why, one step back105 // in the session store to be retrieved,106 // or in this case the entire user object107 req.session.user = user;108 req.session.success = 'Authenticated as ' + user.name109 + ' click to <a href="/logout">logout</a>. '110 + ' You may now access <a href="/restricted">/restricted</a>.';111 res.redirect('back');112 });113 } else {114 req.session.error = 'Authentication failed, please check your '115 + ' username and password.'116 + ' (use "tj" and "foobar")';Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 117why, one step back117 res.redirect('/login');3.4.7Jonathan Ongbefore adoption34c83d7
lines 118–121why, one step back118 }119 });120});121Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 122why, one step back122/* istanbul ignore next */3.4.7Jonathan Ongbefore adoption34c83d7
lines 123–126why, one step back123if (!module.parent) {124 app.listen(3000);125 console.log('Express started on port 3000');126}