tinesoft / ngx-cookieconsent

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

Always shows in angular universal #88

Open blasco opened 4 years ago

blasco commented 4 years ago

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

It always shows in angular universal. In the browser version works correctly, but not sure if it needs to be added in a different way when using angular universal. My guess is that the server is rendering it and showing it, even when the user has already accepted it. Any possible solution?

tinesoft commented 4 years ago

Hi @blasco

the underlying cookieconsent library relies on client side cookies to determine whether or not user has given his consent. This information is stored as a cookie. I'm not sure you can set/store cookies in a server side rendering (which Angular Universal is)...

Maybe using this cookie-parser middleware? Never used it through, let's me know if it works for you.

blasco commented 4 years ago

yes, I would like to have the option of not running the cookie consent automatically (so I can run it only when I check that the code is running on the browser). The problem is that including the module results in the window being shown. Is there await to prevent the automatic init?

blasco commented 4 years ago

Any way to achieve this? The only thing I need is to init the module only if I'm in the client and not the server, which is easy to detect. The only problem I have is that the init is called when the module is included... is there a way to prevent this?

blasco commented 4 years ago

It's very weird though, I don't see any cookie being place. Maybe the error has to do with that I'm using Angular 10?

nilot111 commented 4 years ago

Hi blasco, one question , did you do something else apart from documentation ??, i could not get it to work in angular 10 universal, so i would really appreciate if you can help me or share me a little of the process of adding this cookieconsent. I am getting following error, while in single page app works great : ERROR TypeError: Cannot read property 'initialise' of undefined at ngx_cookieconsent_NgcCookieConsentService.init

blasco commented 4 years ago

@nilot111 I call the initialization only at the browser. Add a check that you are not on the server side to avoid these errors.

nilot111 commented 4 years ago

@blasco thanks, but how can you add that checking if it initialize by itself with just adding in constructor . ??

Following documentation i need to add it to app.component.ts just like this : constructor(private ccService: NgcCookieConsentService){}

As my understanding this by itself call initialize, so how can i prevent it only executes in browser since it is in the constructor. thanks in advance.

blasco commented 4 years ago

@nilot111 I'm not using this library anymore, I don't remember how I made it work, but I think you are right and the init is called in the constructor

tinesoft commented 4 years ago

Hi all,

Sorry for the late response...

The library is indeed automatically initialized in the constructor. This was intended for convenient reasons (to avoid the implicit call to ccService.init()) , but now I realize this may not be ideal for every use case, in particular when the initialization need to be conditionned.

I will update to change this behavior or at least configure it in the upcoming release of the library.

Stay tuned

nilot111 commented 4 years ago

@tinesoft I am glad to hear that you will update. Thank you so much for giving time to this. Regards.

l4nos commented 3 years ago

Did you manage to change this to not automatically call the init method?

It would be helpful if you can as I have a production site that needs this to work on using Angular Universal.

I'm happy to take a stab at changing if you want,

As an alternative solution for anyone else waiting for this, you can use Angular's Injector to conditionally inject into the plugin into the main compponent depending on the presence of a cookie.

The below link explains conditional injection.

https://stackoverflow.com/questions/43450259/how-to-conditionally-inject-service-into-component

arindamINT commented 2 years ago

@tinesoft Why it is always opening the popup? How to set a cookie after clicking the accept button? How to check whether the cookie is already saved or not?

nascarjake commented 2 years ago

I am also having a similar issue to the one arindamINT explains above - the popup always shows up every time the page loads and clicking "Got It" while using the "info" type does not set any cookie.

BenceUszkai commented 2 years ago

Did you tried it on localhost? you cannot save cookies for localhost. you have to add localhost to host file.

arindamINT commented 2 years ago

Did you tried it on localhost? you cannot save cookies for localhost. you have to add localhost to host file.

I tried with both, localhost as well as a staging server.

arindamINT commented 2 years ago

@tinesoft Why it is always opening the popup? How to set a cookie after clicking the accept button? How to check whether the cookie is already saved or not?