Reputation: 4895
is there a way I can convert the jsonwebtoken
package verify
method so it works in this way?
const { err, decoded } = await jwtPromise(post.journey_token, 'test');
I have managed to convert it to a promise, but this want's to use .then
, however I don't want to do it this way. Currently err
and decoded
is undefined.
Here is the full code, can this be done?
import jwt from 'jsonwebtoken';
import util from 'util';
export const store: APIGatewayProxyHandler = async ({body}) => {
const post = JSON.parse(body);
const jwtPromise = util.promisify(jwt.verify);
if (post.journey_token) {
const { err, decoded } = await jwtPromise(post.journey_token, 'test');
console.log(decoded, err);
}
Upvotes: 0
Views: 229
Reputation: 3020
You can convert any asynchronous function to return what you want. You can do so by wrapping it in another function which returns a promise, which instead of rejecting in case of an error always resolves to an object containing both an error and a result key.
function promisify(asyncFunction) {
return function (...args_) {
return new Promise(function (resolve, reject) {
function cb(error, result) {
resolve({ error, result })
}
var args = [
...args_,
cb
]
asyncFunction(...args)
})
}
}
Upvotes: 1
Reputation: 102527
Make your own verify
function.
import jwt from 'jsonwebtoken';
import util from 'util';
const jwtPromise = util.promisify(jwt.verify);
const secret = 'shhhhh';
const wrongSecret = '123';
const token = jwt.sign({ foo: 'bar' }, secret);
export const store = async () => {
const r = await verify(token, secret);
console.log(r);
};
store();
function verify(token, secret) {
return jwtPromise(token, secret)
.then((decoded) => ({ err: null, decoded }))
.catch((err) => ({ err, decoded: null }));
}
Verify correctly output:
{ err: null, decoded: { foo: 'bar', iat: 1619006803 } }
Verify failed output:
{
err: JsonWebTokenError: invalid signature
at /Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/jsonwebtoken/verify.js:133:19
at getSecret (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/jsonwebtoken/verify.js:90:14)
at module.exports (/Users/dulin/workspace/github.com/mrdulin/expressjs-research/node_modules/jsonwebtoken/verify.js:94:10)
at internal/util.js:297:30
at new Promise (<anonymous>)
at internal/util.js:296:12
at /Users/dulin/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/67195388/index.ts:10:19
at Generator.next (<anonymous>)
at /Users/dulin/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/67195388/index.ts:8:71
at new Promise (<anonymous>),
decoded: null
}
Upvotes: 1
Reputation: 792
err and decoded are in the third param as a callback passded to verify function:
jwt.verify(token, secretKey, (err, decoded) => {
console.log(err, decoded);
...
});
Upvotes: -1