8000 refactor: client credentials flow by 16-karan · Pull Request #589 · credebl/studio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

refactor: client credentials flow #589

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/api/organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
78 changes: 23 additions & 55 deletions src/components/Setting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>(true);
const [clientId, setClientId] = useState<string | null>(null);
const [clientSecret, setClientSecret] = useState<string | null>(null);
const [success, setSuccess] = useState<string | null>(null);
Expand All @@ -25,6 +24,8 @@ const Index = () => {
const [hideCopy, setHideCopy] = useState<boolean>(true);
const [userRoles, setUserRoles] = useState<string[]>([]);
const [orgnizationId, setOrgnizationId] = useState<string | null>(null);
const [buttonDisplay, setButtonDisplay] = useState<boolean>(true);
const [regenerate, setRegenerate] = useState<boolean>(false);

const createClientCredentials = async () => {
const orgId = await getFromLocalStorage(storageKeys.ORG_ID);
Expand All @@ -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);
Expand Down Expand Up @@ -64,9 +65,11 @@ const Index = () => {
const { data } = response as AxiosResponse;

if (data?.statusCode === apiStatusCodes.API_STATUS_SUCCESS) {
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);
setClientSecret(null);
Expand All @@ -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(',');
Expand Down Expand Up @@ -175,22 +152,24 @@ const Index = () => {
organization to the API.
</span>
</div>

{(userRoles.includes(Roles.OWNER) ||
userRoles.includes(Roles.ADMIN)) && (
<div className="text-start items-center mt-4 sm:mt-0 shrink-0">
<Button
>
type="button"
disabled={Boolean(clientSecret)}
isProcessing={loading}
className={`text-base font-medium text-center text-white rounded-md focus:ring-4 focus:ring-primary-300 sm:w-auto dark:focus:ring-primary-800 ${
Boolean(clientSecret)
? 'bg-gray-400 dark:bg-gray-600 dark:text-white cursor-not-allowed'
: 'bg-primary-700 hover:!bg-primary-800 hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800'
}`}
>
Generate Client Secret{' '}
</Button>
{buttonDisplay && (
<Button
>
type="button"
isProcessing={loading}
className={
'text-base font-medium text-center text-white rounded-md focus:ring-4 focus:ring-primary-300 sm:w-auto dark:focus:ring-primary-800 bg-primary-700 hover:!bg-primary-800 hover:bg-primary-800 focus:ring-4 focus:ring-primary-300 dark:bg-primary-600 dark:hover:bg-primary-700 dark:focus:ring-primary-800'
}
>
{regenerate
? 'Regenerate Client Secret'
: 'Generate Client Secret'}{' '}
</Button>
)}
</div>
)}
</div>
Expand Down Expand Up @@ -246,17 +225,6 @@ const Index = () => {
</h1>
</div>
</div>
{(userRoles.includes(Roles.OWNER) ||
userRoles.includes(Roles.ADMIN)) && (
<div className="flex text-start sm:justify-end items-center w-auto sm:w-1/5 ">
<button
=> deleteClientCredentials()}
className="bg-red-100 border text-red-600 px-2 rounded-md text-center h-8"
>
Delete
</button>
</div>
)}
</div>
</>
)}
Expand Down
18 changes: 1 addition & 17 deletions src/components/Setting/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
};
0