app.js
This file
Path
examples/auth/app.js
130 lines · 3.4 KiB · JavaScript
Last changed by
deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionca306ea
History of this fileTake it
The walk stopped after 200 commits: lines older than f41d09a point at it.
remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
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');remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
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
line 19why, one step back19app.use(bodyParser.urlencoded({ extended: false }));deps: update example dependenciesDouglas Christopher Wilsonbefore adoptionca306ea
lines 20–24why, one step back20app.use(session({21 resave: false, // don't save session if unmodified22 saveUninitialized: false, // don't create session until something stored23 secret: 'shhhh, very secret'24}));remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 25–28why, one step back2526// Session-persisted message middleware2728app.use(function(req, res, next){Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
lines 29–30why, one step back29 var err = req.session.error;30 var msg = req.session.success;remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 31–69why, one step back31 delete req.session.error;32 delete req.session.success;33 res.locals.message = '';34 if (err) res.locals.message = '<p class="msg error">' + err + '</p>';35 if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';36 next();37});3839// dummy database4041var users = {42 tj: { name: 'tj' }43};4445// when you create a user, generate a salt46// and hash the password ('foobar' is the pass here)4748hash('foobar', function(err, salt, hash){49 if (err) throw err;50 // store the salt & hash in the "db"51 users.tj.salt = salt;52 users.tj.hash = hash;53});545556// Authenticate using our plain-object database of doom!5758function authenticate(name, pass, fn) {59 if (!module.parent) console.log('authenticating %s:%s', name, pass);60 var user = users[name];61 // query the db for the given username62 if (!user) return fn(new Error('cannot find user'));63 // apply the same algorithm to the POSTed password, applying64 // the hash against the pass / salt, if there is a match we65 // found the user66 hash(pass, user.salt, function(err, hash){67 if (err) return fn(err);68 if (hash == user.hash) return fn(null, user);69 fn(new Error('invalid password'));Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
line 70why, one step back70 });remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 71–82why, one step back71}7273function restrict(req, res, next) {74 if (req.session.user) {75 next();76 } else {77 req.session.error = 'Access denied!';78 res.redirect('/login');79 }80}8182app.get('/', function(req, res){Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 83why, one step back83 res.redirect('/login');remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 84–105why, one step back84});8586app.get('/restricted', restrict, function(req, res){87 res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');88});8990app.get('/logout', function(req, res){91 // destroy the user's session to log them out92 // will be re-created next request93 req.session.destroy(function(){94 res.redirect('/');95 });96});9798app.get('/login', function(req, res){99 res.render('login');100});101102app.post('/login', function(req, res){103 authenticate(req.body.username, req.body.password, function(err, user){104 if (user) {105 // Regenerate session when signing inMerge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
line 106why, one step back106 // to prevent fixationremove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
line 107why, one step back107 req.session.regenerate(function(){Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
line 108why, one step back108 // Store the user's primary keyremove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 109–120why, one step back109 // in the session store to be retrieved,110 // or in this case the entire user object111 req.session.user = user;112 req.session.success = 'Authenticated as ' + user.name113 + ' click to <a href="/logout">logout</a>. '114 + ' You may now access <a href="/restricted">/restricted</a>.';115 res.redirect('back');116 });117 } else {118 req.session.error = 'Authentication failed, please check your '119 + ' username and password.'120 + ' (use "tj" and "foobar")';Merge tag '3.7.0'Douglas Christopher Wilsonbefore adoption097cd0c
line 121why, one step back121 res.redirect('/login');remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 122–125why, one step back122 }123 });124});125Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 126why, one step back126/* istanbul ignore next */remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 127–130why, one step back127if (!module.parent) {128 app.listen(3000);129 console.log('Express started on port 3000');130}