Convert all SecurityClient API calls to hooks except auth

This commit is contained in:
Tuan Dang
2023-08-10 17:19:23 +07:00
parent 78802409bd
commit 2dba7847b6
45 changed files with 179 additions and 1377 deletions
@@ -1,23 +0,0 @@
import SecurityClient from "@app/components/utilities/SecurityClient";
/**
* This route lets us get all the projects of a certain user in an org.
* @param {*} req
* @param {*} res
* @returns
*/
const getOrganizationUserProjects = (req: { orgId: string }) =>
SecurityClient.fetchCall(`/api/v1/organization/${req.orgId}/my-workspaces`, {
method: "GET",
headers: {
"Content-Type": "application/json"
}
}).then(async (res) => {
if (res && res.status === 200) {
return (await res.json()).workspaces;
}
console.log("Failed to get projects of a user in an org");
return undefined;
});
export default getOrganizationUserProjects;
@@ -1,38 +0,0 @@
import SecurityClient from "@app/components/utilities/SecurityClient";
export interface IMembershipOrg {
_id: string;
user: {
email: string;
firstName: string;
lastName: string;
_id: string;
publicKey: string;
};
inviteEmail: string;
organization: string;
role: "owner" | "admin" | "member";
status: "invited" | "accepted";
deniedPermissions: any[];
}
/**
* This route lets us get all the users in an org.
* @param {object} obj
* @param {string} obj.orgId - organization Id
* @returns
*/
const getOrganizationUsers = ({ orgId }: { orgId: string }): Promise<IMembershipOrg[]> =>
SecurityClient.fetchCall(`/api/v1/organization/${orgId}/users`, {
method: "GET",
headers: {
"Content-Type": "application/json"
}
}).then(async (res) => {
if (res?.status === 200) {
return (await res.json()).users;
}
console.log("Failed to get org users");
return undefined;
});
export default getOrganizationUsers;
@@ -1,27 +0,0 @@
import SecurityClient from "@app/components/utilities/SecurityClient";
/**
* This function sends an email invite to a user to join an org
* @param {*} email
* @param {*} orgId
* @returns
*/
const addUserToOrg = (email: string, orgId: string) =>
SecurityClient.fetchCall("/api/v1/invite-org/signup", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
inviteeEmail: email,
organizationId: orgId
})
}).then(async (res) => {
if (res && res.status === 200) {
return res;
}
console.log("Failed to add a user to an org");
return undefined;
});
export default addUserToOrg;
@@ -1,23 +0,0 @@
import SecurityClient from "@app/components/utilities/SecurityClient";
/**
* This route lets us get the all the orgs of a certain user.
* @returns
*/
const getOrganizations = () => {
return SecurityClient.fetchCall("/api/v1/organization", {
method: "GET",
headers: {
"Content-Type": "application/json"
}
}).then(async (res) => {
if (res?.status === 200) {
const {organizations} = await res.json();
return organizations;
}
console.log("Failed to get orgs of a user");
return undefined;
});
}
export default getOrganizations;
@@ -1,13 +0,0 @@
import { apiRequest } from "@app/config/request";
/**
* Get the latest key pairs from a certain workspace
* @param {string} workspaceId
* @returns
*/
const getLatestFileKey = async ({ workspaceId }: { workspaceId: string }) => {
const { data } = await apiRequest.get(`/api/v1/key/${workspaceId}/latest`);
return data;
}
export default getLatestFileKey;
@@ -1,32 +0,0 @@
import SecurityClient from "@app/components/utilities/SecurityClient";
/**
* This route uplods the keys in an encrypted format.
* @param {*} workspaceId
* @param {*} userId
* @param {*} encryptedKey
* @param {*} nonce
* @returns
*/
const uploadKeys = (workspaceId: string, userId: string, encryptedKey: string, nonce: string) =>
SecurityClient.fetchCall(`/api/v1/key/${workspaceId}`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
key: {
userId,
encryptedKey,
nonce
}
})
}).then(async (res) => {
if (res && res.status === 200) {
return res;
}
console.log("Failed to upload keys for a new user");
return undefined;
});
export default uploadKeys;
+6 -6
View File
@@ -1,10 +1,11 @@
import { useEffect } from "react";
import { useRouter } from "next/router";
import getOrganizations from "./api/organization/getOrgs";
import { useGetOrganizations } from "@app/hooks/api";
export default function DashboardRedirect() {
const router = useRouter();
const { data: userOrgs } = useGetOrganizations();
/**
* Here we forward to the default workspace if a user opens this url
@@ -16,11 +17,10 @@ export default function DashboardRedirect() {
try {
if (localStorage.getItem("orgData.id")) {
router.push(`/org/${localStorage.getItem("orgData.id")}/overview`);
} else {
const userOrgs = await getOrganizations();
userOrg = userOrgs[0]._id;
router.push(`/org/${userOrg}/overview`);
}
} else if (userOrgs) {
userOrg = userOrgs[0]._id;
router.push(`/org/${userOrg}/overview`);
}
} catch (error) {
console.log("Error - Not logged in yet");
}
@@ -11,14 +11,18 @@ import AddProjectMemberDialog from "@app/components/basic/dialog/AddProjectMembe
import ProjectUsersTable from "@app/components/basic/table/ProjectUsersTable";
import guidGenerator from "@app/components/utilities/randomId";
import { Input } from "@app/components/v2";
import { useAddUserToWorkspace,useGetUser , useGetWorkspaceUsers } from "@app/hooks/api";
import { useOrganization } from "@app/context";
import {
useAddUserToWorkspace,
useGetOrgUsers,
useGetUser,
useGetWorkspaceUsers} from "@app/hooks/api";
import { uploadWsKey } from "@app/hooks/api/keys/queries";
import {
decryptAssymmetric,
encryptAssymmetric
} from "../../../../components/utilities/cryptography/crypto";
import getOrganizationUsers from "../../../api/organization/GetOrgUsers";
import uploadKeys from "../../../api/workspace/uploadKeys";
interface UserProps {
firstName: string;
@@ -44,6 +48,9 @@ export default function Users() {
const workspaceId = router.query.id as string;
const { data: user } = useGetUser();
const { currentOrg } = useOrganization();
const { data: orgUsers } = useGetOrgUsers(currentOrg?._id ?? "");
const { data: workspaceUsers } = useGetWorkspaceUsers(workspaceId);
const { mutateAsync: addUserToWorkspaceMutateAsync } = useAddUserToWorkspace();
@@ -62,7 +69,7 @@ export default function Users() {
const [orgUserList, setOrgUserList] = useState<any[]>([]);
useEffect(() => {
if (user && workspaceUsers) {
if (user && workspaceUsers && orgUsers) {
(async () => {
setPersonalEmail(user.email);
@@ -82,10 +89,6 @@ export default function Users() {
setIsUserListLoading(false);
// This is needed to know wha users from an org (if any), we are able to add to a certain project
const orgUsers = await getOrganizationUsers({
orgId: String(localStorage.getItem("orgData.id"))
});
setOrgUserList(orgUsers);
setEmail(
orgUsers
@@ -98,7 +101,7 @@ export default function Users() {
);
})();
}
}, [user, workspaceUsers]);
}, [user, workspaceUsers, orgUsers]);
const closeAddModal = () => {
setIsAddOpen(false);
@@ -143,7 +146,12 @@ export default function Users() {
privateKey: PRIVATE_KEY
});
uploadKeys(workspaceId, result.invitee._id, ciphertext, nonce);
await uploadWsKey({
workspaceId,
userId: result.invitee._id,
encryptedKey: ciphertext,
nonce
});
}
setEmail("");
setIsAddOpen(false);
@@ -1,18 +0,0 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import Head from "next/head";
import { CreateServiceAccountPage } from "@app/views/Settings/CreateServiceAccountPage";
export default function ServiceAccountPage() {
return (
<>
<Head>
<title>Edit Service Account</title>
<link rel="icon" href="/infisical.ico" />
</Head>
<CreateServiceAccountPage />
</>
);
}
ServiceAccountPage.requireAuth = true;
+2 -2
View File
@@ -12,9 +12,9 @@ import InitialSignupStep from "@app/components/signup/InitialSignupStep";
import TeamInviteStep from "@app/components/signup/TeamInviteStep";
import UserInfoStep from "@app/components/signup/UserInfoStep";
import SecurityClient from "@app/components/utilities/SecurityClient";
import { fetchOrganizations } from "@app/hooks/api/organization/queries";
import { useFetchServerStatus } from "@app/hooks/api/serverDetails";
import checkEmailVerificationCode from "@app/pages/api/auth/CheckEmailVerificationCode";
import getOrganizations from "@app/pages/api/organization/getOrgs";
/**
* @returns the signup page
@@ -37,7 +37,7 @@ export default function SignUp() {
useEffect(() => {
const tryAuth = async () => {
try {
const userOrgs = await getOrganizations();
const userOrgs = await fetchOrganizations();
router.push(`/org/${userOrgs[0]._id}/overview`);
} catch (error) {
console.log("Error - Not logged in yet");
+2 -3
View File
@@ -26,8 +26,7 @@ import SecurityClient from "@app/components/utilities/SecurityClient";
import {
useGetCommonPasswords
} from "@app/hooks/api";
import getOrganizations from "@app/pages/api/organization/getOrgs";
import getOrganizationUserProjects from "@app/pages/api/organization/GetOrgUserProjects";
import { fetchOrganizations } from "@app/hooks/api/organization/queries";
import completeAccountInformationSignupInvite from "./api/auth/CompleteAccountInformationSignupInvite";
import verifySignupInvite from "./api/auth/VerifySignupInvite";
@@ -169,7 +168,7 @@ export default function SignupInvite() {
privateKey
});
const userOrgs = await getOrganizations();
const userOrgs = await fetchOrganizations();
const orgId = userOrgs[0]._id;
localStorage.setItem("orgData.id", orgId);