javascriptnode.jsexpresscookies

Express Cookie Sent With Header and Saved in Storage but After Reload they are gone


I've searched almost every topic about this issue but still could not fixed the problem. Set-Cookie is avaliable in response headers, see below, with a little warning. I am using http therefore i have to set secure to false. I even tried "SameSite=lax|strict" but still could not solve the problem. Also the cookies is presence in the application tab but when I change my url or refresh it the cookies are removed.

I start request with the function below and after this function I am sending the cookies with options configured. I've also shared cors options policy below.

http://localhost:3000/ this is my url, I am also aware an issue that it can also caused because a front-end and back-end domain are different. Im not acquinted with web so deeply I dont know how do I check domains are the same.

By the way I am using chrome version 127 and on macos.

enter image description here

const signInHandler = async function(e){
try {
    await axios({
        method: "POST",
        url: "http://127.0.0.1:3000/api/v1/users/login",
        data:{
            name: userName.value,
            password: password.value
        },
        withCredentials: true
    });
    
    
    // window.setTimeout(() => {
    //     location.assign("/");
    // },500);

} catch (error) {
    console.log(error);
    
}

}

const sendToken = function(user, res){
const token = createJWTToken(user);

const cookieOptions = {
    expires: new Date(Date.now() + process.env.JWT_COOKIE_EXPIRES_IN * 24 * 60 * 60 * 1000),
    httpOnly: true,
    secure: false,
    sameSite: "None"
}

res.cookie("jwt", token, cookieOptions);  

Below are my cors options.

const corsOptions = {
origin: true,
credentials: true
}

app.use(cors(corsOptions));

Solution

  • I just solved my issue. The issue was my server was on http://127.0.0.1:3000/ but from the browser I was using localhost:3000. After changing the url to http://127.0.0.1:3000/ from browser the problem is solved.