salemdar / angular2-cookie

Implementation of Angular 1.x $cookies service to Angular 2
MIT License
109 stars 29 forks source link

get('key') returns "undefined" value but cookie exist #33

Closed toni-moreno closed 7 years ago

toni-moreno commented 7 years ago

I'm trying to recover a cookie value with this code.

import { CookieService } from 'angular2-cookie/core';

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private router: Router,private cookie: CookieService) {}

  canActivate() {
    console.log('check canActivate:');
    var token: string;
    console.log(this.cookie.getAll())
    token=localStorage.getItem('id_token');
    if (token) {
        console.log('check for cookie: snmpcollector-sess-'+token)
        var cval;
        cval = this.cookie.get('snmpcollector-sess-'+token)
        console.log('TONI cookie'+cval)
        if (cval) {
          return true
        }
        console.log('Token exist but  token ['+ token +'] dont match cookie session'+ cval)
        return false;
    }

As you can see in the following image the cookie "snmpcollector-sess-lan exist but this.cookie.get('snmpcollector-sess-lan') returns undefined

Could be this a bug or perhaps I'm doing something wrong?

image

Toub commented 7 years ago

Check your cookie flags, especially "http-only": https://www.owasp.org/index.php/HttpOnly

toni-moreno commented 7 years ago

Ok.. you are rigth. HttpOnly is set.. Thx !!

minkedong commented 7 years ago

@Toub ,thanks a lot!