Configure the Advanced IO Function
Now, we will begin coding the authorization portal application by configuring the function component.
The function’s directory, (functions/custom_token_generation), contains:
- The ndex.js main function file
- The catalyst-config.json configuration file
- Node modules
- package.json and package-lock.json dependency files
Before you start adding code, you need to install the following packages to ensure all required dependencies are satisfied.
- Express: The express package is required to handle HTTP routes
- Axios: The axios package is required to handle outgoing HTTP requests
Navigate to the function folder present in the functions/custom_token_generation directory and install the following packages using the command shown below:
copy
$
npm install express axios --save
You will be adding code in the index.js file.
The Advanced IO function contains the following functionalities:
| Endpoint | Request Method | Purpose | Possible Expected Responses |
|---|---|---|---|
| /getauth | GET |
|
Returns the required Catalyst Custom Token (JSON) or a 401 if no token is obtained. |
| /catalystUser | GET | Initializes Catalyst using request context and retrieves the details of the authenticated user. | Returns a JSON containing the end-user details. |
Copy the code below and paste it into the respective files of your project using an IDE and save the files.
Note: Go through the code in this section to make sure you fully understand it.
index.js
copy
'use strict'
// Import Catalyst Node SDK
const catalyst = require('zcatalyst-sdk-node')
// Import Express framework
const express = require('express')
const app = express()
// Initialize express middleware
app.use(express())
// Import axios to make HTTP requests to Auth0
const axios = require('axios')
// Header name where Auth0 access token is expected
const auth0TokenHeader = 'zc-customauth'
// Replace with your Auth0 domain (example: https://your-domain.auth0.com)
const auth0Domain = '{replace your Auth0 domain}'
// Endpoint to validate Auth0 token and generate Catalyst custom token
app.get('/getauth', async (req, res) => {
try {
// Read Auth0 access token from request header
const auth0AccessToken = req.headers[auth0TokenHeader]
// Auth0 user info endpoint
const auth0Url = auth0Domain + '/userinfo'
// Proceed only if token is provided
if (auth0AccessToken !== undefined) {
const url = auth0Url
// Attach Authorization header with Bearer token
const config = {
headers: {
Authorization: 'Bearer ' + auth0AccessToken
}
}
// Call Auth0 to fetch user details
const auth0response = await axios.get(url, config)
// Initialize Catalyst app using request context
const catalystApp = catalyst.initialize(req)
// Generate Catalyst custom web token using Auth0 user details
const tokenObj = await catalystApp.userManagement().generateCustomToken({
type: 'web',
user_details: {
email_id: auth0response.data.email,
first_name: auth0response.data.given_name,
last_name: auth0response.data.family_name
}
})
// Send generated Catalyst token back to client
res.status(200).send(JSON.stringify(tokenObj))
} else {
// If Auth0 token is missing in header
res.status(401).send({ status: 'failure', message: 'Token not provided in the header' })
}
} catch (error) {
// Log error and return failure response
console.log(error)
res.status(error.response.status).send({ status: 'failure', message: 'An unexpected error has occured. Please try again after sometime.' })
}
})
// Endpoint to fetch the currently authenticated Catalyst user
app.get('/catalystUser', async (req, res) => {
try {
// Initialize Catalyst using request context
const catalystApp = catalyst.initialize(req)
// Retrieve current logged-in Catalyst user
const currentUser = await catalystApp.userManagement().getCurrentUser()
// Send user details as response
res.status(200).send(JSON.stringify(currentUser))
} catch (error) {
// Handle and log any unexpected errors
console.log(error)
res.status(500).send({ status: 'failure', message: error })
}
})
// Export the Express app
module.exports = app
Note: Ensure you provide domain generated by Auth0 in line 14.
Last Updated 2026-02-23 22:17:04 +0530 IST
