Easily create and validate JWT tokens in your browser.
Library by Dominick Lee. Provided under the MIT license. See on GitHub
Include the QuickJWT library in your HTML code using this CDN:
<script src="https://cdn.jsdelivr.net/gh/dominicklee/Quick-JWT@latest/js/quick-jwt.js"></script>
Make sure that line is placed before the "</body>" tag.
This library needs the CryptoJS library for cryptography. For your convenience, you can dynamically load it in QuickJWT like so:
QuickJWT.loadCryptoJS(() => { // Run your JS code here after CryptoJS loads });
Declare your payload and secret variables. Then use QuickJWT.sign to generate and sign your JWT. The output will be your JWT string.
QuickJWT.loadCryptoJS(() => { const payload = { userID: "1234567890", name: "John Doe", iat: 1516239022 }; const secret = "your-256-bit-secret"; //change this // Create a token const token = QuickJWT.sign(payload, secret); console.log("Generated Token:", token); });
If you receive a JWT token from someone, you may want to validate its signature against your own private key or secret to ensure the payload integrity. Afterwards, you may want to decode its payload contents. You can do that as follows:
QuickJWT.loadCryptoJS(() => { const token = 'your-JWT-token-here'; //change this const secret = "your-256-bit-secret"; //change this // Validate the token const isValid = QuickJWT.validate(token, secret); console.log("Is Valid:", isValid); // Output: true (if secret matches) // Decode the payload contents if signature is valid if (isValid) { // Decode the payload const decodedPayload = QuickJWT.decode(token); console.log("Decoded Payload:", decodedPayload); } });
See the entire demo in your browser here. The contents shown here are dynamically generated with QuickJWT.
{ "userID": "1234567890", "name": "John Doe", "iat": 1516239022 }
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySUQiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.ubbaM726WY6-jHjFJ2AXqxDAuyndUWXz6RJT8CAfWV0"
{ "userID": "1234567890", "name": "John Doe", "iat": 1516239022 }
true