refactor: extract common auth headers into reusable helper (#7439)

This commit is contained in:
Pablo Lara
2025-04-07 08:16:55 +02:00
committed by GitHub
parent 67bc16b46d
commit eb7a62ff77
11 changed files with 154 additions and 250 deletions
+8 -13
View File
@@ -1,8 +1,7 @@
"use server";
import { revalidatePath } from "next/cache";
import { auth } from "@/auth.config";
import { apiBaseUrl, parseStringify } from "@/lib";
import { apiBaseUrl, getAuthHeaders, parseStringify } from "@/lib";
export const getCompliancesOverview = async ({
scanId,
@@ -13,7 +12,7 @@ export const getCompliancesOverview = async ({
region?: string | string[];
query?: string;
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/compliance-overviews`);
@@ -27,10 +26,7 @@ export const getCompliancesOverview = async ({
try {
const compliances = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await compliances.json();
const parsedData = parseStringify(data);
@@ -49,7 +45,7 @@ export const getComplianceOverviewMetadataInfo = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/compliance-overviews/metadata`);
@@ -65,14 +61,13 @@ export const getComplianceOverviewMetadataInfo = async ({
try {
const response = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
throw new Error(`Failed to fetch compliance overview metadata info: ${response.statusText}`);
throw new Error(
`Failed to fetch compliance overview metadata info: ${response.statusText}`,
);
}
const parsedData = parseStringify(await response.json());
+5 -12
View File
@@ -3,8 +3,7 @@
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { auth } from "@/auth.config";
import { apiBaseUrl, parseStringify } from "@/lib";
import { apiBaseUrl, getAuthHeaders, parseStringify } from "@/lib";
export const getFindings = async ({
page = 1,
@@ -13,7 +12,7 @@ export const getFindings = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (isNaN(Number(page)) || page < 1)
redirect("findings?include=resources,scan.provider");
@@ -32,10 +31,7 @@ export const getFindings = async ({
try {
const findings = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await findings.json();
const parsedData = parseStringify(data);
@@ -53,7 +49,7 @@ export const getMetadataInfo = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/findings/metadata`);
@@ -73,10 +69,7 @@ export const getMetadataInfo = async ({
try {
const response = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
+16 -28
View File
@@ -3,8 +3,12 @@
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { auth } from "@/auth.config";
import { apiBaseUrl, getErrorMessage, parseStringify } from "@/lib";
import {
apiBaseUrl,
getAuthHeaders,
getErrorMessage,
parseStringify,
} from "@/lib";
export const getInvitations = async ({
page = 1,
@@ -12,7 +16,7 @@ export const getInvitations = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (isNaN(Number(page)) || page < 1) redirect("/invitations");
@@ -31,10 +35,7 @@ export const getInvitations = async ({
try {
const invitations = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await invitations.json();
const parsedData = parseStringify(data);
@@ -48,7 +49,7 @@ export const getInvitations = async ({
};
export const sendInvite = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const email = formData.get("email");
const role = formData.get("role");
@@ -78,11 +79,7 @@ export const sendInvite = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "POST",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body,
});
const data = await response.json();
@@ -96,7 +93,7 @@ export const sendInvite = async (formData: FormData) => {
};
export const updateInvite = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const invitationId = formData.get("invitationId");
const invitationEmail = formData.get("invitationEmail");
@@ -137,11 +134,7 @@ export const updateInvite = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "PATCH",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body: JSON.stringify(body),
});
@@ -161,16 +154,13 @@ export const updateInvite = async (formData: FormData) => {
};
export const getInvitationInfoById = async (invitationId: string) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/tenants/invitations/${invitationId}`);
try {
const response = await fetch(url.toString(), {
method: "GET",
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await response.json();
@@ -183,7 +173,7 @@ export const getInvitationInfoById = async (invitationId: string) => {
};
export const revokeInvite = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const invitationId = formData.get("invitationId");
if (!invitationId) {
@@ -195,9 +185,7 @@ export const revokeInvite = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "DELETE",
headers: {
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
+16 -28
View File
@@ -3,8 +3,12 @@
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { auth } from "@/auth.config";
import { apiBaseUrl, getErrorMessage, parseStringify } from "@/lib";
import {
apiBaseUrl,
getAuthHeaders,
getErrorMessage,
parseStringify,
} from "@/lib";
import { ManageGroupPayload, ProviderGroupsResponse } from "@/types/components";
export const getProviderGroups = async ({
@@ -18,7 +22,7 @@ export const getProviderGroups = async ({
sort?: string;
filters?: Record<string, string | number>;
}): Promise<ProviderGroupsResponse | undefined> => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (isNaN(Number(page)) || page < 1) redirect("/manage-groups");
@@ -37,10 +41,7 @@ export const getProviderGroups = async ({
try {
const response = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
@@ -59,16 +60,13 @@ export const getProviderGroups = async ({
};
export const getProviderGroupInfoById = async (providerGroupId: string) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/provider-groups/${providerGroupId}`);
try {
const response = await fetch(url.toString(), {
method: "GET",
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
@@ -87,7 +85,7 @@ export const getProviderGroupInfoById = async (providerGroupId: string) => {
};
export const createProviderGroup = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const name = formData.get("name") as string;
const providersJson = formData.get("providers") as string;
@@ -127,11 +125,7 @@ export const createProviderGroup = async (formData: FormData) => {
const url = new URL(`${apiBaseUrl}/provider-groups`);
const response = await fetch(url.toString(), {
method: "POST",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body,
});
const data = await response.json();
@@ -148,7 +142,7 @@ export const updateProviderGroup = async (
providerGroupId: string,
formData: FormData,
) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const name = formData.get("name") as string;
const providersJson = formData.get("providers") as string;
@@ -179,11 +173,7 @@ export const updateProviderGroup = async (
const url = `${apiBaseUrl}/provider-groups/${providerGroupId}`;
const response = await fetch(url, {
method: "PATCH",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body: JSON.stringify(payload),
});
@@ -204,7 +194,7 @@ export const updateProviderGroup = async (
};
export const deleteProviderGroup = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const providerGroupId = formData.get("id");
if (!providerGroupId) {
@@ -216,9 +206,7 @@ export const deleteProviderGroup = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "DELETE",
headers: {
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
+7 -17
View File
@@ -2,8 +2,7 @@
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { auth } from "@/auth.config";
import { apiBaseUrl, parseStringify } from "@/lib";
import { apiBaseUrl, getAuthHeaders, parseStringify } from "@/lib";
export const getProvidersOverview = async ({
page = 1,
@@ -11,7 +10,7 @@ export const getProvidersOverview = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (isNaN(Number(page)) || page < 1) redirect("/providers-overview");
@@ -30,10 +29,7 @@ export const getProvidersOverview = async ({
try {
const response = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await response.json();
@@ -53,7 +49,7 @@ export const getFindingsByStatus = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (isNaN(Number(page)) || page < 1) redirect("/");
@@ -72,10 +68,7 @@ export const getFindingsByStatus = async ({
try {
const response = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
@@ -99,7 +92,7 @@ export const getFindingsBySeverity = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (isNaN(Number(page)) || page < 1) redirect("/");
@@ -118,10 +111,7 @@ export const getFindingsBySeverity = async ({
try {
const response = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
+26 -49
View File
@@ -3,8 +3,13 @@
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { auth } from "@/auth.config";
import { apiBaseUrl, getErrorMessage, parseStringify, wait } from "@/lib";
import {
apiBaseUrl,
getAuthHeaders,
getErrorMessage,
parseStringify,
wait,
} from "@/lib";
export const getProviders = async ({
page = 1,
@@ -12,7 +17,7 @@ export const getProviders = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (isNaN(Number(page)) || page < 1) redirect("/providers");
@@ -31,10 +36,7 @@ export const getProviders = async ({
try {
const providers = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await providers.json();
const parsedData = parseStringify(data);
@@ -48,17 +50,14 @@ export const getProviders = async ({
};
export const getProvider = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const providerId = formData.get("id");
const url = new URL(`${apiBaseUrl}/providers/${providerId}`);
try {
const providers = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await providers.json();
const parsedData = parseStringify(data);
@@ -71,7 +70,7 @@ export const getProvider = async (formData: FormData) => {
};
export const updateProvider = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const providerId = formData.get("providerId");
const providerAlias = formData.get("alias");
@@ -81,11 +80,7 @@ export const updateProvider = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "PATCH",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body: JSON.stringify({
data: {
type: "providers",
@@ -96,6 +91,7 @@ export const updateProvider = async (formData: FormData) => {
},
}),
});
const data = await response.json();
revalidatePath("/providers");
return parseStringify(data);
@@ -109,7 +105,7 @@ export const updateProvider = async (formData: FormData) => {
};
export const addProvider = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const providerType = formData.get("providerType") as string;
const providerUid = formData.get("providerUid") as string;
@@ -131,11 +127,7 @@ export const addProvider = async (formData: FormData) => {
const response = await fetch(url.toString(), {
method: "POST",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body: JSON.stringify(bodyData),
});
@@ -152,7 +144,7 @@ export const addProvider = async (formData: FormData) => {
};
export const addCredentialsProvider = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const url = new URL(`${apiBaseUrl}/providers/secrets`);
const secretName = formData.get("secretName");
@@ -230,11 +222,7 @@ export const addCredentialsProvider = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "POST",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body: JSON.stringify(bodyData),
});
const data = await response.json();
@@ -253,7 +241,7 @@ export const updateCredentialsProvider = async (
credentialsId: string,
formData: FormData,
) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const url = new URL(`${apiBaseUrl}/providers/secrets/${credentialsId}`);
const secretName = formData.get("secretName");
@@ -320,11 +308,7 @@ export const updateCredentialsProvider = async (
try {
const response = await fetch(url.toString(), {
method: "PATCH",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body: JSON.stringify(bodyData),
});
@@ -345,7 +329,7 @@ export const updateCredentialsProvider = async (
};
export const checkConnectionProvider = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const providerId = formData.get("providerId");
@@ -354,10 +338,7 @@ export const checkConnectionProvider = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "POST",
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await response.json();
await wait(2000);
@@ -371,7 +352,7 @@ export const checkConnectionProvider = async (formData: FormData) => {
};
export const deleteCredentials = async (secretId: string) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (!secretId) {
return { error: "Secret ID is required" };
@@ -382,9 +363,7 @@ export const deleteCredentials = async (secretId: string) => {
try {
const response = await fetch(url.toString(), {
method: "DELETE",
headers: {
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
@@ -413,7 +392,7 @@ export const deleteCredentials = async (secretId: string) => {
};
export const deleteProvider = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const providerId = formData.get("id");
if (!providerId) {
@@ -425,9 +404,7 @@ export const deleteProvider = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "DELETE",
headers: {
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
+16 -28
View File
@@ -3,8 +3,12 @@
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { auth } from "@/auth.config";
import { apiBaseUrl, getErrorMessage, parseStringify } from "@/lib";
import {
apiBaseUrl,
getAuthHeaders,
getErrorMessage,
parseStringify,
} from "@/lib";
export const getRoles = async ({
page = 1,
@@ -12,7 +16,7 @@ export const getRoles = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (isNaN(Number(page)) || page < 1) redirect("/roles");
@@ -31,10 +35,7 @@ export const getRoles = async ({
try {
const roles = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await roles.json();
const parsedData = parseStringify(data);
@@ -48,16 +49,13 @@ export const getRoles = async ({
};
export const getRoleInfoById = async (roleId: string) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/roles/${roleId}`);
try {
const response = await fetch(url.toString(), {
method: "GET",
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
@@ -74,7 +72,7 @@ export const getRoleInfoById = async (roleId: string) => {
};
export const addRole = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const name = formData.get("name") as string;
const groups = formData.getAll("groups[]") as string[];
@@ -118,11 +116,7 @@ export const addRole = async (formData: FormData) => {
const url = new URL(`${apiBaseUrl}/roles`);
const response = await fetch(url.toString(), {
method: "POST",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body,
});
@@ -139,7 +133,7 @@ export const addRole = async (formData: FormData) => {
};
export const updateRole = async (formData: FormData, roleId: string) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const name = formData.get("name") as string;
const groups = formData.getAll("groups[]") as string[];
@@ -184,11 +178,7 @@ export const updateRole = async (formData: FormData, roleId: string) => {
const url = new URL(`${apiBaseUrl}/roles/${roleId}`);
const response = await fetch(url.toString(), {
method: "PATCH",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body,
});
@@ -205,15 +195,13 @@ export const updateRole = async (formData: FormData, roleId: string) => {
};
export const deleteRole = async (roleId: string) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/roles/${roleId}`);
try {
const response = await fetch(url.toString(), {
method: "DELETE",
headers: {
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
+20 -40
View File
@@ -3,8 +3,12 @@
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { auth } from "@/auth.config";
import { apiBaseUrl, getErrorMessage, parseStringify } from "@/lib";
import {
apiBaseUrl,
getAuthHeaders,
getErrorMessage,
parseStringify,
} from "@/lib";
export const getScans = async ({
page = 1,
@@ -12,7 +16,7 @@ export const getScans = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (isNaN(Number(page)) || page < 1) redirect("/scans");
@@ -31,10 +35,7 @@ export const getScans = async ({
try {
const scans = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await scans.json();
const parsedData = parseStringify(data);
@@ -48,7 +49,7 @@ export const getScans = async ({
};
export const getScansByState = async () => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/scans`);
@@ -57,10 +58,7 @@ export const getScansByState = async () => {
try {
const response = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
@@ -83,16 +81,13 @@ export const getScansByState = async () => {
};
export const getScan = async (scanId: string) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/scans/${scanId}`);
try {
const scan = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await scan.json();
const parsedData = parseStringify(data);
@@ -106,8 +101,7 @@ export const getScan = async (scanId: string) => {
};
export const scanOnDemand = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const providerId = formData.get("providerId");
const scanName = formData.get("scanName") || undefined;
@@ -135,11 +129,7 @@ export const scanOnDemand = async (formData: FormData) => {
const response = await fetch(url.toString(), {
method: "POST",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers: headers,
body: JSON.stringify(requestBody),
});
@@ -164,7 +154,7 @@ export const scanOnDemand = async (formData: FormData) => {
};
export const scheduleDaily = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const providerId = formData.get("providerId");
@@ -173,11 +163,7 @@ export const scheduleDaily = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "POST",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body: JSON.stringify({
data: {
type: "daily-schedules",
@@ -205,7 +191,7 @@ export const scheduleDaily = async (formData: FormData) => {
};
export const updateScan = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const scanId = formData.get("scanId");
const scanName = formData.get("scanName");
@@ -215,11 +201,7 @@ export const updateScan = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "PATCH",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body: JSON.stringify({
data: {
type: "scans",
@@ -243,15 +225,13 @@ export const updateScan = async (formData: FormData) => {
};
export const getExportsZip = async (scanId: string) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/scans/${scanId}/report`);
try {
const response = await fetch(url.toString(), {
headers: {
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
+8 -7
View File
@@ -1,19 +1,20 @@
"use server";
import { auth } from "@/auth.config";
import { apiBaseUrl, getErrorMessage, parseStringify } from "@/lib";
import {
apiBaseUrl,
getAuthHeaders,
getErrorMessage,
parseStringify,
} from "@/lib";
export const getTask = async (taskId: string) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/tasks/${taskId}`);
try {
const response = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await response.json();
return parseStringify(data);
+16 -28
View File
@@ -3,8 +3,12 @@
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { auth } from "@/auth.config";
import { apiBaseUrl, getErrorMessage, parseStringify } from "@/lib";
import {
apiBaseUrl,
getAuthHeaders,
getErrorMessage,
parseStringify,
} from "@/lib";
export const getUsers = async ({
page = 1,
@@ -12,7 +16,7 @@ export const getUsers = async ({
sort = "",
filters = {},
}) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
if (isNaN(Number(page)) || page < 1) redirect("/users?include=roles");
@@ -31,10 +35,7 @@ export const getUsers = async ({
try {
const users = await fetch(url.toString(), {
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
const data = await users.json();
const parsedData = parseStringify(data);
@@ -48,7 +49,7 @@ export const getUsers = async ({
};
export const updateUser = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const userId = formData.get("userId") as string; // Ensure userId is a string
const userName = formData.get("name") as string | null;
@@ -75,11 +76,7 @@ export const updateUser = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "PATCH",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body: JSON.stringify({
data: {
type: "users",
@@ -102,7 +99,7 @@ export const updateUser = async (formData: FormData) => {
};
export const updateUserRole = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: true });
const userId = formData.get("userId") as string;
const roleId = formData.get("roleId") as string;
@@ -126,11 +123,7 @@ export const updateUserRole = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "PATCH",
headers: {
"Content-Type": "application/vnd.api+json",
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
body: JSON.stringify(requestBody),
});
@@ -152,7 +145,7 @@ export const updateUserRole = async (formData: FormData) => {
};
export const deleteUser = async (formData: FormData) => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const userId = formData.get("userId");
if (!userId) {
@@ -164,9 +157,7 @@ export const deleteUser = async (formData: FormData) => {
try {
const response = await fetch(url.toString(), {
method: "DELETE",
headers: {
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
@@ -192,16 +183,13 @@ export const deleteUser = async (formData: FormData) => {
};
export const getProfileInfo = async () => {
const session = await auth();
const headers = await getAuthHeaders({ contentType: false });
const url = new URL(`${apiBaseUrl}/users/me`);
try {
const response = await fetch(url.toString(), {
method: "GET",
headers: {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
},
headers,
});
if (!response.ok) {
+16
View File
@@ -1,11 +1,27 @@
import { getExportsZip } from "@/actions/scans";
import { getTask } from "@/actions/task";
import { auth } from "@/auth.config";
import { useToast } from "@/components/ui";
import { AuthSocialProvider, MetaDataProps, PermissionInfo } from "@/types";
export const baseUrl = process.env.AUTH_URL || "http://localhost:3000";
export const apiBaseUrl = process.env.API_BASE_URL;
export const getAuthHeaders = async (options?: { contentType?: boolean }) => {
const session = await auth();
const headers: Record<string, string> = {
Accept: "application/vnd.api+json",
Authorization: `Bearer ${session?.accessToken}`,
};
if (options?.contentType) {
headers["Content-Type"] = "application/vnd.api+json";
}
return headers;
};
export const getAuthUrl = (provider: AuthSocialProvider) => {
const config = {
google: {