Added authentication example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
very raw and minimal but gives people and idea
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 334e6cc · Browse files · 3 files·+91 added−3 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+91 added−3 removed
- other:+91 added−3 removed
Provenance
- Commit
3876a56- Tree
41368a6- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
334e6cc
3 files changed+91 added−3 removed
modified:examples/auth/views/layout.ejs+6 added
5 unmodified lines6 body {7 padding: 50px;8 font: 13px Helvetica, Arial, sans-serif;9 }10+ .error {11+ color: red12+ }13+ .success {14+ color: green;15+ }16 </style>17 </head>18 <body>19 <%- body %>2 unmodified linesmodified:examples/auth/views/login.ejs+3 added-2 removed
1 <h1>Login</h1>2+<%- message %>3 <form method="post" action="/login">4 <p>5 <label>Username:</label>5- <input type="text" name="user[name]">6+ <input type="text" name="username">7 </p>8 <p>9 <label>Password:</label>9- <input type="text" name="user[pass]">10+ <input type="text" name="password">11 </p>12 <p>13 <input type="submit" value="Login">14 </p>1 unmodified linemodified:examples/auth/app.js+82 added-1 removed
123456678910112 unmodified lines1415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
/** * Module dependencies. */
var express = require('../../lib/express');var express = require('../../lib/express'), crypto = require('crypto');
var app = express.createServer();
app.set('views', __dirname + '/views');2 unmodified linesapp.use(express.bodyDecoder());app.use(express.cookieDecoder());app.use(express.session());
// Message helper, ideally we would use req.flash()// however this is more light-weight for an example
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
var users = { tj: { name: 'tj', salt: 'randomly-generated-salt', pass: md5('foobar' + 'randomly-generated-salt') }};
// Used to generate a hash of the plain-text password + salt
function md5(str) { return crypto.createHash('md5').update(str).digest('hex');}
// Authenticate using our plain-object database of doom!
function authenticate(name, pass, fn) { var user = users[name]; // query the db for the given username if (!user) return fn(new Error('cannot find user')); // apply the same algorithm to the POSTed password, applying // the md5 against the pass / salt, if there is a match we // found the user if (user.pass == md5(pass + user.salt)) return fn(null, user); // Otherwise password is invalid fn(new Error('invalid password'));}
app.get('/', function(req, res){ res.redirect('/login');});
app.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'); });});
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>.'; } res.render('login');});
app.post('/login', function(req, res){ authenticate(req.body.username, req.body.password, function(err, user){ if (user) { // Regenerate session when signing in // to prevent fixation req.session.regenerate(function(){ // 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; }); } else { req.session.error = 'Authentication failed, please check your ' + ' username and password.' + ' (use "tj" and "foobar")'; } res.redirect('back'); });});
app.listen(3000);console.log('Express started on port 3000');No newline at end of fileNo newline at end of file