Skip to content

Instantly share code, notes, and snippets.

@kriswill
Created February 3, 2015 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kriswill/6f27299f95d8e1fe86a1 to your computer and use it in GitHub Desktop.
Save kriswill/6f27299f95d8e1fe86a1 to your computer and use it in GitHub Desktop.
api/service
var jwt = require('jsonwebtoken')
module.exports = {
issue: function(payload) {
sails.log.silly(__filename + ':' + __line + ' [Service.Passport.deserializeUser() called]')
return jwt.sign(payload, sails.config.jwt.secret)
},
verify: function(token, next) {
return jwt.verify(token, sails.config.jwt.secret, {}, next)
},
getToken: function(req, next, throwError) {
var token = ''
if (req.headers && req.headers.authorization) {
var parts = req.headers.authorization.split(' ')
if (parts.length === 2) {
var scheme = parts[0]
var credentials = parts[1]
if (/^Bearer$/i.test(scheme)) {
token = credentials
}
} else if (throwError) {
throw new Error('Invalid authorization header format. Format is Authorization: Bearer [token]')
}
} else if (req.param('token')) {
token = req.param('token')
} else if (throwError) {
throw new Error('No authorization header was found')
}
return sails.services['token'].verify(token, next)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment