Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Last active December 14, 2015 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mafintosh/5089733 to your computer and use it in GitHub Desktop.
Save mafintosh/5089733 to your computer and use it in GitHub Desktop.
// auth.js
var auth = express();
var checkToken = function(req, res, next) {
var token = tokens.decode(req.cookies.token || req.query.token);
if (!token) return res.error(403);
req.user = token;
next();
};
// login does not have a token check
auth.post('/login', function(req, res) {
... login ...
res.cookie('token', tokens.encode(...))
});
// logout does not have a token check
auth.post('/logout', function(req, res) {
... logout ...
res.cookie('token', null);
});
// all other routes we mount on auth have one
auth.all(checkToken);
module.exports = auth;
// main.js
var auth = require('./auth');
var main = express();
auth.get('/my-auth-call', function(req, res) {
});
main.use(auth);
main.listen(80)
var main = express();
var checkToken = function(req, res, next) {
var token = tokens.decode(req.cookies.token || req.query.token);
if (!token) return res.error(403);
req.user = token;
next();
};
// login does not have a token check
main.post('/login', function(req, res) {
... login ...
res.cookie('token', tokens.encode(...))
});
// logout does not have a token check
main.post('/logout', function(req, res) {
... logout ...
res.cookie('token', null);
});
// all other routes we mount on auth have one
main.all(checkToken);
main.get('/my-auth-call', function(req, res) {
});
main.listen(80)
var app = express();
app.post('/login', function() {
...
});
app.post('/logout', function() {
...
});
var auth = express();
auth.all(checkToken);
auth.get('/my-auth-call', function(req, res) {
...
});
app.use(auth)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment