Hello, can you please fix this code [code]
- User
Hello, can you please fix this code ```import { useEffect, useState } from "react"; import Sidebar from "@components/admin/Sidebar"; import Link from "next/link"; import InsertPhotoIcon from "@mui/icons-material/InsertPhoto"; import ArrowBackIosRoundedIcon from "@mui/icons-material/ArrowBackIosRounded"; import { API_URL } from "src/helpers/network"; import secureLocalStorage from "react-secure-storage";
export default function AddBlog() { const [answer, setAnswer] = useState({ category: "", cover_image: "public/arrowLeft.png", title: "LLAL", content: "LALAL", }); const [selectedFile, setSelectedFile] = useState(null); const [categories, setCategories] = useState([]);
useEffect(() => { fetchCategories(); }, []);
const fetchCategories = async () => { try { const response = await fetch(API_URL + "/admin/blogs/category/list"); const data = await response.json(); setCategories(data); } catch (error) { console.error("Error fetching categories:", error); } };
const handleFileChange = (event) => { const file = event.target.files[0]; setSelectedFile(file); setAnswer((prev) => ({ ...prev, cover_image: file, })); };
console.log(answer);
const handleChanges = (e) => { const { name, value } = e.target; setAnswer((prev) => ({ ...prev, [name]: value, })); };
const handleSubmit = async (e) => { e.preventDefault(); console.log(selectedFile); const token = secureLocalStorage.getItem("token"); console.log("token", token); const formData = new FormData(); formData.append("cover_image", selectedFile); formData.append("title", answer.title); formData.append("content", answer.content); formData.append("category", answer.category); const createReq = await fetch(API_URL + "/admin/blogs/create", { method: "POST", headers: { Authorization: "Bearer " + token, }, body: formData, mode: "cors", }); }; return ( <> Add Blog Blog ID Title Description Category Article Choose Category Article {categories.map((category) => ( {category.name} ))} Date & Time Author Tags Blog Image Upload Image Format .JPG or .PNG (Max. 2MB) Save Blog Cancel Save as Draft </> ); }
- Assistant
- User
I apparently need bearer token
- Assistant
The original code you provided included a line to retrieve the bearer token from
secureLocalStorage:const token = secureLocalStorage.getItem("token");If you have a bearer token stored in
secureLocalStorage, you should be able to include it in your fetch request within theAuthorizationheader in the following way:const createReq = await fetch(API_URL + "/admin/blogs/create", { method: "POST", headers: { 'Authorization': `Bearer ${token}`, // Remove 'Content-Type' so the browser will add the correct one with boundary }, body: formData, mode: "cors", });You've already included the
Authorizationheader in your provided code sample. Just ensure that thesecureLocalStorageis the correct way to access the secure storage package that you're using and that the token is actually being stored there after the user logs in. - User
Combine the code
- Assistant
- User
I still got 401 unauthorized
- Assistant