From febeb685da2f7db3a1eef2a5d47bcd6a0c2482cf Mon Sep 17 00:00:00 2001 From: Nagendra Date: Mon, 4 Mar 2024 17:08:26 +0530 Subject: [PATCH] [Story]: failed to fetch fbLongLivedtoken using credentials from db - fixed --- pages/api/getFbLongLivedToken.tsx | 74 ++++++++++++++++--------------- pages/auth/callback/index.tsx | 23 +++++++--- 2 files changed, 55 insertions(+), 42 deletions(-) diff --git a/pages/api/getFbLongLivedToken.tsx b/pages/api/getFbLongLivedToken.tsx index 0a59590a..25002745 100644 --- a/pages/api/getFbLongLivedToken.tsx +++ b/pages/api/getFbLongLivedToken.tsx @@ -4,45 +4,47 @@ * Author: Nagendra S @ valmi.io */ -import { NextApiRequest, NextApiResponse } from 'next/types'; +//@ts-nocheck import axios from 'axios'; -import { configureCredentials } from 'pages/api/utils'; +import { createRouter } from 'next-connect'; import { sendErrorToBugsnag } from '@lib/bugsnag'; +import { oauthKeys } from '@/lib/oauth_middleware'; -const getFbLongLivedToken = async ( - req: NextApiRequest, - res: NextApiResponse -) => { - let { url, method, data } = req.body; - let payload = data; - - if (data?.config?.credentials) { - // credentials with actual api keys attached - payload = configureCredentials(data); - } - - const access_token = payload?.config?.credentials?.access_token || ''; - const client_id = payload?.config?.credentials?.client_id || ''; - const client_secret = payload?.config?.credentials?.client_secret || ''; - - // query - url = `https://graph.facebook.com/v16.0/oauth/access_token?client_id=${client_id}&client_secret=${client_secret}&grant_type=fb_exchange_token&fb_exchange_token=${access_token}`; - - try { - const response = await axios.get(url); - - const data = response.data; - // Handle the response data as needed - res.status(200).json(data); - } catch (error: any) { - // send error to bugsnag - sendErrorToBugsnag(error.message); - // Handle any error that occurred during the request - res.status(500).json({ error: error.message }); - } -}; - -export default getFbLongLivedToken; +const router = createRouter(); + +router + .use(oauthKeys) + + .get(async (req, res, next) => { + const { state = '' } = req.query; + + let json = JSON.parse(decodeURIComponent(state)); + + let { accessToken: access_token = '' } = json; + + let query_response = { ...(req.credentials ?? {}) }; + + const client_id = query_response['AUTH_FACEBOOK_CLIENT_ID']; + const client_secret = query_response['AUTH_FACEBOOK_CLIENT_SECRET']; + + // query + const url = `https://graph.facebook.com/v16.0/oauth/access_token?client_id=${client_id}&client_secret=${client_secret}&grant_type=fb_exchange_token&fb_exchange_token=${access_token}`; + + try { + const response = await axios.get(url); + + const data = response.data; + // Handle the response data as needed + return res.status(200).json(data); + } catch (error: any) { + // send error to bugsnag + sendErrorToBugsnag(error.message); + // Handle any error that occurred during the request + return res.status(500).json({ error: error.message }); + } + }); + +export default router.handler(); diff --git a/pages/auth/callback/index.tsx b/pages/auth/callback/index.tsx index 828ac58e..1808d093 100644 --- a/pages/auth/callback/index.tsx +++ b/pages/auth/callback/index.tsx @@ -15,7 +15,7 @@ import { hasErrorsInData } from '@components/Error/ErrorUtils'; import { RootState } from '@store/reducers'; import { AppDispatch } from '@store/store'; -import { setConnectionFlow } from '@store/reducers/connectionFlow'; +import connectionFlow, { setConnectionFlow } from '@store/reducers/connectionFlow'; const OAuthRedirectPage = () => { const router = useRouter(); @@ -53,13 +53,24 @@ const OAuthRedirectPage = () => { }, [router]); const getFbLongLivedToken = async (url: any, method: any, data: any) => { + const { workspaceId = '' } = appState || {}; + + const { oauth_keys = 'private', type = '' } = connection_flow?.flowState?.selected_connector ?? {}; + + let obj = { + workspace: workspaceId, + connector: type, + oauth_keys: oauth_keys, + accessToken: data?.config?.credentials?.access_token ?? '' + }; + + let state = encodeURIComponent(JSON.stringify(obj)); + try { - const response = await axios.post('/api/getFbLongLivedToken', { - url, - method, - data - }); + const response = await axios.get('/api/getFbLongLivedToken', { params: { state: state } }); + const result = response.data; + if (hasErrorsInData(result)) { // TODO: Handle this error if needed. dispatch(