Skip to content

Instantly share code, notes, and snippets.

@ricexen
Created July 1, 2019 22:47
Show Gist options
  • Save ricexen/1095f4c82119b3824d2c6e9be5dbf3ae to your computer and use it in GitHub Desktop.
Save ricexen/1095f4c82119b3824d2c6e9be5dbf3ae to your computer and use it in GitHub Desktop.
const jwt = require('jsonwebtoken');
const authorization = require('./authorization');
const { session: { secret } } = require('../config');
const sign = (payload) => {
return jwt.sign(payload, secret);
}
const decodeToken = (token) => {
return jwt.verify(token, secret);
}
const verify = (req, tokenType = 'Bearer') => {
const auth = authorization.get(req, tokenType);
const { value: token } = auth;
let { type } = auth;
tokenType = `${tokenType || ''}`;
type = `${type || ''}`;
if (!auth || type !== tokenType) {
return {};
}
return decodeToken(token);
}
module.exports = {
sign,
verify
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment