I’m having connection problems with my api in nodejs after I created a service layer in my react app.
This is the folder structure:
index.html
└── src
├── Components
│ ├── Pages
│ │ ├── Home.jsx
│ │ ├── Provider.jsx
│ │ ├── login.jsx
│ │ ├── signup.jsx
│ │ ...
│ ├── ProviderComponents
│ │ ├── WorkListProvider.jsx
│ │ ...
├── Context
│ └── UserContext.jsx
└── Services
└── Api
├── authService.js
├── config
│ └── axiosConfig.js
└── providerService.js
src/Service/Api/config/axiosConfig.js
import axios from "axios";
import Cookies from "js-cookie";
const token = Cookies.get("token");
// create axios instance
export const api = axios.create({
baseURL: import.meta.env.VITE_API_URL,
headers: {
Authorization : `Bearer ${token}`
},
});
const errorHandler = (error) => {
const statusCode = error.response?.status;
if (statusCode && statusCode !== 401) {
throw error
}
return Promise.reject(error);
};
api.interceptors.response.use(undefined, (error) => {
return errorHandler(error);
});
src/Service/Api/providerServices
import { api } from "./config/axiosConfig";
export const providerService = {
getJobs: async () => {
const response = await api.request({
url: "/provider/myJobs",
method: "GET",
});
return response.data;
},
// other services instances
}
call to providerService
in src/Components/providerComponents/WorkListProvider.jsx
:
useEffect(() => {
providerService
.getJobs()
.then((workList) => {
setWorkData(workList);
})
.catch((error) => {
console.error(error);
});
}, []);
src/Service/Api/authService
:
import { api } from "./config/axiosConfig";
export const authService = {
login: async (data) => {
const response = await api.request({
url: `/login`,
data,
method: "POST",
});
console.log(response.data)
return response.data;
},
// other instances services
};
use authServices.login()
in Login.jsx
const handleSubmit = (e) => {
e.preventDefault();
authService
.login(values)
.then((response) => {
Cookies.set("token", response.token, { expires: 1 });
changeLoggedIn(true);
navigate("/");
})
.catch((error) => setErrorMessage(error.response.data.data.error));
};
When I login, it returns a 500 error (internal server error) with a ‘jwt malformed’ message. But when I refresh the page, it’s fixed and i get a correct response from the server.
2
Answers
Think about the timing of this…
How can you use
Cookies.get("token")
before it is set? Secondly, you don’t want or need anAuthorization
header for logging in.I would make two changes…
In
authService
, use theauthApi
instance insteadThe issue you’re experiencing could be related to the timing of the token retrieval and the request being sent. When you log in, the token is retrieved from the cookies, but it’s possible that the API request is being sent before the token is available. This can result in a "jwt malformed" error because the token is either missing or invalid.
To fix this issue, you can modify your code to ensure that the token is available before sending the request. One approach is to use a promise-based approach to retrieve the token and then make the API request. Here’s an updated version of your code that implements this approach: