Skip to content
Myr

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 file

Take 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 back
1/**
2 * Module dependencies.3 */4

Merge branch 'examples'TJ Holowaychukbefore adoptionc492cde

line 5why, one step back
5var express = require('../..')

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

line 6why, one step back
6 , hash = require('./pass').hash;

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

lines 7–9why, one step back
7
8var app = module.exports = express();9

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

line 10why, one step back
10// config

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

lines 11–14why, one step back
11
12app.set('view engine', 'ejs');13app.set('views', __dirname + '/views');14

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

lines 15–20why, one step back
15// middleware
1617app.use(express.bodyParser());18app.use(express.cookieParser('shhhh, very secret'));19app.use(express.session());20

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

lines 21–22why, one step back
21// Session-persisted message middleware
22

change res.sendfile() to use send() moduleTJ Holowaychukbefore adoptionab61837

line 23why, one step back
23app.use(function(req, res, next){

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

lines 24–30why, one step back
24 var err = req.session.error
25 , 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 back
31 next();
32});

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

line 33why, one step back
33

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

lines 34–35why, one step back
34// dummy database
35

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

line 36why, one step back
36var users = {

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

line 37why, one step back
37 tj: { name: 'tj' }

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

lines 38–39why, one step back
38};
39

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

lines 40–41why, one step back
40// when you create a user, generate a salt
41// and hash the password ('foobar' is the pass here)

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

line 42why, one step back
42

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

lines 43–50why, one step back
43hash('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});4950

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

line 51why, one step back
51// Authenticate using our plain-object database of doom!

Merge branch 'examples'TJ Holowaychukbefore adoptionc492cde

line 52why, one step back
52

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

lines 53–60why, one step back
53function 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 user

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

lines 61–65why, one step back
61 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 back
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 back
82 res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

lines 83–106why, one step back
83});
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 back
107 req.session.success = 'Authenticated as ' + user.name
108 + ' click to <a href="/logout">logout</a>. '

Merge branch 'examples'TJ Holowaychukbefore adoptionc492cde

line 109why, one step back
109 + ' You may now access <a href="/restricted">/restricted</a>.';

removed file heading commentsTJ Holowaychukbefore adoptiond9aea70

lines 110–124why, one step back
110 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}