Skip to content
Myr

Merge branch 'auth-example'

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parents 8b5773c, 3876a56 · Browse files · 3 files+139 added−0 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+139 added−0 removed

  • other:+139 added−0 removed

3 files+139 added−0 removed

added:examples/auth/views/layout.ejs+21 added
1<!DOCTYPE html>2<html>3 <head>4 <title>Authentication Example</title>5 <style>6 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 %>20 </body>21</html>
added:examples/auth/views/login.ejs+15 added
1<h1>Login</h1>2<%- message %>3<form method="post" action="/login">4 <p>5 <label>Username:</label>6 <input type="text" name="username">7 </p>8 <p>9 <label>Password:</label>10 <input type="text" name="password">11 </p>12 <p>13 <input type="submit" value="Login">14 </p>15</form>
added:examples/auth/app.js+103 added
12/**3 * Module dependencies.4 */56var express = require('../../lib/express'),7 crypto = require('crypto');89var app = express.createServer();1011app.set('views', __dirname + '/views');12app.set('view engine', 'ejs');1314app.use(express.bodyDecoder());15app.use(express.cookieDecoder());16app.use(express.session());1718// Message helper, ideally we would use req.flash()19// however this is more light-weight for an example2021app.dynamicHelpers({22 message: function(req){23 var err = req.session.error,24 msg = req.session.success;25 delete req.session.error;26 delete req.session.success;27 if (err) return '<p class="msg error">' + err + '</p>';28 if (msg) return '<p class="msg success">' + msg + '</p>';29 }30});3132// Generate a salt for the user to prevent rainbow table attacks3334var users = {35 tj: {36 name: 'tj',37 salt: 'randomly-generated-salt',38 pass: md5('foobar' + 'randomly-generated-salt')39 }40};4142// Used to generate a hash of the plain-text password + salt4344function md5(str) {45 return crypto.createHash('md5').update(str).digest('hex');46}4748// Authenticate using our plain-object database of doom!4950function authenticate(name, pass, fn) {51 var user = users[name];52 // query the db for the given username53 if (!user) return fn(new Error('cannot find user'));54 // apply the same algorithm to the POSTed password, applying55 // the md5 against the pass / salt, if there is a match we56 // found the user57 if (user.pass == md5(pass + user.salt)) return fn(null, user);58 // Otherwise password is invalid59 fn(new Error('invalid password'));60}6162app.get('/', function(req, res){63 res.redirect('/login');64});6566app.get('/logout', function(req, res){67 // destroy the user's session to log them out68 // will be re-created next request69 req.session.destroy(function(){70 res.redirect('home');71 });72});7374app.get('/login', function(req, res){75 if (req.session.user) {76 req.session.success = 'Authenticated as ' + req.session.user.name77 + ' click to <a href="/logout">logout</a>.';78 }79 res.render('login');80});8182app.post('/login', function(req, res){83 authenticate(req.body.username, req.body.password, function(err, user){84 if (user) {85 // Regenerate session when signing in86 // to prevent fixation 87 req.session.regenerate(function(){88 // Store the user's primary key 89 // in the session store to be retrieved,90 // or in this case the entire user object91 req.session.user = user;92 });93 } else {94 req.session.error = 'Authentication failed, please check your '95 + ' username and password.'96 + ' (use "tj" and "foobar")';97 }98 res.redirect('back');99 });100});101102app.listen(3000);103console.log('Express started on port 3000');