UNPKG

citi-hub-session-engine

Version:

Module for authentication methods for CITI hub

58 lines (45 loc) 1.91 kB
const { logger } = require("../utils/Logger"); const { configurations_key } = require("../utils/Constants"); const getTokenUser = async (token, id) => { try { logger.info(`Initiating getTokenUser`); const config = global[configurations_key]; let endpoint = config?.getTokenUser?.endpoint || ""; logger.info(`endpoint: ${endpoint}`); /** * const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); myHeaders.append("Authorization", "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkIjoiZjc5MGNmNDEtM2Q5NS00Yjc5LTk5YTEtN2VkYzYxNDllNzUyIiwidXNlcm5hbWUiOiIrNTI5OTk5OTk5MDA2In0sImNsaWVudCI6eyJpZCI6IjFjYmI2NzExLTAzM2ItNDdlZC05NDcxLTRlYTEwZmY4ZGM3ZCJ9LCJleHAiOjE3MjUzODgxNzgsImlhdCI6MTcyNTM4NDU3OX0.d32njygSQWQQlMxbWGPcBdUXbzq_PKhPx6epPrecFr8"); const urlencoded = new URLSearchParams(); urlencoded.append("user_id", "b23d7def-bc48-4f38-96bd-a43a019a0264"); const requestOptions = { method: "POST", headers: myHeaders, body: urlencoded, redirect: "follow" }; fetch("http://localhost:3001/api/session-engine/get-token", requestOptions) .then((response) => response.text()) .then((result) => console.log(result)) .catch((error) => console.error(error)); */ //INVOKE ENDPOINT, GET, headers: authorization, body: params const response = await fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: `Bearer ${token}`, }, body: new URLSearchParams({ user_id: id, }), }); const data = await response.json(); logger.info(`Response from userProviders: ${data.code}`); return data; } catch (e) { logger.error(`An error occurred while userProviders :: ${e}`); return null; } }; module.exports = getTokenUser;