Kramti
Kramti

Reputation: 122

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'])
    }
  })
}

}

Upvotes: 8

Views: 18851

Answers (3)

user18699279
user18699279

Reputation:

Use

this.cookieService.deleteAll('/', domainName); 

instead of

this.cookieService.deleteAll('../')

or

this.cookieService.deleteAll()

Upvotes: 0

Vinay Soni
Vinay Soni

Reputation: 98

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

Upvotes: 7

Kwoque
Kwoque

Reputation: 291

I was having the same problem in Angular8 (also in Angular 6 and 7 before). I found the answer in the documentation: https://www.npmjs.com/package/ngx-cookie-service

Apparently the cookieservice saves cookies by path. So as stated in the documentation, if you are not sure under which path you should delete: add '/'

I used the following method

logout(): void {
        this.cookieService.delete('TESTCOOKIE', '/');
    }

this works for me now.

Upvotes: 17

Related Questions