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

292
Views
How to get cookies value from client service to server service in react js?

How to fetch cookies value from client service to server service in react js? The cookie successfully stored on the clent side but when i am trying console from server side it is showing undefined .

please help me !!

index.js (server side):

import express from "express";
import cors from "cors";
import bodyParser from "body-parser";
const app = express();
import cookieParser from "cookie-parser";
import connection from "./database/db.js";
import Router from "./routes/route.js";
import auth from "./middleware/auth.js";

const corsOptions = {
    origin: true, //included origin as true
    credentials: true, //included credentials as true
};

app.use(cors(corsOptions));
app.use(cookieParser());

app.use(bodyParser.json({ extended: true }))
app.use(bodyParser.urlencoded({ extended: true }))
app.use('/', Router);
app.use(express.json());

const PORT = 8000;
app.listen(PORT, () => {
    console.log(`server is running successfully at ${PORT}`);
})

connection();

Auth.js (Authentication middleware(server side)):

import jwt from 'jsonwebtoken';
import post from '../schema/post-schema.js'
import profile from '../schema/profile-schema.js';

const auth = async (req, res, next) => {
    try {

        const token = req.cookies['jwt'];
        console.log("The token is", token) //it shows undefined
        const verifyuser = jwt.verify(token, "mynameismohitkumarfromnationalinstituteoftechnologyagartala");
        const user = await profile.findOne({ _id: verifyuser._id });
        
        console.log(verifyuser);
        console.log(user);
        next();
    } catch (error) {
        res.status(401).json(error);
    }
}

export default auth;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Have a look at cookie options if the cookie is HttpOnly then you cant read the cookie from the client-side.

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!