mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-23 20:42:02 +00:00
chore: update all providers API requests
This commit is contained in:
+12
-30
@@ -3,21 +3,15 @@
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { auth } from "@/auth.config";
|
||||
import { parseStringify } from "@/lib";
|
||||
|
||||
// Credentials for basic auth
|
||||
const username = process.env.API_USERNAME;
|
||||
const password = process.env.API_PASSWORD;
|
||||
|
||||
export const getProviders = async ({
|
||||
page = 1,
|
||||
query = "",
|
||||
sort = "",
|
||||
filters = {},
|
||||
}) => {
|
||||
const session = await auth();
|
||||
const tenantId = session?.user.tenantId;
|
||||
// const session = await auth();
|
||||
|
||||
if (isNaN(Number(page)) || page < 1) redirect("/providers");
|
||||
|
||||
@@ -38,8 +32,7 @@ export const getProviders = async ({
|
||||
try {
|
||||
const providers = await fetch(url.toString(), {
|
||||
headers: {
|
||||
"X-Tenant-ID": `${tenantId}`,
|
||||
Authorization: "Basic " + btoa(`${username}:${password}`),
|
||||
Accept: "application/vnd.api+json",
|
||||
},
|
||||
});
|
||||
const data = await providers.json();
|
||||
@@ -53,8 +46,7 @@ export const getProviders = async ({
|
||||
};
|
||||
|
||||
export const getProvider = async (formData: FormData) => {
|
||||
const session = await auth();
|
||||
const tenantId = session?.user.tenantId;
|
||||
// const session = await auth();
|
||||
const providerId = formData.get("id");
|
||||
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
@@ -63,8 +55,7 @@ export const getProvider = async (formData: FormData) => {
|
||||
try {
|
||||
const providers = await fetch(url.toString(), {
|
||||
headers: {
|
||||
"X-Tenant-ID": `${tenantId}`,
|
||||
Authorization: "Basic " + btoa(`${username}:${password}`),
|
||||
Accept: "application/vnd.api+json",
|
||||
},
|
||||
});
|
||||
const data = await providers.json();
|
||||
@@ -78,10 +69,9 @@ export const getProvider = async (formData: FormData) => {
|
||||
};
|
||||
|
||||
export const updateProvider = async (formData: FormData) => {
|
||||
const session = await auth();
|
||||
// const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
|
||||
const tenantId = session?.user.tenantId;
|
||||
const providerId = formData.get("providerId");
|
||||
const providerAlias = formData.get("alias");
|
||||
|
||||
@@ -91,9 +81,8 @@ export const updateProvider = async (formData: FormData) => {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
"X-Tenant-ID": `${tenantId}`,
|
||||
Authorization: "Basic " + btoa(`${username}:${password}`),
|
||||
"Content-Type": "application/vnd.api+json",
|
||||
Accept: "application/vnd.api+json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
data: {
|
||||
@@ -117,11 +106,9 @@ export const updateProvider = async (formData: FormData) => {
|
||||
};
|
||||
|
||||
export const addProvider = async (formData: FormData) => {
|
||||
const session = await auth();
|
||||
// const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
|
||||
const tenantId = session?.user.tenantId;
|
||||
|
||||
const providerType = formData.get("providerType");
|
||||
const providerId = formData.get("providerId");
|
||||
const providerAlias = formData.get("providerAlias");
|
||||
@@ -132,9 +119,8 @@ export const addProvider = async (formData: FormData) => {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-Tenant-ID": `${tenantId}`,
|
||||
Authorization: "Basic " + btoa(`${username}:${password}`),
|
||||
"Content-Type": "application/vnd.api+json",
|
||||
Accept: "application/vnd.api+json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
data: {
|
||||
@@ -159,9 +145,8 @@ export const addProvider = async (formData: FormData) => {
|
||||
};
|
||||
|
||||
export const checkConnectionProvider = async (formData: FormData) => {
|
||||
const session = await auth();
|
||||
// const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
const tenantId = session?.user.tenantId;
|
||||
|
||||
const providerId = formData.get("id");
|
||||
|
||||
@@ -171,8 +156,7 @@ export const checkConnectionProvider = async (formData: FormData) => {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"X-Tenant-ID": `${tenantId}`,
|
||||
Authorization: "Basic " + btoa(`${username}:${password}`),
|
||||
Accept: "application/vnd.api+json",
|
||||
},
|
||||
});
|
||||
const data = await response.json();
|
||||
@@ -186,9 +170,8 @@ export const checkConnectionProvider = async (formData: FormData) => {
|
||||
};
|
||||
|
||||
export const deleteProvider = async (formData: FormData) => {
|
||||
const session = await auth();
|
||||
// const session = await auth();
|
||||
const keyServer = process.env.API_BASE_URL;
|
||||
const tenantId = session?.user.tenantId;
|
||||
|
||||
const providerId = formData.get("id");
|
||||
const url = new URL(`${keyServer}/providers/${providerId}`);
|
||||
@@ -197,8 +180,7 @@ export const deleteProvider = async (formData: FormData) => {
|
||||
const response = await fetch(url.toString(), {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"X-Tenant-ID": `${tenantId}`,
|
||||
Authorization: "Basic " + btoa(`${username}:${password}`),
|
||||
Accept: "application/vnd.api+json",
|
||||
},
|
||||
});
|
||||
const data = await response.json();
|
||||
|
||||
+25
-11
@@ -5,6 +5,13 @@ import { z } from "zod";
|
||||
|
||||
import { userMockData } from "./lib";
|
||||
|
||||
// const key = new TextEncoder().encode(process.env.AUTH_SECRET);
|
||||
// const SALT_ROUNDS = 10;
|
||||
|
||||
// export async function hashPassword(password: string) {
|
||||
// return hash(password, SALT_ROUNDS);
|
||||
// }
|
||||
|
||||
async function getUser(email: string, password: string): Promise<any | null> {
|
||||
// Check if the user exists in the userMockData array.
|
||||
const user = userMockData.find((user) => user.email === email);
|
||||
@@ -32,17 +39,22 @@ export const authConfig = {
|
||||
newUser: "/sign-up",
|
||||
},
|
||||
callbacks: {
|
||||
// authorized({ auth, request: { nextUrl } }) {
|
||||
// const isLoggedIn = !!auth?.user;
|
||||
// const isOnDashboard = nextUrl.pathname.startsWith("/");
|
||||
// if (isOnDashboard) {
|
||||
// if (isLoggedIn) return true;
|
||||
// return false; // Redirect unauthenticated users to login page
|
||||
// } else if (isLoggedIn) {
|
||||
// return Response.redirect(new URL("/", nextUrl));
|
||||
// }
|
||||
// return true;
|
||||
// },
|
||||
authorized({ auth, request: { nextUrl } }) {
|
||||
const isLoggedIn = !!auth?.user;
|
||||
const isOnDashboard = nextUrl.pathname.startsWith("/");
|
||||
const isSignUpPage = nextUrl.pathname === "/sign-up";
|
||||
|
||||
// Permitir acceso a /sign-up incluso si no está autenticado
|
||||
if (isSignUpPage) return true;
|
||||
|
||||
if (isOnDashboard) {
|
||||
if (isLoggedIn) return true;
|
||||
return false; // Redirigir usuarios no autenticados a la página de login
|
||||
} else if (isLoggedIn) {
|
||||
return Response.redirect(new URL("/", nextUrl));
|
||||
}
|
||||
return true;
|
||||
},
|
||||
|
||||
jwt({ token, user }) {
|
||||
if (user) {
|
||||
@@ -75,6 +87,8 @@ export const authConfig = {
|
||||
return null;
|
||||
}
|
||||
const { email, password } = parsedCredentials.data;
|
||||
console.log("email", email);
|
||||
console.log("password", password);
|
||||
|
||||
const user = await getUser(email, password);
|
||||
if (!user) return null;
|
||||
|
||||
@@ -31,6 +31,11 @@ export const AuthForm = ({ type }: { type: string }) => {
|
||||
defaultValues: {
|
||||
email: "",
|
||||
password: "",
|
||||
...(type === "sign-up" && {
|
||||
firstName: "",
|
||||
companyName: "",
|
||||
confirmPassword: "",
|
||||
}),
|
||||
},
|
||||
});
|
||||
|
||||
@@ -48,14 +53,16 @@ export const AuthForm = ({ type }: { type: string }) => {
|
||||
try {
|
||||
// Sign-up logic will be here.
|
||||
if (type === "sign-in") {
|
||||
console.log(data);
|
||||
dispatch({
|
||||
email: data.email.toLowerCase(),
|
||||
password: data.password,
|
||||
});
|
||||
}
|
||||
// if (type === "sign-up") {
|
||||
// const newUser = await signUp(data);
|
||||
// }
|
||||
if (type === "sign-up") {
|
||||
console.log(data);
|
||||
// const newUser = await signUp(data);
|
||||
}
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(error);
|
||||
|
||||
Reference in New Issue
Block a user