Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to read cookies value by it's name angularjs

I've cookie in my application and I need to read it using angularJS ngCookies. When I exported the cookies from browser extension it looks like following json :

[
  {
    "domain": "localhost",
    "hostOnly": true,
    "httpOnly": false,
    "name": "JSESSIONID",
    "sameSite": "no_restriction",
    "secure": true,
    "session": true,
    "storeId": "0",
    "value": "00FC04BF082458FFE6F175C7E03F5712",
    "id": 18
  }
]

there can be more objects in this JSON along with 'JSESSIONID'. so I want to read only JSESSIONID's value.

my Code :

 var jsessionCookie = $cookies.get('JSESSIONID');
 console.log(" Cookies 'JSESSIONID' : "+jsessionCookie);

I'm getting undefined object.

like image 565
Enigma Avatar asked Dec 21 '25 17:12

Enigma


1 Answers

The code you have seems fine, but the underlying issue is different.

You have your cookie marked as

"httpOnly": true

This means that cookie cannot be accessed by client side code including Angular.js.

The only way to access it is to change the code that generates the code so the cookie is not marked as httpOnly. There are some security considerations for making the change, so make sure you understand what you are doing.

You can read more about HttpOnly at OWASP web site.

like image 147
dotnetom Avatar answered Dec 24 '25 02:12

dotnetom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!