Skip to content

Commit

Permalink
[bug]: failed to fetch acces token from checkConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Nagendra committed Dec 5, 2023
1 parent 990cdab commit f2bd63f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
7 changes: 2 additions & 5 deletions pages/api/access.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NextApiRequest, NextApiResponse } from 'next/types';

import cookie from 'cookie';
import { getAccessTokenCookie } from './utils';

type ResponseData = {
accessToken: string;
Expand All @@ -10,10 +10,7 @@ export default function accessTokenHandler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
const cookies = cookie.parse(req.headers?.cookie ?? '');
const appCookie = cookies?.['AUTH'] ?? '';
const parsedCookies = appCookie ? JSON.parse(appCookie) : {};
const accessToken = parsedCookies?.accessToken ?? null;
const accessToken = getAccessTokenCookie(req) || '';

res.status(200).json({ accessToken: accessToken });
}
8 changes: 2 additions & 6 deletions pages/api/checkConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NextApiRequest, NextApiResponse } from 'next/types';

import axios from 'axios';

import { getAccessToken, getBaseUrl } from 'pages/api/utils';
import { getAccessTokenCookie, getBaseUrl } from 'pages/api/utils';

import { sendErrorToBugsnag } from '@lib/bugsnag';

Expand All @@ -19,11 +19,7 @@ const checkConnection = async (req: NextApiRequest, res: NextApiResponse) => {
// query
url = `${getBaseUrl()}${url}`;

const response = await getAccessToken();

const jsonData = await response.json();

const bearerToken = jsonData.accessToken || '';
const bearerToken = getAccessTokenCookie(req) || '';

try {
const response = await axios.post(url, payload, {
Expand Down
8 changes: 2 additions & 6 deletions pages/api/connectionNetworkHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NextApiRequest, NextApiResponse } from 'next/types';

import axios from 'axios';

import { getAccessToken, getBaseUrl } from 'pages/api/utils';
import { getAccessTokenCookie, getBaseUrl } from 'pages/api/utils';

import { sendErrorToBugsnag } from '@lib/bugsnag';

Expand All @@ -21,11 +21,7 @@ const connectionNetworkHandler = async (
// query
connectionUrl = `${getBaseUrl()}${connectionUrl}`;

const response = await getAccessToken();

const jsonData = await response.json();

const bearerToken = jsonData.accessToken || '';
const bearerToken = getAccessTokenCookie(req) || '';

try {
const response = await axios.post(connectionUrl, payload, {
Expand Down
10 changes: 10 additions & 0 deletions pages/api/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Author: Nagendra S @ valmi.io
*/

import cookie from 'cookie';

export const configureCredentials = (data: any) => {
let config = data?.config
? data.config.credentials
Expand Down Expand Up @@ -55,3 +57,11 @@ export const logoutUser = () => {
}
});
};

export const getAccessTokenCookie = (req: any) => {
const cookies = cookie.parse(req.headers?.cookie ?? '');
const appCookie = cookies?.['AUTH'] ?? '';
const parsedCookies = appCookie ? JSON.parse(appCookie) : {};
const accessToken = parsedCookies?.accessToken ?? null;
return accessToken;
};

0 comments on commit f2bd63f

Please sign in to comment.