3

An http only cookie uid has been set on the browser by the server. I need to find out whether this cookie exists and take some actions in my javascript. I know that http only cookies cannot be read by javascript. So I'm thinking of setting another non-http cookie loggedin with the same maxAge as that of uid whenever the latter is being set from server. Both will be set together and cleared together. Will this approach work? Are there any caveats to this?

Jophin Joseph
  • 2,864
  • 4
  • 27
  • 40

1 Answers1

0

Take a look at the answers here

My approach was to try and overwrite the cookie and it worked in Edge, Chrome and Firefox(don't know about other browsers)

This is the code I used:

private doesHttpOnlyCookieExist(cookieName: string) {
    var d = new Date();
    d.setTime(d.getTime() + (1000));
    var expires = "expires=" + d.toUTCString();

    document.cookie = cookieName + "=new_value;path=/;" + expires;
    return document.cookie.indexOf(cookieName + '=') == -1;
 }

And this is the explanation:

You can indirectly check to see if it exists by trying to set it to a value with javascript if it can't be set, then the HTTP Only Cookie must be there (or the user is blocking cookies).

Lille
  • 103
  • 1
  • 3
  • 13