login issue fixes

This commit is contained in:
Mohit kalshan 2025-02-13 15:57:28 +05:30
parent 53e4f76f22
commit ff308d124e
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({
// 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;

View file

@ -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,