OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

React JS not accepting cookies from express sever

  • Thread starter Thread starter Firas SCMP
  • Start date Start date
F

Firas SCMP

Guest
I'm sending cookies from express server (res.cookie()) but this ain't working with my front end even though I include {withCredentials:true} in the get requests but it just doesn't work in the browser, no cookies are set in the application tab in browser. BUT if I try the requests with postman the middleware works perfectly and cookies are shown. I tried different browsers and different devices, but none. cors config:

Code:
app.use(
  cors({
  
    credentials: true,
    origin: [
      "http://localhost:3000", 
    ],
    methods: ["GET", "POST"],

  })
);

cookie parser config:

Code:
app.use(cookieParser())

this is the get request to check if the user is already logged in :

Code:
    await axios
        .get("http://192.168.0.141:3001/login", { withCredentials: true })
        .then(async (response) => {

            if (response) {
                loggedIn = true
            }
        })
        .catch(async err => {

            loggedIn = false
        })

the middleware of jwt :

Code:
const validateToken = (req, res, next) => {
    const accessToken = req.cookies["access-token"]
    if (!accessToken) { return res.status(400).json({ error: "user not authenticated" }) }
    try {
        const validToken = jwt.verify(accessToken, "test");
        if (validToken) {
            req.authenticated = true
            return next();
        }
    } catch (error) {
        return res.status(400).json({ error: error });
    }
}

If you need more clarification please tell me , thank you for helping

<p>I'm sending cookies from express server (<code>res.cookie()</code>) but this ain't working with my front end even though I include <code>{withCredentials:true}</code> in the get requests but it just doesn't work in the browser, no cookies are set in the application tab in browser.
BUT if I try the requests with postman the middleware works perfectly and cookies are shown.
I tried different browsers and different devices, but none.
cors config:</p>
<pre><code>app.use(
cors({

credentials: true,
origin: [
"http://localhost:3000",
],
methods: ["GET", "POST"],

})
);
</code></pre>
<p>cookie parser config:</p>
<pre><code>app.use(cookieParser())
</code></pre>
<p>this is the get request to check if the user is already logged in :</p>
<pre><code> await axios
.get("http://192.168.0.141:3001/login", { withCredentials: true })
.then(async (response) => {

if (response) {
loggedIn = true
}
})
.catch(async err => {

loggedIn = false
})
</code></pre>
<p>the middleware of jwt :</p>
<pre><code>const validateToken = (req, res, next) => {
const accessToken = req.cookies["access-token"]
if (!accessToken) { return res.status(400).json({ error: "user not authenticated" }) }
try {
const validToken = jwt.verify(accessToken, "test");
if (validToken) {
req.authenticated = true
return next();
}
} catch (error) {
return res.status(400).json({ error: error });
}
}
</code></pre>
<p>If you need more clarification please tell me , thank you for helping</p>
 

Latest posts

M
Replies
0
Views
1
MOHAMED AMIIN ABDI AADAN
M
Top