From d56678786b6b996532030c1869d723726e83e2ce Mon Sep 17 00:00:00 2001 From: "Siddhesh.More" Date: Mon, 10 Feb 2025 12:27:51 +0530 Subject: [PATCH] update api --- .env | 4 +++- src/Pages/Login.tsx | 42 ++++++++++++++++++++++++---------- src/Redux/Service/apiSlice.tsx | 4 ++-- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/.env b/.env index 905ee5e..90e2eef 100644 --- a/.env +++ b/.env @@ -1,2 +1,4 @@ -VITE_API_URL=https://ssa.betadelivery.com/apia +VITE_API_URL=https://ssa.betadelivery.com/apia/v1 +VITE_USER_NAME="Admin" +VITE_PASSWORD="71%@L%es^bUX94`J9XT*%4&^%tUU^%Q^ffgt" VITE_APP_NAME=MyViteApp \ No newline at end of file diff --git a/src/Pages/Login.tsx b/src/Pages/Login.tsx index d71bcfe..b8d4a68 100644 --- a/src/Pages/Login.tsx +++ b/src/Pages/Login.tsx @@ -29,25 +29,43 @@ const Login = () => { formState: { errors }, } = useForm() - const onSubmit = handleSubmit(async (data) => { - setIsLoading(true) + setIsLoading(true); + + // Encode Basic Auth Credentials + const username = import.meta.env.VITE_USER_NAME||''; // Replace with actual username + const password = import.meta.env.VITE_PASSWORD||''; // Replace with actual password + const basicAuth = btoa(`${username}:${password}`); // Encode to Base64 + try { - const response = await axios.post(`${import.meta.env.VITE_API_URL}/v1/login`, { - mobile_number: data.mobileNumber, - password: data.password, - }); - console.log('===================================='); + const response = await axios.post( + `${import.meta.env.VITE_API_URL}/v1/login`, + { + mobile_number: data.mobileNumber, + password: data.password, + }, + { + headers: { + Authorization: `Basic ${basicAuth}`, + "Content-Type": "application/json", + }, + } + ); + + console.log("===================================="); console.log(response); - console.log('===================================='); + console.log("===================================="); dispatch(setToken(String(response.data["access-token"]))); - } catch (error) { - console.error("Login failed", error); + if (error) { + + console.error("Login failed", error); + setIsLoading(false) + + } } }); - - + diff --git a/src/Redux/Service/apiSlice.tsx b/src/Redux/Service/apiSlice.tsx index af6c96a..cc8ae3a 100644 --- a/src/Redux/Service/apiSlice.tsx +++ b/src/Redux/Service/apiSlice.tsx @@ -4,7 +4,7 @@ import { logout } from "./authSlice"; // Import logout action from authSlice import { RootState } from "../Store"; const baseQuery = fetchBaseQuery({ - baseUrl: "https://api.example.com", + baseUrl: `${import.meta.env.VITE_API_URL}`, prepareHeaders: (headers, { getState }) => { const token = (getState() as RootState).auth.token; // Get token from Redux store if (token) { @@ -42,7 +42,7 @@ export const apiSlice = createApi({ - + }), });