最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cookies are deleted when I close the browser? - Stack Overflow

programmeradmin1浏览0评论

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 :

import { CookiesProvider } from 'react-cookie';
export class Layout extends React.Component<{}, {}> {
    public render() {
        return <CookiesProvider> ( some stuff ) </CookiesProvider>

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>
Share Improve this question edited Sep 16, 2017 at 6:27 Ry- 225k56 gold badges490 silver badges497 bronze badges asked Sep 16, 2017 at 6:17 medsmeds 22.9k42 gold badges171 silver badges335 bronze badges 1
  • Cookies don't get deleted automatically unless you are in incognito mode, or you have configures your browser that way. Either way, how do you expect to solve this problem using programming? – Nisarg Shah Commented Sep 16, 2017 at 6:19
Add a comment  | 

1 Answer 1

Reset to default 21

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.

发布评论

评论列表(0)

  1. 暂无评论