Overview & recent fixes, auth updates, SWR fixes
This commit is contained in:
parent
7df947f35a
commit
8d86db882d
29 changed files with 1252 additions and 1221 deletions
|
|
@ -1,6 +1,6 @@
|
|||
"use client";
|
||||
|
||||
import { FC, PropsWithChildren, useState, useEffect } from "react";
|
||||
import { FC, PropsWithChildren, useState } from "react";
|
||||
import { usePathname } from "next/navigation";
|
||||
import { CssBaseline } from "@mui/material";
|
||||
import { CookiesProvider } from "react-cookie";
|
||||
|
|
@ -24,25 +24,46 @@ export const MultiProvider: FC<PropsWithChildren> = ({ children }) => {
|
|||
typeof window !== "undefined" && window.location.origin
|
||||
? window.location.origin
|
||||
: null;
|
||||
const client = new GraphQLClient(`${origin}/zammad/graphql`, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
},
|
||||
});
|
||||
const client = new GraphQLClient(`${origin}/zammad/graphql`);
|
||||
const messages: any = { en: locales.en, fr: locales.fr };
|
||||
const locale = "en";
|
||||
const fetchAndCheckAuth = async ({ document, variables }: any) => {
|
||||
const fetchAndCheckAuth = async ({
|
||||
document,
|
||||
variables,
|
||||
url,
|
||||
method,
|
||||
body,
|
||||
}: any) => {
|
||||
const requestHeaders = {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
"X-CSRF-Token": csrfToken,
|
||||
};
|
||||
const { data, headers, status } = await client.rawRequest(
|
||||
document,
|
||||
variables,
|
||||
requestHeaders,
|
||||
);
|
||||
let responseData = null;
|
||||
let responseHeaders = new Headers();
|
||||
let responseStatus = null;
|
||||
|
||||
if (status !== 200) {
|
||||
if (document) {
|
||||
const { data, headers, status } = await client.rawRequest(
|
||||
document,
|
||||
variables,
|
||||
requestHeaders,
|
||||
);
|
||||
responseData = data;
|
||||
responseHeaders = headers;
|
||||
responseStatus = status;
|
||||
} else {
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
headers: requestHeaders,
|
||||
body,
|
||||
});
|
||||
responseData = await res.json();
|
||||
responseHeaders = res.headers;
|
||||
responseStatus = res.status;
|
||||
}
|
||||
|
||||
if (responseStatus !== 200) {
|
||||
const res = await fetch("/zammad/auth/sso", {
|
||||
method: "GET",
|
||||
redirect: "manual",
|
||||
|
|
@ -53,18 +74,30 @@ export const MultiProvider: FC<PropsWithChildren> = ({ children }) => {
|
|||
return null;
|
||||
}
|
||||
|
||||
const token = headers.get("CSRF-Token");
|
||||
const token = responseHeaders.get("CSRF-Token");
|
||||
setCsrfToken(token);
|
||||
|
||||
return data;
|
||||
return responseData;
|
||||
};
|
||||
|
||||
const graphQLFetcher = async ({ document, variables }: any) => {
|
||||
const multiFetcher = async ({
|
||||
document,
|
||||
variables,
|
||||
url,
|
||||
method,
|
||||
body,
|
||||
}: any) => {
|
||||
let checks = 0;
|
||||
let data = null;
|
||||
|
||||
while (!data && checks < 2) {
|
||||
data = await fetchAndCheckAuth({ document, variables });
|
||||
data = await fetchAndCheckAuth({
|
||||
document,
|
||||
variables,
|
||||
url,
|
||||
method,
|
||||
body,
|
||||
});
|
||||
checks++;
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +108,7 @@ export const MultiProvider: FC<PropsWithChildren> = ({ children }) => {
|
|||
<>
|
||||
<CssBaseline />
|
||||
<NextAppDirEmotionCacheProvider options={{ key: "css" }}>
|
||||
<SWRConfig value={{ fetcher: graphQLFetcher }}>
|
||||
<SWRConfig value={{ fetcher: multiFetcher }}>
|
||||
<SessionProvider>
|
||||
<CookiesProvider>
|
||||
<LocalizationProvider dateAdapter={AdapterDateFns}>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue