Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cookies are deleted when I close the browser?

I've been using window.localstorage to save some data without problem, the data was persisting between sessions.

I decided to switch to using cookies, using 'react-cookie', code as follows:

import Cookies from 'react-cookie';

export default class Auth {
    static STORAGE_KEY: string = "token";

    static cookies = new Cookies();


    public static getToken() {
        var toRet = this.cookies.get(Auth.STORAGE_KEY);
        return toRet;
    }

    public static setToken(token: string) {
        this.cookies.set(Auth.STORAGE_KEY, token, { path: '/' });
    }

    public static removeToken(): void {
        this.cookies.remove(Auth.STORAGE_KEY, { path: '/' });
    }
}

If I call 'setToken' the value set persists, however if I close the brower and open it again that data is lost.

My root render function has the cookies provider as per the webpage https://www.npmjs.com/package/react-cookie:

import { CookiesProvider } from 'react-cookie';
export class Layout extends React.Component<{}, {}> {
    public render() {
        return <CookiesProvider> ( some stuff ) </CookiesProvider>
like image 422
meds Avatar asked Sep 16 '17 06:09

meds


People also ask

Do cookies get deleted when browser is closed?

You can let sites remember information during your browsing session, but automatically delete the cookies when you close Chrome. On your computer, open Google Chrome. Settings. Cookies and other site data.

Why are my cookies being deleted automatically?

Google chrome has a cap on the number of cookies it allowed per domain . Once the total number of cookies in that domain exceeds that count, it deletes cookies!

What type of cookies are deleted when you close your browser?

Session cookies – text files that are stored in a temporary folder. Session cookies do not collect your personal data and are deleted when your browsing session ends.


1 Answers

The default cookie lifetime is “session”. You should set a maxAge:

this.cookies.set(Auth.STORAGE_KEY, token,
                 { path: '/', maxAge: 31536000 });

It’s in seconds.

like image 196
Ry- Avatar answered Sep 24 '22 23:09

Ry-