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

cookies disappear after page reload on localhost

  • Thread starter Thread starter Cthulhu
  • Start date Start date
C

Cthulhu

Guest
when you enter the site, cookies are set and everything goes fine, but when you reload the page, the set cookies are deleted

response from login(back):

Code:
    export const sendAccessTokenAndRefreshToken = (response:ServerResponse, accesstoken:string, refreshtoken:string) => {

    response.writeHead(200, {
        'Content-Type': 'text/json; application/json',
        "Access-Control-Allow-Credentials": "true",
        "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,PATCH,OPTIONS",
        "Access-Control-Allow-Origin": "http://localhost:3000",
        "Access-Control-Allow-Headers": "Access-Control-Allow-Headers, Origin, X-Requested-With, Content-Type, Accept, Authorization",
        'Set-Cookie':  [`refreshtoken=${refreshtoken}; Secure; HttpOnly; SameSite=None; Path=/; Max-Age=99999999;`]
       
    });

    response.end(JSON.stringify({accesstoken}));

}

respons options (cors):

Code:
response.writeHead(200, {
    'Content-Type': 'text/json; application/json',
    "Access-Control-Allow-Credentials": "true",
    "Access-Control-Allow-Origin": "http://localhost:3000",
    "Access-Control-Expose-Headers": "Authorization",
    "Access-Control-Allow-Headers": "Access-Control-Allow-Headers, Origin, X-Requested-With, Content-Type, Accept, Authorization",
});

response.end(JSON.stringify({message: 'Cors Work!'}));

when login is done:

enter image description here

after site reload:

enter image description here

in this picture you can see when sending a post request, cookies are displayed, but after a reboot it disappears

enter image description here

front login.js:

Code:
    const body = {

    login: login_input.value,
    password: password_input.value

}

fetch('http://127.0.0.1:3000/login', {
    method: 'POST',
    mode: 'cors',
    headers: {
        'Content-Type': 'application/json'
    },
    credentials: 'include',
    body: JSON.stringify(body)
})
.then((response) => response.json())
.then((json) => {
    localStorage.setItem('token', json.accesstoken);

How to solve this problem? I tried to use the cors browser extension but even that did not help

how to solve this problem without resorting to extraneous frameworks?

<p>when you enter the site, cookies are set and everything goes fine, but when you reload the page, the set cookies are deleted</p>
<p>response from login(back):</p>
<pre><code> export const sendAccessTokenAndRefreshToken = (response:ServerResponse, accesstoken:string, refreshtoken:string) => {

response.writeHead(200, {
'Content-Type': 'text/json; application/json',
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,PATCH,OPTIONS",
"Access-Control-Allow-Origin": "http://localhost:3000",
"Access-Control-Allow-Headers": "Access-Control-Allow-Headers, Origin, X-Requested-With, Content-Type, Accept, Authorization",
'Set-Cookie': [`refreshtoken=${refreshtoken}; Secure; HttpOnly; SameSite=None; Path=/; Max-Age=99999999;`]

});

response.end(JSON.stringify({accesstoken}));

}
</code></pre>
<p>respons options (cors):</p>
<pre><code>response.writeHead(200, {
'Content-Type': 'text/json; application/json',
"Access-Control-Allow-Credentials": "true",
"Access-Control-Allow-Origin": "http://localhost:3000",
"Access-Control-Expose-Headers": "Authorization",
"Access-Control-Allow-Headers": "Access-Control-Allow-Headers, Origin, X-Requested-With, Content-Type, Accept, Authorization",
});

response.end(JSON.stringify({message: 'Cors Work!'}));
</code></pre>
<p>when login is done:</p>
<p><a href="https://i.sstatic.net/9e0c1.png" rel="nofollow noreferrer">enter image description here</a></p>
<p>after site reload:</p>
<p><a href="https://i.sstatic.net/exWO8.png" rel="nofollow noreferrer">enter image description here</a></p>
<p>in this picture you can see when sending a post request, cookies are displayed, but after a reboot it disappears</p>
<p><a href="https://i.sstatic.net/SUK07.png" rel="nofollow noreferrer">enter image description here</a></p>
<p>front login.js:</p>
<pre><code> const body = {

login: login_input.value,
password: password_input.value

}

fetch('http://127.0.0.1:3000/login', {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
credentials: 'include',
body: JSON.stringify(body)
})
.then((response) => response.json())
.then((json) => {
localStorage.setItem('token', json.accesstoken);
</code></pre>
<p>How to solve this problem? I tried to use the cors browser extension but even that did not help</p>
<p>how to solve this problem without resorting to extraneous frameworks?</p>
 

Latest posts

Online statistics

Members online
1
Guests online
6
Total visitors
7
Top