icynet-admin/pages/api/[...path].ts

38 lines
910 B
TypeScript
Raw Normal View History

2022-08-29 18:09:28 +00:00
import { proxy } from '../../server/proxy';
import Cookies from 'cookies';
import { NextApiRequest, NextApiResponse } from 'next';
import { COOKIE_KEYS } from '../../lib/constants';
2022-09-11 09:31:09 +00:00
const inProd = process.env.NODE_ENV === 'production';
2022-08-29 18:09:28 +00:00
const handler = (req: NextApiRequest, res: NextApiResponse) => {
2022-09-20 14:58:49 +00:00
return new Promise<void>((resolve, reject) => {
2022-09-11 09:31:09 +00:00
const cookies = new Cookies(req, res, {
keys: COOKIE_KEYS,
secure: inProd,
});
2022-08-29 18:09:28 +00:00
const authorization = cookies.get('authorization', { signed: true });
2022-09-20 14:58:49 +00:00
// don't forward the cookies to the target server
2022-08-29 18:09:28 +00:00
req.headers.cookie = '';
if (authorization) {
req.headers.authorization = `Bearer ${authorization}`;
}
2022-09-20 14:58:49 +00:00
proxy.web(req, res, undefined, (err) => {
if (err) return reject(err);
resolve();
});
2022-08-29 18:09:28 +00:00
});
};
export default handler;
export const config = {
api: {
bodyParser: false,
},
};