Skip to content

Instantly share code, notes, and snippets.

@thebergamo
Last active December 11, 2015 01:57
Show Gist options
  • Save thebergamo/927a4a7b5b1606370313 to your computer and use it in GitHub Desktop.
Save thebergamo/927a4a7b5b1606370313 to your computer and use it in GitHub Desktop.
Hapi + Auth JWT
'use strict';
const Promise = require('bluebird');
const jwt = require('hapi-auth-jwt2');
const db = require('./database');
exports.register = (server, options, next) => {
server.register(jwt, registerAuth);
function registerAuth (err) {
if (err) { return next(err); }
server.auth.strategy('jwt', 'jwt', {
key: process.env.JWT || 'stubJWT',
validateFunc: validate,
verifyOptions: {algorithms: [ 'HS256' ]}
});
server.auth.default('jwt');
return next();
}
function validate (decoded, request, cb) {
const User = db.User;
return new Promise((resolve) => {
User.findAsync({_id: decoded.id})
.then((user) => {
if (!user) {
return cb(null, false);
}
return cb(null, true);
});
});
}
};
exports.register.attributes = {
name: 'auth-jwt',
version: '1.0.0'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment