Skip to content
Myr

app.js

This file

Path

examples/auth/app.js

126 lines · 3.1 KiB · JavaScript

Last changed by

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

History of this file

Take it

The walk stopped after 200 commits: lines older than 7294806 point at it.

try npm publishConfigTJ Holowaychukbefore adoption7294806

lines 1–6why, one step back
1
2/**3 * Module dependencies.4 */56var express = require('../../lib/express')

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

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

try npm publishConfigTJ Holowaychukbefore adoption7294806

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

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

line 11why, one step back
11// config

try npm publishConfigTJ Holowaychukbefore adoption7294806

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

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

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

try npm publishConfigTJ Holowaychukbefore adoption7294806

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

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

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

try npm publishConfigTJ Holowaychukbefore adoption7294806

lines 25–31why, one step back
25 var err = req.session.error
26 , msg = req.session.success;27 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>';

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

lines 32–33why, one step back
32 next();
33});

try npm publishConfigTJ Holowaychukbefore adoption7294806

line 34why, one step back
34

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

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

try npm publishConfigTJ Holowaychukbefore adoption7294806

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

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

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

try npm publishConfigTJ Holowaychukbefore adoption7294806

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

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

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

try npm publishConfigTJ Holowaychukbefore adoption7294806

line 43why, one step back
43

update auth example to utilize cores pbkdf2TJ Holowaychukbefore adoption26fab2a

lines 44–51why, one step back
44hash('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});5051

try npm publishConfigTJ Holowaychukbefore adoption7294806

lines 52–53why, one step back
52// Authenticate using our plain-object database of doom!
53function authenticate(name, pass, fn) {

less verbose example apps when testingTJ Holowaychukbefore adoption3016735

line 54why, one step back
54 if (!module.parent) console.log('authenticating %s:%s', name, pass);

try npm publishConfigTJ Holowaychukbefore adoption7294806

lines 55–60why, one step back
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 })

try npm publishConfigTJ Holowaychukbefore adoption7294806

lines 66–126why, 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){82 res.send('Wahoo! restricted area');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 if (req.session.user) {95 req.session.success = 'Authenticated as ' + req.session.user.name96 + ' click to <a href="/logout">logout</a>. '97 + ' You may now access <a href="/restricted">/restricted</a>.';98 }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 in106 // to prevent fixation 107 req.session.regenerate(function(){108 // Store the user's primary key 109 // in the session store to be retrieved,110 // or in this case the entire user object111 req.session.user = user;112 res.redirect('back');113 });114 } else {115 req.session.error = 'Authentication failed, please check your '116 + ' username and password.'117 + ' (use "tj" and "foobar")';118 res.redirect('login');119 }120 });121});122123if (!module.parent) {124 app.listen(3000);125 console.log('Express started on port 3000');126}