Created
September 20, 2020 10:27
-
-
Save gaufung/8152fd967a4acc89ac6432750ef5c830 to your computer and use it in GitHub Desktop.
Generate a JWT using javascript
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 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