Created
January 20, 2021 08:20
-
-
Save Karnak19/8f86d71db2517ff27676ac9a3f4441d0 to your computer and use it in GitHub Desktop.
Async JWT sign example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const asyncJwtVerify = (token, privateKey) => { | |
| return new Promise((resolve, reject) => { | |
| jwt.verify(token, privateKey, (err, decoded) => { | |
| if (err) { | |
| reject(err); | |
| } | |
| resolve(decoded); | |
| }); | |
| }); | |
| }; | |
| // Promise example | |
| asyncJwtVerify(token, PRIVATE_KEY) | |
| .then((decoded) => { | |
| console.log(decoded); | |
| // Do something when it works... | |
| }) | |
| .catch((error) => { | |
| console.error(error); | |
| // Do something when it doesn't work | |
| }); | |
| // Async/await example | |
| (async () => { | |
| try { | |
| const decoded = await asyncJwtVerify(token, PRIVATE_KEY); | |
| console.log(decoded); | |
| // Do something when it works... | |
| } catch (error) { | |
| console.log(error); | |
| // Do something when it doesn't work | |
| } | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment