Me gustaría enviar 'cookieValue' a mi servidor, pero no estoy muy seguro de cómo hacerlo y no he podido encontrar buenos recursos en línea.
Actualmente, no creo que mi 'cookieValue' realmente tenga un valor cuando intento enviarlo a mi servidor. También recibo una advertencia que dice que 'cookieValue' se declara pero nunca se usa en mi 'función findCookie'. A continuación tengo mi código actual, cualquier ayuda sería apreciada.
Código frontal:
import axios from 'axios' import { Outlet, Navigate } from "react-router"; import { useState } from 'react' let backEndResponse = null function SendCookie() { const [cookieValue, setCookieValue] = useState(""); if (document.cookie != null) { const cookieValue = document.cookie .split('; ') .find(row => row.startsWith('Auth=')) .split('=')[1]; axios.post('http://localhost:5000/auth', { cookieValue, }).then(getBackEndResponse => { return axios.get('http://localhost:5000/auth', { withCredentials: true }).then((res) => { res.render(backEndResponse); }) }); } else { } } const useAuth = () => { if (backEndResponse) { const authorized = {loggedIn: true} return authorized && authorized.loggedIn; } else { const authorized = {loggedIn: false} return authorized && authorized.loggedIn; } }; const ProtectedRoutes = () => { const isAuth = useAuth(); return isAuth ? <Outlet/> : <Navigate to="/login" />; } export default ProtectedRoutesCódigo de fondo:
app.post('/auth', (req, res) => { const authCookie = req.body.cookieValue if (authCookie === result) { backEndResponse = true } else { backEndResponse = false } }); app.get('/auth', (req, res) => { res.render(backEndResponse) });Las cookies provienen de su backend y el navegador las enviará automáticamente con cualquier solicitud posterior al mismo backend. Su backend puede acceder a ellos como res.cookies . No necesita escribir ningún código adicional para eso.