Skip to content
Myr

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 file

Take it

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

expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb

lines 1–4why, one step back
1/**
2 * Module dependencies.3 */4

Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff

lines 5–8why, one step back
5var express = require('../..');
6var hash = require('./pass').hash;7var bodyParser = require('body-parser');8var session = require('express-session');

expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb

lines 9–18why, one step back
9
10var app = module.exports = express();1112// config1314app.set('view engine', 'ejs');15app.set('views', __dirname + '/views');1617// middleware18

deps: update example dependenciesDouglas Christopher Wilsonbefore adoption6ee9433

lines 19–20why, one step back
19app.use(bodyParser.urlencoded({ extended: false }));
20app.use(session({ secret: 'shhhh, very secret' }));

expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb

lines 21–24why, one step back
21
22// 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 back
25 var err = req.session.error;
26 var msg = req.session.success;

expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb

lines 27–65why, one step back
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>';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 back
66 });

expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb

lines 67–78why, one step back
67}
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 back
79 res.redirect('/login');

expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb

lines 80–101why, one step back
80});
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 in

Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff

line 102why, one step back
102 // to prevent fixation

expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb

line 103why, one step back
103 req.session.regenerate(function(){

Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff

line 104why, one step back
104 // Store the user's primary key

expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb

lines 105–116why, one step back
105 // 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 back
117 res.redirect('/login');

expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb

lines 118–121why, one step back
118 }
119 });120});121

Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d

line 122why, one step back
122/* istanbul ignore next */

expose connect.middleware using Object.getOwnPropertyDescriptor()Jonathan Ongbefore adoption2e68ddb

lines 123–126why, one step back
123if (!module.parent) {
124 app.listen(3000);125 console.log('Express started on port 3000');126}