From 43e6acea5c24378dbbbc587eb22e14da97192d22 Mon Sep 17 00:00:00 2001 From: karan Date: Wed, 21 Feb 2024 16:32:42 +0530 Subject: [PATCH 1/2] refactor: client credentials flow Signed-off-by: karan --- src/api/organization.ts | 4 +- src/components/Setting/index.tsx | 74 +++++++++---------------------- src/components/Setting/setting.ts | 18 +------- 3 files changed, 24 insertions(+), 72 deletions(-) diff --git a/src/api/organization.ts b/src/api/organization.ts index 73cf3ad8b..8a78fd7b3 100644 --- a/src/api/organization.ts +++ b/src/api/organization.ts @@ -190,8 +190,8 @@ export const spinupSharedAgent = async (data: object, orgId: string) => { }; export const getOrganizationRoles = async () => { - const url = `${apiRoutes.organizations.orgRoles}`; - + const orgId = await getFromLocalStorage(storageKeys.ORG_ID); + const url = `${apiRoutes.organizations.root}/${orgId}/roles`; const token = await getFromLocalStorage(storageKeys.TOKEN); const config = { diff --git a/src/components/Setting/index.tsx b/src/components/Setting/index.tsx index 80057a292..1a876b587 100644 --- a/src/components/Setting/index.tsx +++ b/src/components/Setting/index.tsx @@ -10,13 +10,12 @@ import { Roles } from '../../utils/enums/roles'; import BreadCrumbs from '../BreadCrumbs'; import { createCredentials, - deleteCredentials, getCredentials, } from './setting'; import { EmptyListMessage } from '../EmptyListComponent'; const Index = () => { - const [loading, setLoading] = useState(true); + const [loading, setLoading] = useState(true); const [clientId, setClientId] = useState(null); const [clientSecret, setClientSecret] = useState(null); const [success, setSuccess] = useState(null); @@ -25,6 +24,8 @@ const Index = () => { const [hideCopy, setHideCopy] = useState(true); const [userRoles, setUserRoles] = useState([]); const [orgnizationId, setOrgnizationId] = useState(null); + const [buttonDisplay, setButtonDisplay] = useState(true); + const [regenerate, setRegenerate] = useState(false); const createClientCredentials = async () => { const orgId = await getFromLocalStorage(storageKeys.ORG_ID); @@ -35,8 +36,8 @@ const Index = () => { const { data } = response as AxiosResponse; if (data?.statusCode === apiStatusCodes.API_STATUS_CREATED) { + setButtonDisplay(false); setHideCopy(false); - setClientId(data.data.clientId); setClientSecret(data.data.clientSecret); setSuccess(data.message); @@ -64,9 +65,11 @@ const Index = () => { const { data } = response as AxiosResponse; if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) { + setRegenerate(true); setHideCopy(true); setClientId(data.data.clientId); setClientSecret(data.data.clientSecret); + setButtonDisplay(true); } else { setClientId(null); setClientSecret(null); @@ -82,32 +85,6 @@ const Index = () => { } }; - const deleteClientCredentials = async () => { - const orgId = await getFromLocalStorage(storageKeys.ORG_ID); - - if (orgId) { - setLoading(true); - try { - const response = await deleteCredentials(); - const { data } = response as AxiosResponse; - - if (data?.statusCode === apiStatusCodes.API_STATUS_DELETED) { - setSuccess(data?.message); - await getClientCredentials(); - } - } catch (error) { - setFailure(error as string); - setLoading(false); - setClientId(null); - setClientSecret(null); - } finally { - setLoading(false); - } - } else { - setLoading(false); - } - }; - const getUserRoles = async () => { const orgRoles = await getFromLocalStorage(storageKeys.ORG_ROLES); const roles = orgRoles.split(','); @@ -175,22 +152,24 @@ const Index = () => { organization to the API. + {(userRoles.includes(Roles.OWNER) || userRoles.includes(Roles.ADMIN)) && (
- + {buttonDisplay && ( + + )}
)} @@ -246,17 +225,6 @@ const Index = () => { - {(userRoles.includes(Roles.OWNER) || - userRoles.includes(Roles.ADMIN)) && ( -
- -
- )} )} diff --git a/src/components/Setting/setting.ts b/src/components/Setting/setting.ts index 001f617ba..c0593c0fd 100644 --- a/src/components/Setting/setting.ts +++ b/src/components/Setting/setting.ts @@ -2,7 +2,7 @@ import { getFromLocalStorage } from "../../api/Auth"; import { storageKeys } from "../../config/CommonConstant"; import { getHeaderConfigs } from "../../config/GetHeaderConfigs"; import { apiRoutes } from "../../config/apiRoutes"; -import { axiosDelete, axiosGet, axiosPost } from "../../services/apiRequests"; +import { axiosGet, axiosPost } from "../../services/apiRequests"; export const getCredentials = async () => { const orgId = await getFromLocalStorage(storageKeys.ORG_ID); @@ -36,19 +36,3 @@ export const createCredentials = async () => { return err?.message; } }; - -export const deleteCredentials = async () => { - const orgId = await getFromLocalStorage(storageKeys.ORG_ID); - const url = `${apiRoutes.organizations.root}/${orgId}${apiRoutes.setting.setting}`; - - const axiosPayload = { - url, - config: await getHeaderConfigs(), - }; - try { - return await axiosDelete(axiosPayload); - } catch (error) { - const err = error as Error; - return err?.message; - } -}; From 62b798d86144cc56d6ee54cb2a3a6e2e2429dbf6 Mon Sep 17 00:00:00 2001 From: Nishad Date: Wed, 6 Mar 2024 16:02:25 +0530 Subject: [PATCH 2/2] Handled the condition for regenerate client secret for organization Signed-off-by: Nishad --- src/components/Setting/index.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/Setting/index.tsx b/src/components/Setting/index.tsx index 1a876b587..2bbebb60c 100644 --- a/src/components/Setting/index.tsx +++ b/src/components/Setting/index.tsx @@ -65,10 +65,10 @@ const Index = () => { const { data } = response as AxiosResponse; if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) { - setRegenerate(true); + setRegenerate(data?.data?.clientSecret ? true : false); setHideCopy(true); - setClientId(data.data.clientId); - setClientSecret(data.data.clientSecret); + setClientId(data?.data?.clientId); + setClientSecret(data?.data?.clientSecret); setButtonDisplay(true); } else { setClientId(null);