tinesoft/ngx-cookieconsent

How to check consent status?

Lempkin opened this issue · 3 comments

Hi,

Once I've clicked on "Allow" I can see in my cookies that I got the cookie set to "allow".
But I'm not able to check cookie status in the app. I've tried with

this.ccService.getStatus()

but all it shows is
{deny: "deny", allow: "allow", dismiss: "dismiss"}

How can I check cookie consent status in the rest of my app? Do I have to check in the cookies by my own or can I use your library?
Thx

I've encountered on the same issue. How should this method be used?

/**
* Gets the current cookie status.
*/
getStatus(): NgcCookieConsentStatus {
return this.cookieconsent.status;
}

It always returns the same data as @Lempkin provided:

{deny: "deny", allow: "allow", dismiss: "dismiss"}

This comes from the fact that this.ccService.getStatus() calls method from cookieconsent library which always return this value. Check this here; open console and type cookieconsent.status and you will see that, whatever you change on this webapp, result will be the same.

Shouldn't .getStatus() method check value on popup instance like it's been implemented in .hasAnswered() and .hasConsented()?

hasAnswered(): boolean {
this.checkPopupInstantiated('hasAnswered');
return this.popupInstance.hasAnswered();
}
hasConsented(): boolean {
this.checkPopupInstantiated('hasConsented');
return this.popupInstance.hasConsented();
}
}

To answer @Lempkin , I get what I want when I call .hasConsented() because it properly reads from cookie.

@tinesoft Could you reopen this issue?

I have the same issue @mightymatth @tinesoft

Hi all,

Sorry for the very late answer.

hasConsented()can indeed be used to know if user as consented or not, based on cookies set.
But you can also listen to changes on the status by subscribing to statusChange$

import { Component, OnInit, OnDestroy } from '@angular/core';
import { NgcCookieConsentService } from 'ngx-cookieconsent';
import { Subscription }   from 'rxjs';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {

  //keep refs to subscriptions to be able to unsubscribe later
  private statusChangeSubscription!: Subscription;

  constructor(private ccService: NgcCookieConsentService){}

  ngOnInit() {

    this.statusChangeSubscription = this.ccService.statusChange$.subscribe(
      (event: NgcStatusChangeEvent) => {
        // you can use event.status or event.chosenBefore to know the new status

  }

  ngOnDestroy() {
    // unsubscribe to cookieconsent observables to prevent memory leaks
    this.statusChangeSubscription.unsubscribe();
  }
}

Hope it helps