tinesoft / ngx-cookieconsent

Cookie :cookie: Consent module for Angular.
https://tinesoft.github.io/ngx-cookieconsent/
MIT License
164 stars 35 forks source link

How to set consent? How to prevent popup opening after setting cookie? #140

Closed kahanu closed 9 months ago

kahanu commented 9 months ago

The documentation lacks information on this which I feel is the whole point to this package.

In my Angular 17 application, I'd like to know how, and in what event to set the cookie, and how to prevent the popup from opening after the "Allow Cookies" item has been selected?

I'm using the Cookie Service npm package to set the cookie.

import { CookieService } from 'ngx-cookie-service';
    this.statusChangeSubscription = this.ccService.statusChange$.subscribe(
      (event: NgcStatusChangeEvent) => {
        // This is where you should send the current cookie status to a
        // web service call to update the users preference.
        // Also, if the user declined accepting cookies, then the app
        // should not set any cookies for the user.
        console.log(`statusChange, ${JSON.stringify(event)}`);

        this.cookieService.set(this.cookieKey, 'allow');
      });

When the "Allow Cookies" button is clicked, the cookie is successfully created for the user and the "allow" status is shown.

The popupOpen$ event always executes before the Initalizing or Initialized events. Can someone provide code that shows how to set the cookie and how to prevent the popup from appearing for those users who have consented?

Thank you.

kahanu commented 9 months ago

Ok, I found the issue. I had my domain setting as 'localhost:4200' when it should have been just 'localhost'.