import express from 'express';const app = express();const PORT = 8080;async function generateVeltAuthToken(userId) { const url = "https://api.velt.dev/v1/auth/token/get"; 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 userProperties: { isAdmin: true, // Set to true if you want to set user as admin organizationId: "YOUR_ORGANIZATION_ID", // If organizationId is provided here then we will validate it with the organizationId used in the identify call email: "USER_EMAIL", // If email is provided here then we will validate it with the email used in the identify call } }, }; 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); }}app.get('/generate-velt-jwt-token', async (req,res) => { const veltAuthToken = await generateVeltAuthToken(req.body.userId) res.json(veltAuthToken)})app.listen(PORT, () => { console.log(`JWT Server listening on port ${PORT}`);});
In your server endpoint, call our https://api.velt.dev/v1/auth/token/get endpoint to generate a JWT Token.
Example server code:
Copy
async function generateVeltAuthToken(userId: string) { const url = "https://api.velt.dev/v1/auth/token/get"; 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 userProperties: { isAdmin: true, // Set to true if you want to set user as admin organizationId: "YOUR_ORGANIZATION_ID", // If organizationId is provided here then we will validate it with the organizationId used in the identify call email: "USER_EMAIL", // If email is provided here then we will validate it with the email used in the identify call } }, }; 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); }}
To get your Auth Token that is required for your request body, read here.
Field
Required
Description
apiKey
Yes
Velt API Key
authToken
Yes
Auth Token from the Velt console
userId
Yes
Unique user id of the user
userProperties.organizationId
Yes
The ogranizationId should match the organizationId used in the identify call.
userProperties.isAdmin
No
Set to true if you want to set user as admin. This is the only way to set a user as an admin User. Please do not set this property in the identify call as this will unset the isAdmin property.
userProperties.email
No
If email is provided, it will be validated with the email used in the identify call. Recommended if you are setting email.
Copy
{ "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 "userProperties": { isAdmin: true, // Set to true if you want to set user as admin organizationId: "YOUR_ORGANIZATION_ID", // If organizationId is provided here then we will validate it with the organizationId used in the identify call email: "USER_EMAIL", // If email is provided here then we will validate it with the email used in the identify call } }}
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.
When a token expires, Velt emits a token_expired error event
Your application should:
Subscribe to the error event to detect expired tokens
Generate a new token via your server endpoint when expiry occurs
Call identify() with the fresh token to re-authenticate the user
Using Hook:
Copy
const errorEvent = useVeltEventCallback('error');useEffect(() => { if (errorEvent?.code === "token_expired") { // Generate new Velt Auth Token // Call identify with the new token }}, [errorEvent]);
Using API:
Copy
client.on('error').subscribe((error) => { if (error?.code === "token_expired") { // Generate new Velt Auth Token // Call identify with the new token }});
Using Hook:
Copy
const errorEvent = useVeltEventCallback('error');useEffect(() => { if (errorEvent?.code === "token_expired") { // Generate new Velt Auth Token // Call identify with the new token }}, [errorEvent]);
Using API:
Copy
client.on('error').subscribe((error) => { if (error?.code === "token_expired") { // Generate new Velt Auth Token // Call identify with the new token }});
Copy
Velt.on('error').subscribe((error) => { if (error?.code === "token_expired") { // Generate new Velt Auth Token // Call identify with the new token }});
7
Your All Done!
You are all done! Now you have added an additional level of security with JWT Tokens.
Copy
import express from 'express';const app = express();const PORT = 8080;async function generateVeltAuthToken(userId) { const url = "https://api.velt.dev/v1/auth/token/get"; 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 userProperties: { isAdmin: true, // Set to true if you want to set user as admin organizationId: "YOUR_ORGANIZATION_ID", // If organizationId is provided here then we will validate it with the organizationId used in the identify call email: "USER_EMAIL", // If email is provided here then we will validate it with the email used in the identify call } }, }; 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); }}app.get('/generate-velt-jwt-token', async (req,res) => { const veltAuthToken = await generateVeltAuthToken(req.body.userId) res.json(veltAuthToken)})app.listen(PORT, () => { console.log(`JWT Server listening on port ${PORT}`);});
import express from 'express';const app = express();const PORT = 8080;async function generateVeltAuthToken(userId) { const url = "https://api.velt.dev/v1/auth/token/get"; 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 userProperties: { isAdmin: true, // Set to true if you want to set user as admin organizationId: "YOUR_ORGANIZATION_ID", // If organizationId is provided here then we will validate it with the organizationId used in the identify call email: "USER_EMAIL", // If email is provided here then we will validate it with the email used in the identify call } }, }; 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); }}app.get('/generate-velt-jwt-token', async (req,res) => { const veltAuthToken = await generateVeltAuthToken(req.body.userId) res.json(veltAuthToken)})app.listen(PORT, () => { console.log(`JWT Server listening on port ${PORT}`);});
Assistant
Responses are generated using AI and may contain mistakes.