Cookie not set in browser but visible in postman/thunderclient | cookie problem



This problem can occur due to following reasons

In a cross-origin scenario, by default, browsers prevent setting cookies for domains that don't match the current domain from which the frontend code is served. However, there are ways to enable this behavior:

Backend


const cors = require('cors'); const express = require('express'); const app = express(); // Enable CORS for a specific origin (replace 'http://frontend-domain' with your frontend's domain) const corsOptions = { origin: 'http://frontend-domain', credentials: true, // Enable sending cookies with requests }; app.use(cors(corsOptions));



res.cookie('jwt', token, { httpOnly: true, secure: true, // Enable secure flag if using HTTPS sameSite: 'strict', domain: '.signup-domain.com', // Specify the domain });
Frontend Sideres.cookie('jwt', token, { httpOnly: true, secure: true, // Enable secure flag if using HTTPS sameSite: 'strict', domain: '.signup-domain.com', // Specify the domain });



Comments