From 7d43300724ab41cdad6bfb9da0a383e0d67799f4 Mon Sep 17 00:00:00 2001 From: victorAnumudu Date: Wed, 1 May 2024 14:42:43 +0100 Subject: [PATCH] added axios verb call function --- src/app/modules/auth/core/AxiosCallHelper.ts | 50 ++++++++++++++++++++ src/app/modules/auth/core/_requests.ts | 20 ++++++-- 2 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 src/app/modules/auth/core/AxiosCallHelper.ts diff --git a/src/app/modules/auth/core/AxiosCallHelper.ts b/src/app/modules/auth/core/AxiosCallHelper.ts new file mode 100644 index 0000000..5ecdbd6 --- /dev/null +++ b/src/app/modules/auth/core/AxiosCallHelper.ts @@ -0,0 +1,50 @@ +import axios from "axios"; + +export function postAuxEnd(uri:string, reqData:any):Promise { + // const endPoint = process.env.REACT_APP_USERS_ENDPOINT + uri; + const formData = new FormData(); + for (let value in reqData) { + formData.append(value, reqData[value]); + } + return axios.post(uri, reqData) + .then((response) => { + console.log(response); + // if (response.data.internal_return == "-9999") { + // localStorage.clear(); + // window.location.href = `/login?sessionExpired=true`; + // } + return response; + }) + .catch((error) => { + if (error.response) { + //response status is an error code + console.log( + "ERROR-------------------------------------------------------" + ); + console.log(error.response.status); + console.log( + "ERROR-------------------------------------------------------" + ); + } else if (error.request) { + //response not received though the request was sent + console.log( + "ERROR2-------------------------------------------------------" + ); + console.log(error?.request); + console.log( + "ERROR2-------------------------------------------------------" + ); + } else { + //an error occurred when setting up the request + console.log( + "ERROR3-------------------------------------------------------" + ); + console.log(error); + console.log( + "ERROR3-------------------------------------------------------" + ); + } + }); + } + + diff --git a/src/app/modules/auth/core/_requests.ts b/src/app/modules/auth/core/_requests.ts index 2c29b06..a64e73d 100644 --- a/src/app/modules/auth/core/_requests.ts +++ b/src/app/modules/auth/core/_requests.ts @@ -1,19 +1,29 @@ import axios from "axios"; import { AuthModel, UserModel } from "./_models"; +import { postAuxEnd } from "./AxiosCallHelper"; + const API_URL = import.meta.env.VITE_APP_API_URL; export const GET_USER_BY_ACCESSTOKEN_URL = `${API_URL}/verify_token`; export const LOGIN_URL = `${API_URL}/login`; +// export const LOGIN_URL = 'https://digifi-apidev.chiefsoft.net/digibko/v1/identity/token' export const REGISTER_URL = `${API_URL}/register`; export const REQUEST_PASSWORD_URL = `${API_URL}/forgot_password`; -// Server should return AuthModel +//Function to login user in // Server should return AuthModel export function login(email: string, password: string) { - return axios.post(LOGIN_URL, { - email, - password, - }); + // return axios.post(LOGIN_URL, { + // email, + // password, + // }); + + // let formData = new FormData() + // formData.append('username', email) + // formData.append('pass', password) + // return axios.post(LOGIN_URL, formData); + + return postAuxEnd(LOGIN_URL, {email, password}) } // Server should return AuthModel -- 2.34.1