Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>

<head>
  <title>Creating Json Web Token (JWT) with Javascript</title>
</head>

<body class="p-2">
  <div class="flex flex-row">
    <div style="width: 25%;">
      <h2 class="p-1 text-lg font-bold border-b border-gray-100">Token properties</h2>
      <p class="p-2">
        You can find more info about Json Web Tokens at <a href="https://pdfgeneratorapi.com" targer="_blank">https://jwt.io/</a>.
      </p>
      <form class="m-4">
        <div class="space-y-4">
          <div>
            <label for="iss" class="block font-medium">iss (issuer)</label>
            <input type="text" name="iss" id="iss" class="block rounded ring-1 ring-inset ring-gray-300 p-1" placeholder="iss (issuer)">
          </div>
          <div>
            <label for="sub" class="block font-medium">sub (subject)</label>
            <input type="text" name="sub" id="sub" class="block rounded ring-1 ring-inset ring-gray-300 p-1" placeholder="sub (subject)">
          </div>
          <div>
            <label for="secret" class="block font-medium">secret (not base64 encoded)</label>
            <input type="text" name="secret" id="secret" class="block rounded ring-1 ring-inset ring-gray-300 p-1" placeholder="secret (not base64 encoded)">
          </div>
        </div>
        <div class="mt-4">
          <button type="button" id="create-token-btn" class="rounded-md bg-indigo-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600">Create token</button>
        </div>
      </form>
    </div>
    <div style="width: 75%;">
      <h2 class="p-1 text-lg font-bold border-b border-gray-100">Token</h2>
      <p class="p-2">Toke will appare here.</p>
      <div class="p-2 italic" id="token-placeholder"></div>
    </div>
  </div>
</body>

</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                import * as jose from "https://cdn.skypack.dev/jose@4.13.1";

document.querySelector("#create-token-btn").addEventListener("click", (event) => {
  createJsonWebToken(
    document.querySelector("#iss").value,
    document.querySelector("#sub").value,
    document.querySelector("#secret").value
  ).then((token) => {
    document.querySelector('#token-placeholder').innerHTML = token;
  });
});

async function createJsonWebToken(iss, sub, secret) {
  const header = {
    alg: "HS256",// Token generation algorithm
    typ: "JWT"
  };

  const payload = {
    iss: iss,
    sub: sub,
    exp: Math.round(Date.now() / 1000) + 60 // token is valid for 60 seconds
  };

  return await new jose.SignJWT(payload)
    .setProtectedHeader(header)
    .sign(new TextEncoder().encode(secret));
}

              
            
!
999px

Console