tweak proxy

This commit is contained in:
Evert Prants 2022-09-20 17:58:49 +03:00
parent 303e52da3e
commit 3bd4feb0ed
Signed by: evert
GPG Key ID: 1688DA83D222D0B5

View File

@ -7,31 +7,24 @@ import { COOKIE_KEYS } from '../../lib/constants';
const inProd = process.env.NODE_ENV === 'production'; const inProd = process.env.NODE_ENV === 'production';
const handler = (req: NextApiRequest, res: NextApiResponse) => { const handler = (req: NextApiRequest, res: NextApiResponse) => {
return new Promise((resolve, reject) => { return new Promise<void>((resolve, reject) => {
// removes the api prefix from url
// req.url = req.url!.replace(/^\/api/, '');
const cookies = new Cookies(req, res, { const cookies = new Cookies(req, res, {
keys: COOKIE_KEYS, keys: COOKIE_KEYS,
secure: inProd, secure: inProd,
}); });
const authorization = cookies.get('authorization', { signed: true }); const authorization = cookies.get('authorization', { signed: true });
// don't forwards the cookies to the target server // don't forward the cookies to the target server
req.headers.cookie = ''; req.headers.cookie = '';
if (authorization) { if (authorization) {
req.headers.authorization = `Bearer ${authorization}`; req.headers.authorization = `Bearer ${authorization}`;
} }
/** proxy.web(req, res, undefined, (err) => {
* if an error occurs in the proxy, we will reject the promise. if (err) return reject(err);
* it is so important. if you don't reject the promise, resolve();
* you're facing the stalled requests issue. });
*/
proxy.once('error', reject);
proxy.web(req, res);
}); });
}; };