From ff308d124e18260cba7f8d1bb7c9a3bde7b6d885 Mon Sep 17 00:00:00 2001 From: Mohit Kalshan Date: Thu, 13 Feb 2025 15:57:28 +0530 Subject: [PATCH] login issue fixes --- src/lib/https.ts | 50 +++++++---------------------------- src/redux/slices/authSlice.ts | 17 ++++++------ 2 files changed, 18 insertions(+), 49 deletions(-) diff --git a/src/lib/https.ts b/src/lib/https.ts index cd3d31b..5369fe1 100644 --- a/src/lib/https.ts +++ b/src/lib/https.ts @@ -1,47 +1,15 @@ -// import axios from 'axios'; +import axios from "axios"; -// const http = axios.create({ -// baseURL: process.env.REACT_APP_BACKEND_URL, -// }); -// console.log(process.env.REACT_APP_BACKEND_URL); -// http.interceptors.request.use((config) => { -// const authToken = localStorage.getItem('authToken'); -// if (authToken) { -// config.headers.Authorization = authToken; -// } - -// return config; -// }); - -// export default http; - -//Eknoor singh -//date:- 10-Feb-2025 -//Made different functions for calling different backends and sent them in a function for clarity - -import axios, { AxiosInstance } from "axios"; - -const backendHttp = axios.create({ +const http = axios.create({ baseURL: process.env.REACT_APP_BACKEND_URL, }); +http.interceptors.request.use((config) => { + const authToken = localStorage.getItem("authToken"); + if (authToken) { + config.headers.Authorization = authToken; + } -// Axios instance for the local API -const apiHttp = axios.create({ - baseURL: "http://localhost:5000/api", + return config; }); -// Add interceptors to both instances -const addAuthInterceptor = (instance: AxiosInstance) => { - instance.interceptors.request.use((config) => { - const authToken = localStorage.getItem("authToken"); - if (authToken) { - config.headers.Authorization = `Bearer ${authToken}`; // <-- Add "Bearer " - } - return config; - }); -}; - -addAuthInterceptor(backendHttp); -addAuthInterceptor(apiHttp); - -export { backendHttp, apiHttp }; +export default http; diff --git a/src/redux/slices/authSlice.ts b/src/redux/slices/authSlice.ts index 6984d9a..9251e07 100644 --- a/src/redux/slices/authSlice.ts +++ b/src/redux/slices/authSlice.ts @@ -1,6 +1,5 @@ import { createSlice, createAsyncThunk, PayloadAction } from "@reduxjs/toolkit"; -import axios from "axios"; -import { backendHttp, apiHttp } from "../../lib/https"; +import http from "../../lib/https"; import { toast } from "react-toastify"; // Define types for state @@ -45,7 +44,8 @@ export const loginUser = createAsyncThunk< { rejectValue: string } >("auth/login", async ({ email, password }, { rejectWithValue }) => { try { - const response = await apiHttp.post("auth/login", { + // this is endpoint not action name + const response = await http.post("admin/login", { email, password, }); @@ -72,7 +72,7 @@ export const registerUser = createAsyncThunk< { rejectValue: string } >("auth/signup", async (data, { rejectWithValue }) => { try { - const response = await apiHttp.post("auth/signup", data); + const response = await http.post("auth/signup", data); return response.data; } catch (error: any) { return rejectWithValue( @@ -91,7 +91,7 @@ export const adminList = createAsyncThunk< { rejectValue: string } >("/auth", async (_, { rejectWithValue }) => { try { - const response = await apiHttp.get("/auth"); + const response = await http.get("/auth"); console.log(response?.data?.data); return response?.data?.data?.map( (admin: { @@ -123,7 +123,7 @@ export const deleteAdmin = createAsyncThunk< { rejectValue: string } >("deleteAdmin", async (id, { rejectWithValue }) => { try { - const response = await apiHttp.delete(`/auth/${id}`); + const response = await http.delete(`/auth/${id}`); toast.success(response.data?.message); return id; } catch (error: any) { @@ -140,7 +140,7 @@ export const updateAdmin = createAsyncThunk( { rejectWithValue } ) => { try { - const response = await apiHttp.put(`/auth/${id}`, { name, role }); + const response = await http.put(`/auth/${id}`, { name, role }); toast.success("Admin updated successfully"); console.log(response?.data); return response?.data; @@ -164,7 +164,7 @@ export const fetchAdminProfile = createAsyncThunk< const token = localStorage?.getItem("authToken"); if (!token) throw new Error("No token found"); - const response = await apiHttp?.get("/auth/profile", { + const response = await http?.get("/auth/profile", { headers: { Authorization: `Bearer ${token}` }, // Ensure 'Bearer' prefix }); @@ -185,6 +185,7 @@ export const fetchAdminProfile = createAsyncThunk< ); } }); +// TODO: create logout action and delete token then handle logout cases const initialState: AuthState = { user: null,