45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
import { proxy } from '../../server/proxy';
|
|
|
|
import Cookies from 'cookies';
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
import { COOKIE_KEYS } from '../../lib/constants';
|
|
|
|
const inProd = process.env.NODE_ENV === 'production';
|
|
|
|
const handler = (req: NextApiRequest, res: NextApiResponse) => {
|
|
return new Promise((resolve, reject) => {
|
|
// removes the api prefix from url
|
|
// req.url = req.url!.replace(/^\/api/, '');
|
|
|
|
const cookies = new Cookies(req, res, {
|
|
keys: COOKIE_KEYS,
|
|
secure: inProd,
|
|
});
|
|
const authorization = cookies.get('authorization', { signed: true });
|
|
|
|
// don't forwards the cookies to the target server
|
|
req.headers.cookie = '';
|
|
|
|
if (authorization) {
|
|
req.headers.authorization = `Bearer ${authorization}`;
|
|
}
|
|
|
|
/**
|
|
* if an error occurs in the proxy, we will reject the promise.
|
|
* it is so important. if you don't reject the promise,
|
|
* you're facing the stalled requests issue.
|
|
*/
|
|
proxy.once('error', reject);
|
|
|
|
proxy.web(req, res);
|
|
});
|
|
};
|
|
|
|
export default handler;
|
|
|
|
export const config = {
|
|
api: {
|
|
bodyParser: false,
|
|
},
|
|
};
|