Use server actions instead of client-side API calls
This commit is contained in:
parent
5a3127dcb0
commit
aa453954ed
30 changed files with 703 additions and 462 deletions
|
|
@ -121,16 +121,23 @@ export const authOptions: NextAuthOptions = {
|
|||
session.user.roles = token.roles ?? [];
|
||||
// @ts-ignore
|
||||
session.user.leafcutter = token.leafcutter; // remove
|
||||
// @ts-ignore
|
||||
session.user.zammadCsrfToken = token.zammadCsrfToken;
|
||||
|
||||
return session;
|
||||
},
|
||||
jwt: async ({ token, user, account, profile, trigger }) => {
|
||||
jwt: async ({ token, user, account, profile, trigger, session }) => {
|
||||
if (user) {
|
||||
token.roles = (await getUserRoles(user.email)) ?? [];
|
||||
}
|
||||
|
||||
if (session && trigger === "update") {
|
||||
token.zammadCsrfToken = session.csrfToken;
|
||||
}
|
||||
return token;
|
||||
},
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
export const getServerSession = (
|
||||
...args:
|
||||
|
|
|
|||
68
apps/link/app/_lib/zammad.ts
Normal file
68
apps/link/app/_lib/zammad.ts
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import { getServerSession } from "app/_lib/authentication";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
const getHeaders = async () => {
|
||||
const allCookies = cookies().getAll();
|
||||
const session = await getServerSession();
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
// @ts-ignore
|
||||
"X-CSRF-Token": session.user.zammadCsrfToken,
|
||||
Cookie: allCookies
|
||||
.map((cookie: any) => `${cookie.name}=${cookie.value}`)
|
||||
.join("; "),
|
||||
};
|
||||
|
||||
return headers;
|
||||
};
|
||||
|
||||
interface ExecuteGraphQLOptions {
|
||||
query: string;
|
||||
variables?: Record<string, any>;
|
||||
}
|
||||
|
||||
export const executeGraphQL = async ({
|
||||
query,
|
||||
variables,
|
||||
}: ExecuteGraphQLOptions): Promise<any> => {
|
||||
const headers = await getHeaders();
|
||||
const endpoint = `${process.env.ZAMMAD_URL}/graphql`;
|
||||
const body = JSON.stringify({ query, ...(variables && { variables }) });
|
||||
const result = await fetch(endpoint, {
|
||||
headers,
|
||||
body,
|
||||
method: "POST",
|
||||
next: { revalidate: 0 },
|
||||
});
|
||||
|
||||
const { data, errors } = await result.json();
|
||||
|
||||
if (result.status !== 200) {
|
||||
throw new Error(`Error: ${result.status}`);
|
||||
} else if (errors) {
|
||||
throw new Error(`Error: ${JSON.stringify(errors)}`);
|
||||
}
|
||||
|
||||
return data;
|
||||
};
|
||||
|
||||
interface ExecuteRESTOptions {
|
||||
path: string;
|
||||
}
|
||||
|
||||
export const executeREST = async ({
|
||||
path,
|
||||
}: ExecuteRESTOptions): Promise<any> => {
|
||||
const headers = await getHeaders();
|
||||
const result = await fetch(`${process.env.ZAMMAD_URL}${path}`, {
|
||||
method: "GET",
|
||||
headers,
|
||||
});
|
||||
|
||||
if (result.status !== 200) {
|
||||
throw new Error(`Error: ${result.status}`);
|
||||
}
|
||||
|
||||
return await result.json();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue