Fix incorrect admin login error handling
Change-Id: I68c338b0cb7b261db6c9240cbc5c1e253290e753
diff --git a/client/src/App.tsx b/client/src/App.tsx
index f5c36a7..86c226f 100644
--- a/client/src/App.tsx
+++ b/client/src/App.tsx
@@ -23,9 +23,7 @@
import { apiUrl } from './utils/constants';
export async function checkSetupStatus(): Promise<boolean> {
- const { data } = await axios.get('/setup/check', {
- baseURL: apiUrl,
- });
+ const { data } = await axios.get('/setup/check', { baseURL: apiUrl });
return data.isSetupComplete;
}
diff --git a/client/src/components/ContactList.jsx b/client/src/components/ContactList.jsx
index 578d3c9..1f98091 100644
--- a/client/src/components/ContactList.jsx
+++ b/client/src/components/ContactList.jsx
@@ -61,7 +61,7 @@
const getContactDetails = async () => {
const controller = new AbortController();
try {
- const data = await axiosInstance.get(`/contacts/${currentContact.id}`, {
+ const { data } = await axiosInstance.get(`/contacts/${currentContact.id}`, {
signal: controller.signal,
});
console.log('CONTACT LIST - DETAILS: ', data);
diff --git a/client/src/components/ConversationListItem.tsx b/client/src/components/ConversationListItem.tsx
index aeef7de..9fa1acc 100644
--- a/client/src/components/ConversationListItem.tsx
+++ b/client/src/components/ConversationListItem.tsx
@@ -117,7 +117,7 @@
const getContactDetails = async () => {
const controller = new AbortController();
try {
- const data = await axiosInstance.get(`/contacts/${userId}`, {
+ const { data } = await axiosInstance.get(`/contacts/${userId}`, {
signal: controller.signal,
});
console.log('CONTACT LIST - DETAILS: ', data);
diff --git a/client/src/components/UsernameChooser.jsx b/client/src/components/UsernameChooser.jsx
index 77c1d73..19b8380 100644
--- a/client/src/components/UsernameChooser.jsx
+++ b/client/src/components/UsernameChooser.jsx
@@ -33,18 +33,14 @@
useEffect(() => {
if (isInputValid(query)) {
setIsLoading(true);
- axios
- .get(`/ns/username/${query}`, {
- baseURL: apiUrl,
- })
- .then((res) => {
- setIsLoading(false);
- if (res.status === 200) {
- setData(res.data);
- } else {
- throw res.status;
- }
- });
+ axios.get(`/ns/username/${query}`, { baseURL: apiUrl }).then((res) => {
+ setIsLoading(false);
+ if (res.status === 200) {
+ setData(res.data);
+ } else {
+ throw res.status;
+ }
+ });
} else {
setError(400);
}
diff --git a/client/src/pages/SetupLogin.tsx b/client/src/pages/SetupLogin.tsx
index 4608a59..d34d366 100644
--- a/client/src/pages/SetupLogin.tsx
+++ b/client/src/pages/SetupLogin.tsx
@@ -25,6 +25,7 @@
import { checkSetupStatus } from '../App';
import { apiUrl } from '../utils/constants';
+import { InvalidPassword } from '../utils/errors';
export default function SetupLogin() {
const [isSetupComplete, setIsSetupComplete] = useState(false);
@@ -38,49 +39,21 @@
checkSetupStatus().then(setIsSetupComplete);
}, []);
- const adminCreation = async (password: string) => {
- let response: Response;
- try {
- response = await axios.post(
- '/setup/admin/create',
- { password },
- {
- baseURL: apiUrl,
- }
- );
- } catch (e) {
- throw new Error(`Admin creation failed`);
- }
-
- if (response.status !== HttpStatusCode.Created) {
- throw new Error('Admin creation failed');
- }
+ const registerAdmin = async (password: string) => {
+ await axios.post('/setup/admin/create', { password }, { baseURL: apiUrl });
};
- const adminLogin = async (password: string) => {
- let response: Response;
+ const loginAdmin = async (password: string) => {
try {
- response = await axios.post(
- '/setup/admin/login',
- { password },
- {
- baseURL: apiUrl,
- }
- );
- } catch (e) {
- throw new Error(`Admin login failed`);
+ const { data } = await axios.post('/setup/admin/login', { password }, { baseURL: apiUrl });
+ localStorage.setItem('adminAccessToken', data.accessToken);
+ } catch (e: any) {
+ if (e.response?.status === HttpStatusCode.Forbidden) {
+ throw new InvalidPassword();
+ } else {
+ throw e;
+ }
}
-
- if (response.status === HttpStatusCode.Forbidden) {
- throw new Error('Invalid password');
- }
-
- if (response.status !== HttpStatusCode.Ok) {
- throw new Error('Admin login failed');
- }
-
- const data: { accessToken: string } = await response.json();
- localStorage.setItem('adminAccessToken', data.accessToken);
};
const isValid = isSetupComplete || (password && password === passwordRepeat);
@@ -92,9 +65,9 @@
try {
if (!isSetupComplete) {
- await adminCreation(password);
+ await registerAdmin(password);
}
- await adminLogin(password);
+ await loginAdmin(password);
} catch (e) {
console.error(e);
navigate('/login');
diff --git a/client/src/utils/auth.ts b/client/src/utils/auth.ts
index 3704fe4..822771c 100644
--- a/client/src/utils/auth.ts
+++ b/client/src/utils/auth.ts
@@ -41,9 +41,7 @@
export async function isNameRegistered(name: string): Promise<boolean> {
try {
- await axios.get(`/ns/username/${name}`, {
- baseURL: apiUrl,
- });
+ await axios.get(`/ns/username/${name}`, { baseURL: apiUrl });
return true;
} catch (e: any) {
if (e.response?.status !== HttpStatusCode.NotFound) {
@@ -63,24 +61,12 @@
}
export async function registerUser(username: string, password: string): Promise<void> {
- await axios.post(
- '/auth/new-account',
- { username, password },
- {
- baseURL: apiUrl,
- }
- );
+ await axios.post('/auth/new-account', { username, password }, { baseURL: apiUrl });
}
export async function loginUser(username: string, password: string): Promise<string> {
try {
- const { data } = await axios.post(
- '/auth/login',
- { username, password },
- {
- baseURL: apiUrl,
- }
- );
+ const { data } = await axios.post('/auth/login', { username, password }, { baseURL: apiUrl });
return data.accessToken;
} catch (e: any) {
switch (e.response?.status) {