iam using nextjs14 as my frontend and iam using axios to perform api calls to my backend
here is my axios Instance:
import axios from "axios";
import { apiBaseURL, webBaseURL } from "config";
import { getCookie } from "cookies-next";
export const axiosServerBase = axios.create({
baseURL: apiBaseURL,
timeout: 30000,
headers: {
Authorization: `Bearer ${getCookie("user") && JSON.parse(getCookie("user") as string).token}`,
},
});
and iam using it in my API folder under the app
here is my example request
import { axiosServerBase } from "config";
export async function POST(request: Request): Promise<Response> {
console.log(request.headers);
try {
const body = await request.json();
const { data } = await axiosServerBase.post(
"/templates/save-email-template",
{
data: body,
}
);
return Response.json({ status: 200, data });
} catch (error: any) {
return Response.json({
status: 400,
message: error.response.data.message,
});
}
}
for some reason i have to extract the token from the headers via this line
request.headers?.get("authorization")
and send it manually in the headers however it is set in the axios instance
my question:
- is it right to set the authorization manually in every request?
- and if not what should i do?