Updated auth example for express 3.x. With a test.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Robert Sköld · committed by TJ Holowaychuk · parent 6368ab4 · Browse files · 3 files·+94 added−24 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+94 added−24 removed
- tests:+72 added−0 removed
- other:+22 added−24 removed
Provenance
- Commit
a8c7364- Tree
13562be- Author
Robert Sköld <robert@publicclass.se>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
6368ab4
3 files changed+94 added−24 removed
modified:examples/auth/app.js+21 added-23 removed
4 unmodified lines56789101112139101112131415161617181920212223242526272821222324252627282930312930313233 unmodified lines66676869707371727374757876777879802 unmodified lines838485868987888990919293949596999798991001014 unmodified lines1061071081091121101111121131141151181161171181191202 unmodified lines4 unmodified lines
var express = require('../../lib/express') , crypto = require('crypto');
var app = express.createServer( express.bodyParser() , express.cookieParser() , express.session({ secret: 'keyboard cat' }));var app = module.exports = express()
app.use(express.bodyParser())app.use(express.cookieParser('shhhh, very secret'))app.use(express.session({ secret: 'keyboard cat' }))
app.set('views', __dirname + '/views');app.set('view engine', 'ejs');
// Message helper, ideally we would use req.flash()// however this is more light-weight for an exampleapp.locals.use(function(req,res){ var err = req.session.error , msg = req.session.success; delete req.session.error; delete req.session.success; res.locals.message = ''; if (err) res.locals.message = '<p class="msg error">' + err + '</p>'; if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';})
app.dynamicHelpers({ message: function(req){ var err = req.session.error , msg = req.session.success; delete req.session.error; delete req.session.success; if (err) return '<p class="msg error">' + err + '</p>'; if (msg) return '<p class="msg success">' + msg + '</p>'; }});
// Generate a salt for the user to prevent rainbow table attacks// for better security take a look at the bcrypt c++ addon:// https://github.com/ncb000gt/node.bcrypt.js
33 unmodified lines }}
function accessLogger(req, res, next) { if( process.env.NODE_ENV !== 'test' ) console.log('/restricted accessed by %s', req.session.user.name); console.log('/restricted accessed by %s', req.session.user.name); next();}
app.get('/', function(req, res){ res.redirect('/login'); res.redirect('login');});
app.get('/restricted', restrict, accessLogger, function(req, res){ res.send('Wahoo! restricted area');2 unmodified linesapp.get('/logout', function(req, res){ // destroy the user's session to log them out // will be re-created next request req.session.destroy(function(){ res.redirect('home'); res.redirect('/'); });});
app.get('/login', function(req, res){ if (req.session.user) { req.session.success = 'Authenticated as ' + req.session.user.name + ' click to <a href="/logout">logout</a>. ' + ' You may now access <a href="/restricted">/restricted</a>.'; } res.render('login'); res.render('login.ejs');});
app.post('/login', function(req, res){ authenticate(req.body.username, req.body.password, function(err, user){4 unmodified lines // Store the user's primary key // in the session store to be retrieved, // or in this case the entire user object req.session.user = user; res.redirect('back'); res.redirect('restricted'); }); } else { req.session.error = 'Authentication failed, please check your ' + ' username and password.' + ' (use "tj" and "foobar")'; res.redirect('back'); res.redirect('login'); } });});
2 unmodified linesadded:test/acceptance/auth.js+72 added
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172var app = require('../../examples/auth/app') , request = require('../support/http') , should = require('should');
describe('examples', function(){ describe('auth', function(){ var cookie; it('should redirect to /login', function(done){ request(app) .get('/') .end(function(res){ res.statusCode.should.equal(302); res.headers.should.have.property('location').match(/\/login$/); done(); }) }) it('should be restricted', function(done){ request(app) .get('/restricted') .end(function(res){ res.statusCode.should.equal(302); res.headers.should.have.property('location').match(/\/login$/); done(); }) }) it('should fail to authenticate', function(done){ request(app) .post('/login') .write('&username=not-tj&password=foobar') .end(function(res){ res.statusCode.should.equal(302); res.headers.should.have.property('location').match(/\/login$/); done(); }) }) it('should authenticate', function(done){ request(app) .post('/login') .set('content-type','application/x-www-form-urlencoded') .write('&username=tj&password=foobar') .end(function(res){ res.statusCode.should.equal(302); res.headers.should.have.property('location').match(/\/restricted$/); cookie = res.headers['set-cookie'][0].split(';')[0]; cookie.should.match(/^connect.sid=/) done(); }) }) it('should not be restricted with an authenticated session cookie',function(done){ request(app) .get('/restricted') .set('cookie',cookie) .expect(200,done) }) it('should logout',function(done){ request(app) .get('/logout') .set('cookie',cookie) .end(function(res){ res.statusCode.should.equal(302); res.headers.should.not.have.property('set-cookie') done(); }) }) it('should be restricted again',function(done){ request(app) .get('/restricted') .set('cookie',cookie) .expect(302,done) }) })})No newline at end of filemodified:Makefile+1 added-1 removed
33 unmodified lines34 35 docclean:36 rm -f docs/*.{1,html}37 38-.PHONY: site test benchmark docs docclean test-acceptance38+.PHONY: site test benchmark docs docclean test-acceptance