diff --git a/src/ui/services/config.ts b/src/ui/services/config.ts index fa30ad28d..152e538f1 100644 --- a/src/ui/services/config.ts +++ b/src/ui/services/config.ts @@ -6,25 +6,22 @@ import { getApiV1BaseUrl } from './apiConfig'; const setAttestationConfigData = async (setData: (data: QuestionFormData[]) => void) => { const apiV1Base = await getApiV1BaseUrl(); const url = new URL(`${apiV1Base}/config/attestation`); - await axios(url.toString()).then((response) => { - setData(response.data.questions); - }); + const response = await axios(url.toString()); + setData(response.data.questions); }; const setURLShortenerData = async (setData: (data: string) => void) => { const apiV1Base = await getApiV1BaseUrl(); const url = new URL(`${apiV1Base}/config/urlShortener`); - await axios(url.toString()).then((response) => { - setData(response.data); - }); + const response = await axios(url.toString()); + setData(response.data); }; const setEmailContactData = async (setData: (data: string) => void) => { const apiV1Base = await getApiV1BaseUrl(); const url = new URL(`${apiV1Base}/config/contactEmail`); - await axios(url.toString()).then((response) => { - setData(response.data); - }); + const response = await axios(url.toString()); + setData(response.data); }; const setUIRouteAuthData = async (setData: (data: UIRouteAuth) => void) => { @@ -32,9 +29,8 @@ const setUIRouteAuthData = async (setData: (data: UIRouteAuth) => void) => { const urlString = `${apiV1Base}/config/uiRouteAuth`; console.log(`URL: ${urlString}`); const url = new URL(urlString); - await axios(url.toString()).then((response) => { - setData(response.data); - }); + const response = await axios(url.toString()); + setData(response.data); }; export { setAttestationConfigData, setURLShortenerData, setEmailContactData, setUIRouteAuthData }; diff --git a/src/ui/services/repo.ts b/src/ui/services/repo.ts index 9b5a36323..98a2ecb5c 100644 --- a/src/ui/services/repo.ts +++ b/src/ui/services/repo.ts @@ -8,20 +8,18 @@ import { ServiceResult, getServiceError, errorResult, successResult } from './er const canAddUser = async (repoId: string, user: string, action: string) => { const apiV1Base = await getApiV1BaseUrl(); const url = new URL(`${apiV1Base}/repo/${repoId}`); - return axios - .get(url.toString(), getAxiosConfig()) - .then((response) => { - const repo = response.data; - if (action === 'authorise') { - return !repo.users.canAuthorise.includes(user); - } else { - return !repo.users.canPush.includes(user); - } - }) - .catch((error: any) => { - const { message } = getServiceError(error, 'Failed to validate repo permissions'); - throw new Error(message); - }); + try { + const response = await axios.get(url.toString(), getAxiosConfig()); + const repo = response.data; + if (action === 'authorise') { + return !repo.users.canAuthorise.includes(user); + } else { + return !repo.users.canPush.includes(user); + } + } catch (error: any) { + const { message } = getServiceError(error, 'Failed to validate repo permissions'); + throw new Error(message); + } }; class DupUserValidationError extends Error { @@ -79,11 +77,13 @@ const addUser = async (repoId: string, user: string, action: string): Promise { + try { + await axios.patch(url.toString(), data, getAxiosConfig()); + } catch (error: any) { const { message } = getServiceError(error, 'Failed to add user'); console.log(message); throw new Error(message); - }); + } } else { console.log('Duplicate user can not be added'); throw new DupUserValidationError('Duplicate user can not be added'); @@ -94,22 +94,26 @@ const deleteUser = async (user: string, repoId: string, action: string): Promise const apiV1Base = await getApiV1BaseUrl(); const url = new URL(`${apiV1Base}/repo/${repoId}/user/${action}/${user}`); - await axios.delete(url.toString(), getAxiosConfig()).catch((error: any) => { + try { + await axios.delete(url.toString(), getAxiosConfig()); + } catch (error: any) { const { message } = getServiceError(error, 'Failed to remove user'); console.log(message); throw new Error(message); - }); + } }; const deleteRepo = async (repoId: string): Promise => { const apiV1Base = await getApiV1BaseUrl(); const url = new URL(`${apiV1Base}/repo/${repoId}/delete`); - await axios.delete(url.toString(), getAxiosConfig()).catch((error: any) => { + try { + await axios.delete(url.toString(), getAxiosConfig()); + } catch (error: any) { const { message } = getServiceError(error, 'Failed to delete repository'); console.log(message); throw new Error(message); - }); + } }; export { addUser, deleteUser, getRepos, getRepo, addRepo, deleteRepo }; diff --git a/src/ui/views/Login/Login.tsx b/src/ui/views/Login/Login.tsx index 72962a5f8..f7b58752f 100644 --- a/src/ui/views/Login/Login.tsx +++ b/src/ui/views/Login/Login.tsx @@ -36,20 +36,21 @@ const Login: React.FC = () => { const [usernamePasswordMethod, setUsernamePasswordMethod] = useState(''); useEffect(() => { - getBaseUrl().then((baseUrl) => { - axios.get(`${baseUrl}/api/auth/config`).then((response) => { - const usernamePasswordMethod = response.data.usernamePasswordMethod; - const otherMethods = response.data.otherMethods; + const fetchAuthConfig = async () => { + const baseUrl = await getBaseUrl(); + const response = await axios.get(`${baseUrl}/api/auth/config`); + const usernamePasswordMethod = response.data.usernamePasswordMethod; + const otherMethods = response.data.otherMethods; - setUsernamePasswordMethod(usernamePasswordMethod); - setAuthMethods(otherMethods); + setUsernamePasswordMethod(usernamePasswordMethod); + setAuthMethods(otherMethods); - // Automatically login if only one non-username/password method is enabled - if (!usernamePasswordMethod && otherMethods.length === 1) { - handleAuthMethodLogin(otherMethods[0]); - } - }); - }); + // Automatically login if only one non-username/password method is enabled + if (!usernamePasswordMethod && otherMethods.length === 1) { + await handleAuthMethodLogin(otherMethods[0]); + } + }; + fetchAuthConfig(); }, []); function validateForm(): boolean { @@ -58,40 +59,40 @@ const Login: React.FC = () => { ); } - function handleAuthMethodLogin(authMethod: string): void { - getBaseUrl().then((baseUrl) => { - window.location.href = `${baseUrl}/api/auth/${authMethod}`; - }); + async function handleAuthMethodLogin(authMethod: string): Promise { + const baseUrl = await getBaseUrl(); + window.location.href = `${baseUrl}/api/auth/${authMethod}`; } - function handleSubmit(event: FormEvent): void { + async function handleSubmit(event: FormEvent): Promise { event.preventDefault(); setIsLoading(true); - getBaseUrl().then((baseUrl) => { + try { + const baseUrl = await getBaseUrl(); const loginUrl = `${baseUrl}/api/auth/login`; - axios - .post(loginUrl, { username, password }, getAxiosConfig()) - .then(() => { + await axios.post(loginUrl, { username, password }, getAxiosConfig()); + window.sessionStorage.setItem('git.proxy.login', 'success'); + setMessage('Success!'); + setSuccess(true); + await authContext.refreshUser(); + navigate(0); + } catch (error: unknown) { + if (error instanceof AxiosError) { + if (error.response?.status === 307) { window.sessionStorage.setItem('git.proxy.login', 'success'); - setMessage('Success!'); - setSuccess(true); - authContext.refreshUser().then(() => navigate(0)); - }) - .catch((error: AxiosError) => { - if (error.response?.status === 307) { - window.sessionStorage.setItem('git.proxy.login', 'success'); - setGitAccountError(true); - } else if (error.response?.status === 403) { - setMessage(processAuthError(error, false)); - } else { - setMessage('You entered an invalid username or password...'); - } - }) - .finally(() => { - setIsLoading(false); - }); - }); + setGitAccountError(true); + } else if (error.response?.status === 403) { + setMessage(processAuthError(error, false)); + } else { + setMessage('You entered an invalid username or password...'); + } + } else { + setMessage('You entered an invalid username or password...'); + } + } finally { + setIsLoading(false); + } } if (gitAccountError) {