Skip to content
Myr

app.js

This file

Path

examples/auth/app.js

124 lines · 3.2 KiB · JavaScript

Last changed by

Not known.

History of this file

Take it

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

Fix req.protocol for proxy-direct connectionsAshley Strebbefore adoptiona28b7a8

lines 1–124why, one step back
1/**
2 * Module dependencies.3 */45var express = require('../..')6 , hash = require('./pass').hash;78var app = module.exports = express();910// config1112app.set('view engine', 'ejs');13app.set('views', __dirname + '/views');1415// middleware1617app.use(express.urlencoded({ extended: false }));18app.use(express.session({ secret: 'shhhh, very secret' }));1920// Session-persisted message middleware2122app.use(function(req, res, next){23 var err = req.session.error24 , msg = req.session.success;25 delete req.session.error;26 delete req.session.success;27 res.locals.message = '';28 if (err) res.locals.message = '<p class="msg error">' + err + '</p>';29 if (msg) res.locals.message = '<p class="msg success">' + msg + '</p>';30 next();31});3233// dummy database3435var users = {36 tj: { name: 'tj' }37};3839// when you create a user, generate a salt40// and hash the password ('foobar' is the pass here)4142hash('foobar', function(err, salt, hash){43 if (err) throw err;44 // store the salt & hash in the "db"45 users.tj.salt = salt;46 users.tj.hash = hash;47});484950// Authenticate using our plain-object database of doom!5152function authenticate(name, pass, fn) {53 if (!module.parent) console.log('authenticating %s:%s', name, pass);54 var user = users[name];55 // query the db for the given username56 if (!user) return fn(new Error('cannot find user'));57 // apply the same algorithm to the POSTed password, applying58 // the hash against the pass / salt, if there is a match we59 // found the user60 hash(pass, user.salt, function(err, hash){61 if (err) return fn(err);62 if (hash == user.hash) return fn(null, user);63 fn(new Error('invalid password'));64 })65}6667function restrict(req, res, next) {68 if (req.session.user) {69 next();70 } else {71 req.session.error = 'Access denied!';72 res.redirect('/login');73 }74}7576app.get('/', function(req, res){77 res.redirect('login');78});7980app.get('/restricted', restrict, function(req, res){81 res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');82});8384app.get('/logout', function(req, res){85 // destroy the user's session to log them out86 // will be re-created next request87 req.session.destroy(function(){88 res.redirect('/');89 });90});9192app.get('/login', function(req, res){93 res.render('login');94});9596app.post('/login', function(req, res){97 authenticate(req.body.username, req.body.password, function(err, user){98 if (user) {99 // Regenerate session when signing in100 // to prevent fixation 101 req.session.regenerate(function(){102 // Store the user's primary key 103 // in the session store to be retrieved,104 // or in this case the entire user object105 req.session.user = user;106 req.session.success = 'Authenticated as ' + user.name107 + ' click to <a href="/logout">logout</a>. '108 + ' You may now access <a href="/restricted">/restricted</a>.';109 res.redirect('back');110 });111 } else {112 req.session.error = 'Authentication failed, please check your '113 + ' username and password.'114 + ' (use "tj" and "foobar")';115 res.redirect('login');116 }117 });118});119120/* istanbul ignore next */121if (!module.parent) {122 app.listen(3000);123 console.log('Express started on port 3000');124}