Replace fetch with Axios in client
Replace `.then` with await syntax where possible.
GitLab: #142
Change-Id: I6c132f49f152afa7e20919a1c70c539f2ad54878
diff --git a/client/src/utils/auth.ts b/client/src/utils/auth.ts
index 8404eee..3704fe4 100644
--- a/client/src/utils/auth.ts
+++ b/client/src/utils/auth.ts
@@ -15,6 +15,7 @@
* License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
+import axios from 'axios';
import { passwordStrength } from 'check-password-strength';
import { HttpStatusCode } from 'jami-web-common';
@@ -39,68 +40,58 @@
const idToStrengthValueCode: StrengthValueCode[] = ['too_weak', 'weak', 'medium', 'strong'];
export async function isNameRegistered(name: string): Promise<boolean> {
- const url = new URL(`/ns/username/${name}`, apiUrl);
- const response = await fetch(url);
-
- switch (response.status) {
- case HttpStatusCode.Ok:
- return true;
- case HttpStatusCode.NotFound:
- return false;
- default:
- throw new Error(await response.text());
+ try {
+ await axios.get(`/ns/username/${name}`, {
+ baseURL: apiUrl,
+ });
+ return true;
+ } catch (e: any) {
+ if (e.response?.status !== HttpStatusCode.NotFound) {
+ throw e;
+ }
+ return false;
}
}
export function checkPasswordStrength(password: string): PasswordCheckResult {
const strengthResult: PasswordStrengthResult = passwordStrength(password);
- const checkResult: PasswordCheckResult = {
+ return {
strong: strengthResult.id === PasswordStrength.Strong.valueOf(),
valueCode: idToStrengthValueCode[strengthResult.id] ?? 'default',
};
-
- return checkResult;
}
export async function registerUser(username: string, password: string): Promise<void> {
- const url = new URL('/auth/new-account', apiUrl);
- const response: Response = await fetch(url, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({ username, password }),
- });
-
- if (response.status !== HttpStatusCode.Created) {
- throw new Error(await response.text());
- }
+ await axios.post(
+ '/auth/new-account',
+ { username, password },
+ {
+ baseURL: apiUrl,
+ }
+ );
}
export async function loginUser(username: string, password: string): Promise<string> {
- const url = new URL('/auth/login', apiUrl);
- const response = await fetch(url, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({ username, password }),
- });
-
- switch (response.status) {
- case HttpStatusCode.Ok:
- break;
- case HttpStatusCode.NotFound:
- throw new UsernameNotFound();
- case HttpStatusCode.Unauthorized:
- throw new InvalidPassword();
- default:
- throw new Error(await response.text());
+ try {
+ const { data } = await axios.post(
+ '/auth/login',
+ { username, password },
+ {
+ baseURL: apiUrl,
+ }
+ );
+ return data.accessToken;
+ } catch (e: any) {
+ switch (e.response?.status) {
+ case HttpStatusCode.NotFound:
+ throw new UsernameNotFound();
+ case HttpStatusCode.Unauthorized:
+ throw new InvalidPassword();
+ default:
+ throw e;
+ }
}
-
- const data: { accessToken: string } = await response.json();
- return data.accessToken;
}
export function getAccessToken(): string {
diff --git a/client/src/utils/constants.ts b/client/src/utils/constants.ts
index f5c5423..56c738a 100644
--- a/client/src/utils/constants.ts
+++ b/client/src/utils/constants.ts
@@ -21,4 +21,10 @@
export const jamiLogoDefaultSize = '512px';
-export const apiUrl = new URL(import.meta.env.VITE_API_URL);
+const apiUrl: string = import.meta.env.VITE_API_URL;
+
+if (!apiUrl) {
+ throw new Error('VITE_API_URL not defined');
+}
+
+export { apiUrl };