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

216
Views
How to send cookies from server side to client side (MERN)

I create a cookie from server side and I want to send it to client side with axios call and set it dynamic

this is server side API and works fine.

router.post('/login', async (req, res) => {

    const {email, password} = req.body;

    const checkEmail = await User.findOne({where: {email: email}});
    
    // check email is correct
    if (checkEmail) {

        const checkPassword = await User.findOne({where: {email: checkEmail.email, password: password}})

        // Check if is password is correct
        if (checkPassword) {

            const user = checkPassword;

            const token = jwt.sign({id: user.id, email: user.email}, 'theSecretKey', {expiresIn: '48h'});

            res.cookie('jwt', token, {maxAge: 3600000, httpOnly: true}); // 1 hour

            return res.json({user});
        }

        return res.json({message: 'password is wrong'});

    }

    res.json({message: 'Email or password is wrong'});

});

And my problem with client side I want when I call this Api make a cookies direct to my client side from server side.

    const handleSubmit = async (event) => {

        event.preventDefault();

        const data = {
            email: email,
            password: password
        }

        console.log(data);

        // call axios
        await axios.post('/login', data, {
            headers: {
                'Content-Type': 'application/json',
            }
        }).then((res) => {

            console.log(res.data);

            if (res.data.user) {
                console.log(res.data.user);
            }

        });

    };

also I add withCredentials in axios

axios.create({
    baseURL: 'http://localhost:5000',
    withCredentials: true,
    credentials: 'include',
});
about 4 years ago · Juan Pablo Isaza
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!