Skip to content

Instantly share code, notes, and snippets.

@gaufung
Created September 20, 2020 10:27
Show Gist options
  • Save gaufung/8152fd967a4acc89ac6432750ef5c830 to your computer and use it in GitHub Desktop.
Save gaufung/8152fd967a4acc89ac6432750ef5c830 to your computer and use it in GitHub Desktop.
Generate a JWT using javascript
const crypto = require('crypto')
# header
const header = { "alg": "HS256", "typ": "JWT" }
const encodedHeader = Buffer.from(JSON.stringify(header)).toString('base64')
# payload
const payload = { username: 'Flavio' }
const encodedPayload = Buffer.from(JSON.stringify(payload)).toString('base64')
# signature
const jwtSecret = 'secretKey'
const signature = crypto.createHmac('sha256', jwtSecret).update(encodedHeader + '.' + encodedPayload).digest('base64')
const jwt = `${encodedHeader}.${encodedPayload}.${signature}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment