Skip to content

Instantly share code, notes, and snippets.

@gregfroese
Created August 12, 2014 14:23
Show Gist options
  • Save gregfroese/febcc484aa962bf8c88c to your computer and use it in GitHub Desktop.
Save gregfroese/febcc484aa962bf8c88c to your computer and use it in GitHub Desktop.
Protecting routes with authentication in Express
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');
var jwtauth = require('../../../jwtauth.js');
router.get('/', [bodyParser(), jwtauth], function(req, res) {
//jwtauth is run automatically and will populate req.user if there is a valid token supplied
if(!req.user) {
res.status(401).send({error: "Unauthorized access"});
return
}
//retrieve your data and pass it back
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment