Next.js Discord

Discord Forum

Can not pass cookie in request from api router

Unanswered
Northern Shoveler posted this in #help-forum
Open in Discord
Northern ShovelerOP
I need to pass cookie in the request send by react server components

1 Reply

Northern ShovelerOP
when I visit the backend API from the browser directly it returns the auth user info because req.user has a value

app.get("/user-data", isLoggedIn, (req, res) => { res.json({ user: req.user }); });

but I can not return it from fetch API or axios

import axios from "axios"; async function getData() { try { const response = await axios.get('http://localhost:5000/get-user/', { withCredentials: true // Send cookies with the request }); return { success: true, data: response.data }; } catch (error) { console.error('Error fetching data:', error); return { success: false, error: error.message }; } } const Page = async () => { const data = await getData(); return ( <div> {JSON.stringify(data)} </div> ); }; export default Page;