Skip to content

Works wrong In cookie fallback. If once removeItem is called #5

@mzahidriaz

Description

@mzahidriaz

I've found that if localstorage is not available and this code falls back to cookie solution. in that case it always goes wrong. here it is how it goes wrong.

If used once like

$.localStorage('fbloginresult', null);

This will set the cookie expires option to -1. From going forward it always set to -1

File Code

this.setItem = function( key, value ) {
                value = JSON.stringify(value);
                return $.support[method] ? window[method].setItem(key, value) : $.cookie(options.cookiePrefix + key, value, options.cookieOptions);
            };

            this.removeItem = function( key ) {
                return $.support[method] ? window[method].removeItem(key) : $.cookie(options.cookiePrefix + key, null, $.extend(options.cookieOptions, {
                    expires: -1
                }));
            };

if (typeof key !== "undefined") {
                return typeof value !== "undefined" ? ( value === null ? this.removeItem(key) : this.setItem(key, value) ) : this.getItem(key);
            }

You see that this.removeItem is extending the cookieOptions and changing the original options passed to the calling method. Hence when it calls the this.setItem after this.removeItem the cookie options are now changed and always passed expiry as -1

So this.removeItem should be like this.

this.removeItem = function( key ) {
                return $.support[method] ? window[method].removeItem(key) : $.cookie(options.cookiePrefix + key, null, $.extend({}, options.cookieOptions, {
                    expires: -1
                }));
            };

So that it should not extend the original options passed to this function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions