Skip to content

Commit

Permalink
[Story]: failed to fetch fbLongLivedtoken using credentials from db -…
Browse files Browse the repository at this point in the history
… fixed
  • Loading branch information
S-Nagendra committed Mar 4, 2024
1 parent 9df6689 commit febeb68
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 42 deletions.
74 changes: 38 additions & 36 deletions pages/api/getFbLongLivedToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
23 changes: 17 additions & 6 deletions pages/auth/callback/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit febeb68

Please sign in to comment.