JWT Tokens
Generate JWT Tokens for additional security
Overview
JWT Tokens
is an optional feature to add additional authentication security to our client.identify()
method to prevent user impersonation.
Enable JWT Tokens in your console
Go to https://console.velt.dev and enable the toggle for Require JWT Token
. The toggle is listed at the very bottom of the page.
Create a server endpoint for generating and sending JWT Tokens to the client
Create a server endpoint that will be used to generate and send a JWT Token
to the client.
Example server endpoint code:
app.get('/generate-velt-jwt-token', async (req,res) => {
const veltAuthToken = await generateVeltAuthToken(req.body.userId)
res.json(veltAuthToken)
})
Generate a JWT Token from your server
In your server endpoint, call our https://generateveltauthtoken.api.velt.dev
endpoint to generate a JWT Token
.
Example server code:
async function generateVeltAuthToken(userId: string) {
const url = "https://generateveltauthtoken.api.velt.dev";
const body = {
data: {
userId: userId, // Unique user id of your user
apiKey: "YOUR_VELT_API_KEY",
authToken: "YOUR_CLIENT_AUTH_TOKEN", // Get this token from console.velt.dev
},
};
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
return data?.result?.data?.token;
} catch (error) {
console.error("Error:", error);
}
}
Request Body for https://generateveltauthtoken.api.velt.dev
:
To get your Auth Token that is required for your request body, read here.
{
"data": {
"apiKey": "YOUR_API_KEY", //Velt API Key
"authToken": "YOUR_AUTH_TOKEN", // Auth Token from the Velt console
"userId": "yourUserId" // unique user id of the user you are generating a JWT Token for
}
}
Success Response for https://generateveltauthtoken.api.velt.dev
:
{
"result": {
"status": "success",
"message": "Token generated successfully.",
"data": {
"token": "YOUR_JWT_TOKEN"
}
}
}
Failure Response for https://generateveltauthtoken.api.velt.dev
:
{
"error": {
"message": "Auth token not found.",
"status": "INVALID_ARGUMENT"
}
}
Call your server endpoint from your client to pass your JWT Token to your client
Call your server endpoint from your client to pass your JWT Token
to your client.
const yourJWTToken = await callToYourServerToGetJWTToken(userId)
Pass the JWT Token to client.identify()
Once the JWT Token is generated, you can pass it into the client.identify()
method. The client.identify()
method has an optional second parameter that takes in a configuration object that includes the JWT Token
as a field.
const yourJWTToken = await callToYourServerToGetJWTToken(userId)
client.identify(user, {
authToken: yourJWTToken,
});
Your All Done!
You are all done! Now you have added an additional level of security with JWT Tokens
.
Was this page helpful?