Code Steps
Code Steps
Code steps are pages just like this one, where you can show the steps to achieving something in code. To add a code step:
- From the index, click on a
. - Choose Code Steps
To demonstrate Code Steps, this is an example of how you could use Custom Login on DeveloperHub.
This example uses express
as the backend server.
First, to generate JWT, it is easier to do it using a library. To install jsonwebtoken
, run npm install jsonwebtoken
.
The JWT would be signed using the API Key. Get your API Key and save it in the code.
The payload is the information being sent securely using JWT. The payload for Custom Login is a JSON object and it must have two properties.
version
must be set to 1
vars
contains the variables to set when the docs load. You could personalise the docs for the user here.
Setting a shorter expiry ensures that if a JWT falls into the wrong hands. We recommend a day.
Sign the token with the API key, setting the expiry.
The URL contains the path that the reader tried to access and a query param jwt
containing the token.
Finally, redirect the user back to the docs with the signed token.
const sign = require('jsonwebtoken').sign;
const apiKey = '689c3ce8e7c68b7c7f86acca6a028e6f8656eb792b19a334f8e3f2a56ca8f561';
function getSignedDeveloperHubUrl(url) {
const docsUrl = url || 'https://docs.pied-piper.com';
const payload = {
version: 1,
vars: {
user: {
id: 1234,
name: "John"
}
}
};
const expiresIn = 24 * 60 * 60;
const token = sign(payload, apiKey, {expiresIn: expiresIn});
return `${docsUrl}?jwt=${token}`;
}