Use NextJS middleware for proxying
This commit is contained in:
parent
4e4603bd71
commit
df9b8abf15
13 changed files with 154 additions and 94 deletions
|
|
@ -1,39 +0,0 @@
|
|||
import { NextApiRequest, NextApiResponse } from 'next';
|
||||
import { createProxyMiddleware } from 'http-proxy-middleware';
|
||||
import { getToken } from "next-auth/jwt";
|
||||
|
||||
const withAuthInfo =
|
||||
(handler) => async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const session = await getToken({
|
||||
req,
|
||||
secret: process.env.NEXTAUTH_SECRET,
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return res.redirect("/login");
|
||||
}
|
||||
|
||||
if (req.headers) {
|
||||
req.headers['X-Forwarded-User'] = session.email.toLowerCase();
|
||||
req.headers['host'] = 'zammad.example.com';
|
||||
}
|
||||
|
||||
return handler(req, res);
|
||||
};
|
||||
|
||||
const proxy = createProxyMiddleware({
|
||||
target: process.env.ZAMMAD_URL,
|
||||
changeOrigin: true,
|
||||
xfwd: true,
|
||||
ws: false,
|
||||
pathRewrite: { "^/zammad": "" },
|
||||
});
|
||||
|
||||
export default withAuthInfo(proxy);
|
||||
|
||||
export const config = {
|
||||
api: {
|
||||
bodyParser: false,
|
||||
externalResolver: true,
|
||||
},
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue