क्या कोई तरीका है जिससे मैं jsonwebtoken
पैकेज verify
विधि को रूपांतरित कर सकूं ताकि यह इस तरह से काम करे?
const { err, decoded } = await jwtPromise(post.journey_token, 'test');
मैं इसे एक वादे में बदलने में कामयाब रहा हूं, लेकिन यह .then
का उपयोग करना चाहता है, हालांकि मैं इसे इस तरह से नहीं करना चाहता। वर्तमान में err
और decoded
अपरिभाषित हैं।
यहां पूरा कोड है, क्या यह किया जा सकता है?
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);
}
3 जवाब
आप जो चाहते हैं उसे वापस करने के लिए आप किसी भी एसिंक्रोनस फ़ंक्शन को कनवर्ट कर सकते हैं। आप इसे किसी अन्य फ़ंक्शन में लपेटकर ऐसा कर सकते हैं जो एक वादा लौटाता है, जो त्रुटि के मामले में अस्वीकार करने के बजाय हमेशा एक त्रुटि और परिणाम कुंजी दोनों वाले ऑब्जेक्ट को हल करता है।
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)
})
}
}
अपना खुद का verify
फ़ंक्शन बनाएं।
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 }));
}
सही ढंग से आउटपुट सत्यापित करें:
{ err: null, decoded: { foo: 'bar', iat: 1619006803 } }
विफल आउटपुट सत्यापित करें:
{
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
}
फ़ंक्शन को सत्यापित करने के लिए पारित कॉलबैक के रूप में गलती और डीकोड तीसरे पैरा में हैं:
jwt.verify(token, secretKey, (err, decoded) => {
console.log(err, decoded);
...
});
संबंधित सवाल
नए सवाल
node.js
Node.js एक घटना-आधारित, गैर-अवरोधक, अतुल्यकालिक I / O रनटाइम है जो Google के V8 जावास्क्रिप्ट इंजन और libuv लाइब्रेरी का उपयोग करता है। इसका उपयोग उन अनुप्रयोगों को विकसित करने के लिए किया जाता है जो क्लाइंट पर और साथ ही सर्वर साइड पर जावास्क्रिप्ट को चलाने की क्षमता का भारी उपयोग करते हैं और इसलिए कोड के पुन: प्रयोज्य और संदर्भ स्विचिंग की कमी से लाभान्वित होते हैं।