Created
December 17, 2013 00:38
-
-
Save therealplato/7997908 to your computer and use it in GitHub Desktop.
Bouncing a user back to their last place after authentication with Express 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Bouncing a user back to their last place after authentication with Express 3 | |
// To accompany http://stackoverflow.com/a/12443844/1380669 | |
// by therealplato | |
var express = require('express'); | |
var app = express(); | |
require('http').createServer(app).listen(3000 | |
, function(err){ | |
console.log(err || "Listening on 3000"); | |
}); | |
app.use(express.cookieParser()); | |
app.use(express.session({secret:'apapppxxllerjam,dfa654163210321'})); | |
app.use(express.bodyParser()); | |
app.use(express.logger('dev')); | |
app.use(app.router); | |
// If a user visits an auth page and we don't have .bounceTo in session, | |
// set it to: query['bounce'] || req.header('Referer') || '/' | |
app.get('/auth/*', function(req, res, next){ | |
if(!!req.session.bounceTo){ // already have a bounce destination | |
return next(); | |
} else { | |
if(req.query['bounce']){ | |
req.session.bounceTo = req.query['bounce']; | |
} else { // no explicit destination, use referer or homepage | |
req.session.bounceTo=req.header('Referer') || '/'; | |
} | |
return next(); | |
}; | |
}); | |
// continue to render a template or whatever. see bottom | |
// app.get('/auth/register', function(req, res, next){ ... }); | |
// the register page posts a form: | |
app.post('/auth/register/form', function(req, res, next){ | |
// you do stuff and either succeed or fail: | |
function createUser(req, callback){ | |
if(req.body.name.match(/^[a-zA-Z0-9]+$/)){ // e.g. valid user | |
callback(null, {name: req.body.name}); | |
} else { | |
callback({message:"Bad character"}); | |
}; | |
}; | |
createUser(req, function(err, user){ | |
if(err){ return res.redirect('/auth/failure') }; | |
// save the user to session or something | |
// with Express, it's `req.logIn(user);` to place user in req.user | |
res.redirect('/auth/success'); | |
}); | |
}); | |
// Redirect appropriately | |
app.get('/auth/success', function(req, res){ | |
var tmp = req.session.bounceTo; | |
delete req.session.bounceTo; | |
res.redirect(tmp); | |
}); | |
app.get('/auth/failure', function(req, res){ | |
res.redirect('/auth/register'); | |
}); | |
// Your login form can similarly redirect to /auth/success or /auth/failure | |
// example app routes: | |
app.get('/auth/register', function(req, res){ | |
res.send(200, | |
'<h1>/auth/register</h1>' | |
+'<form action="/auth/register/form" method="post">' | |
+'<p>Alphanumeric username only</p>' | |
+'<input type="text" name="name" />' | |
+'<input type="submit" value="Register!"/>' | |
+'</form>'); | |
}); | |
app.get('/', function(req, res){ | |
res.send(200, | |
'<h1>/</h1>' | |
+'<a href="/auth/register">/auth/register</a>' | |
+'<a href="/test1">/test1</a>' | |
+'<a href="/test2">/test2</a>' | |
+'<a href="/test3">/test3</a>' | |
) | |
}); | |
app.get('/test1', function(req, res){ res.send(200, '<h1>/test1</h1><a href="/auth/register">/auth/register</a>') }); | |
app.get('/test2', function(req, res){ res.send(200, '<h1>/test2</h1><a href="/auth/register?bounce=/test3">/auth/register?bounce=/test3</a>') }); | |
app.get('/test3', function(req, res){ res.send(200, '<h1>/test3</h1><a href="/auth/register">/auth/register</a>') }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment