Search code examples
cookiesangular7

Cookies not deleting all the time angular 7


I use ngx-cookie-service to store my token but when i click Disconnect does not delete the cookies everytime. Sometimes its working, but sometimes it not.

sometime i need just to reload the page to make sure cookies are deleted,sometime it works fine but it not redirect me to login page . i tested it in localhost and in build same thing . for the browser i use chrome

To set my token i use this :

 setAuth(value, expireTime): void {
this.cookieService.set('id_token', value, expireTime, '../');

}

I am using following code to delete a cookie:

clearCookies(){this.cookieService.deleteAll('../');}

and this my logout function:

  logOut() {
let path = location.pathname;
if (path.indexOf('/panier') > -1 || path.indexOf('/store') > -1) {
  this.setLogout({ value: true });
} else {
  this.disconnect().subscribe(res => {
    if (res.status == 'success') {
      this.setLogout({ value: false })
      this.clearCookies();
      this.router.navigate(['/login'])
    }
  })
}

}


Solution

  • can you try with Following

     logout() {
                this.cookieService.deleteAll('/', 'xyz.net');       
            }
    

    here '/' is Path and xyz.net domain name , which you have specified while setting the Cookie this code is working for me . it will remove all cookie with same domain i guess