Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!