Skip to content

Instantly share code, notes, and snippets.

@aksel
Created July 1, 2020 09:07
Show Gist options
  • Save aksel/95c35c4a8138d2d583f20decd2fa4980 to your computer and use it in GitHub Desktop.
Save aksel/95c35c4a8138d2d583f20decd2fa4980 to your computer and use it in GitHub Desktop.
Generating JWT.
const jwt = require('jsonwebtoken');
const fs = require('fs');
// Get jwt.dev.key from AWS Secrets Manager.
const privateKey = fs.readFileSync('jwt.dev.key');
const payload = {
user: {
// Must correspond with the ID of a user, in whichever database your connecting to.
// Otherwise, inserts will fail, due to FK constraints.
id: 'your-id',
username: 'FPL_YourUsername',
// Optionally also include is_admin: true, if you want greater access
},
session: {
id: 'some-session-id', // Not really important.
},
// Must be a user token.
// This is a flag that may be used in the future, e.g. for some other token types.
token_type: 'user',
};
const options = {
algorithm: 'RS512',
noTimestamp: true,
// Specify however long you wish to keep this token valid.
// These are usually only valid for 15 minutes.
// Maybe a year is overkill, eh?
expiresIn: '1 year',
};
const token = jwt.sign(payload, privateKey, options);
console.log(token);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment