Middleware and iframe updates

This commit is contained in:
Darren Clarke 2023-03-01 11:02:15 +00:00
parent d7624d723f
commit ba04aa108c
29 changed files with 69 additions and 4128 deletions

View file

@ -0,0 +1,39 @@
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");
}
req.headers['X-Forwarded-User'] = session.email.toLowerCase();
req.headers['Host'] = 'zammad.example.com';
console.log({ headers: req.headers })
return handler(req, res);
};
const proxy = createProxyMiddleware({
target: process.env.ZAMMAD_URL,
changeOrigin: true,
xfwd: true,
ws: true,
pathRewrite: { "^/zammad": "" },
});
export default withAuthInfo(proxy);
export const config = {
api: {
bodyParser: false,
externalResolver: true,
},
};

View file

@ -4,6 +4,9 @@ import { Apple as AppleIcon, Google as GoogleIcon } from "@mui/icons-material";
import { signIn, getSession } from "next-auth/react";
const Login = ({ session }) => {
const origin = typeof window !== 'undefined' && window.location.origin
? window.location.origin
: '';
const buttonStyles = {
borderRadius: 500,
width: "100%",
@ -40,7 +43,7 @@ const Login = ({ session }) => {
sx={buttonStyles}
onClick={() =>
signIn("google", {
callbackUrl: `https://redaranj-bookish-tribble-56jwjx5wh4j4w-8003.preview.app.github.dev/auth/sso`,
callbackUrl: `${origin}/auth/sso`,
})
}
>

View file

@ -12,18 +12,21 @@ type TicketProps = {
};
const Ticket: NextPage<TicketProps> = ({ id }) => {
const origin = typeof window !== 'undefined' && window.location.origin
? window.location.origin
: '';
const fetcher = async (url: string) => {
const res = await fetch(url);
return res.json();
}
const { data: ticketData, error: ticketError } = useSWR(
`https://redaranj-bookish-tribble-56jwjx5wh4j4w-8003.preview.app.github.dev/api/v1/tickets/${id}`,
`${origin}/api/v1/tickets/${id}`,
fetcher
);
const { data: articlesData, error: articlesError } = useSWR(
`https://redaranj-bookish-tribble-56jwjx5wh4j4w-8003.preview.app.github.dev/api/v1/ticket_articles/by_ticket/${id}`,
`${origin}/api/v1/ticket_articles/by_ticket/${id}`,
fetcher
);