Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
How to send value between React.js Frontend and Express.js Backend

I would like to send 'cookieValue' to my backend but I'm not quite sure how to and haven't been able to find any good resources online.

Currently, I don't believe that my 'cookieValue' actually has a value when I try to send it to my backend. I am also getting a waring saying that 'cookieValue' is declared but never used in my 'findCookie function'. Below I have my current code, any help would be appreciated.

Front End Code:

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 ProtectedRoutes

Backend Code:

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)
});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Cookies come from your backend, and the browser will automatically send them back with any subsequent request to the same backend. Your backend can access them as res.cookies. You need not write any extra code for that.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda