Merge branch 'develop' of https://git.digimantra.com/DigiMantra/digiev_frontend into feature/adminList

This commit is contained in:
Eknoor Singh 2025-02-13 16:11:22 +05:30
commit 45ad56bdf9
2 changed files with 18 additions and 49 deletions

View file

@ -1,47 +1,15 @@
// import axios from 'axios'; import axios from "axios";
// const http = axios.create({ 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({
baseURL: process.env.REACT_APP_BACKEND_URL, 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 return config;
const apiHttp = axios.create({
baseURL: "http://localhost:5000/api",
}); });
// Add interceptors to both instances export default http;
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 };

View file

@ -1,6 +1,5 @@
import { createSlice, createAsyncThunk, PayloadAction } from "@reduxjs/toolkit"; import { createSlice, createAsyncThunk, PayloadAction } from "@reduxjs/toolkit";
import axios from "axios"; import http from "../../lib/https";
import { backendHttp, apiHttp } from "../../lib/https";
import { toast } from "react-toastify"; import { toast } from "react-toastify";
// Define types for state // Define types for state
@ -46,7 +45,8 @@ export const loginUser = createAsyncThunk<
{ rejectValue: string } { rejectValue: string }
>("auth/login", async ({ email, password }, { rejectWithValue }) => { >("auth/login", async ({ email, password }, { rejectWithValue }) => {
try { try {
const response = await apiHttp.post("auth/login", { // this is endpoint not action name
const response = await http.post("admin/login", {
email, email,
password, password,
}); });
@ -73,7 +73,7 @@ export const registerUser = createAsyncThunk<
{ rejectValue: string } { rejectValue: string }
>("auth/signup", async (data, { rejectWithValue }) => { >("auth/signup", async (data, { rejectWithValue }) => {
try { try {
const response = await apiHttp.post("auth/signup", data); const response = await http.post("auth/signup", data);
return response.data; return response.data;
} catch (error: any) { } catch (error: any) {
return rejectWithValue( return rejectWithValue(
@ -92,7 +92,7 @@ export const adminList = createAsyncThunk<
{ rejectValue: string } { rejectValue: string }
>("/auth", async (_, { rejectWithValue }) => { >("/auth", async (_, { rejectWithValue }) => {
try { try {
const response = await apiHttp.get("/auth"); const response = await http.get("/auth");
console.log(response?.data?.data); console.log(response?.data?.data);
return response?.data?.data?.map( return response?.data?.data?.map(
(admin: { (admin: {
@ -124,7 +124,7 @@ export const deleteAdmin = createAsyncThunk<
{ rejectValue: string } { rejectValue: string }
>("deleteAdmin", async (id, { rejectWithValue }) => { >("deleteAdmin", async (id, { rejectWithValue }) => {
try { try {
const response = await apiHttp.delete(`/auth/${id}`); const response = await http.delete(`/auth/${id}`);
toast.success(response.data?.message); toast.success(response.data?.message);
return id; return id;
} catch (error: any) { } catch (error: any) {
@ -141,7 +141,7 @@ export const updateAdmin = createAsyncThunk(
{ rejectWithValue } { rejectWithValue }
) => { ) => {
try { try {
const response = await apiHttp.put(`/auth/${id}`, { name, role }); const response = await http.put(`/auth/${id}`, { name, role });
toast.success("Admin updated successfully"); toast.success("Admin updated successfully");
console.log(response?.data); console.log(response?.data);
return response?.data; return response?.data;
@ -165,7 +165,7 @@ export const fetchAdminProfile = createAsyncThunk<
const token = localStorage?.getItem("authToken"); const token = localStorage?.getItem("authToken");
if (!token) throw new Error("No token found"); 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 headers: { Authorization: `Bearer ${token}` }, // Ensure 'Bearer' prefix
}); });
@ -186,6 +186,7 @@ export const fetchAdminProfile = createAsyncThunk<
); );
} }
}); });
// TODO: create logout action and delete token then handle logout cases
const initialState: AuthState = { const initialState: AuthState = {
user: null, user: null,