Giter Site home page Giter Site logo

Comments (11)

github-actions avatar github-actions commented on June 15, 2024 2

Hello 👋 @Bazhan4ik
Thank you for raising an issue. We will investigate into the issue and get back to you as soon as possible. Please make sure you have given us as much context as possible.
Feel free to raise a PR if you can fix the issue

from ngx-cookie-service.

boubou158 avatar boubou158 commented on June 15, 2024 1

@Bazhan4ik There is a possibility it only does not work for localhost. I am currently doing a test with another domain from localhost. What i am trying to do is to create a cookie from a subdomain to its parent domain: example test.mydomain.com will create a cookie on mydomain.com to be readable by all others sub domains.

I will keep you posted once deployed :)

from ngx-cookie-service.

mkumar92 avatar mkumar92 commented on June 15, 2024

@Bazhan4ik

I have also faced this issue, this solution work for me

this.cookieService.set('cookieName', data, {
expires: new Date(),
path: '/',
sameSite: 'Strict'
});

from ngx-cookie-service.

boubou158 avatar boubou158 commented on June 15, 2024

Same here, i am also looking for an answer, @mkumar92 your solution does not work for me unfortunately.

from ngx-cookie-service.

Bazhan4ik avatar Bazhan4ik commented on June 15, 2024

Sorry @mkumar92, your solution doesn't work for me either

from ngx-cookie-service.

mkumar92 avatar mkumar92 commented on June 15, 2024

@boubou158 yes, this is working only on live domain.

from ngx-cookie-service.

Bazhan4ik avatar Bazhan4ik commented on June 15, 2024

I think this is because browsers don't treat localhost as a real domain. This question's answer could be a workaround for localhost: https://stackoverflow.com/questions/38669040/share-cookies-to-subdomain-on-localhost

from ngx-cookie-service.

boubou158 avatar boubou158 commented on June 15, 2024

Thanks a lot for the hint. In fact, localhost do not work the same way as per any deployed application on real domains. It is working fine once deployed. I think the solution you shared for localhost could work, i did not try personnally but it is worth a try if you really need it.
I think it would be a good idea to add this in the documentation of the library to prevent other users to struggle as well.

from ngx-cookie-service.

Bazhan4ik avatar Bazhan4ik commented on June 15, 2024

I couldn't find any solution about setting cookies to subdomains of localhost, but I found a workaround for development and testing cookies.

You can set .domain.com (which is all subdomains of domain.com domains) only if your domain has at least one dot in it.
You can't set .localhost because localhost doesn't have a dot it in.

Workaround for development

What you can do is redirect a random domain to your localhost
For example redirect mydomain.com to localhost and then you can set cookies to all subdomains of mydomain.com like this .mydomain.com

To redirect mydomain.com to localhost you will have to open c:\windows\system32\drivers\etc\hosts file and add some lines after the comments:

127.0.0.1    yourdomain.com
127.0.0.1    subdomain.yourdomain.com

There is 127.0.0.1 because localhost is redirected to 127.0.0.1 too, so instead of redirecting yourdomain.com to localhost
and then to 127.0.0.1 you can just redirect yourdomain to 127.0.0.1.

Now you can use this method to save cookie to all subdomain of yourdomain.com

this.cookieService.set("name", value, 1, "/", ".yourdomain.com");

Don't forget the port!
subdomain.yourdomain.com:3000 if your domain is yourdomain.com

from ngx-cookie-service.

pavankjadda avatar pavankjadda commented on June 15, 2024

@Bazhan4ik I have tried this in https://setcookie.net/ and it works fine as expected. We need more details

Please everyone note that you can set a cookie from a subdomain on a domain (sent in the response for requesting subdomain.example.com)

Set-Cookie: name=value; Domain=example.com // GOOD

But you can't set a cookie from a domain on a subdomain(sent in the response for requesting example.com)

Set-Cookie: name=value; Domain=subdomain.example.com // Browser rejects cookie

And see https://stackoverflow.com/a/57803192/9244861 for more details on this

from ngx-cookie-service.

Bazhan4ik avatar Bazhan4ik commented on June 15, 2024

Yes, it works on https://setcookie.net/ because setcookie.net has a dot in it. To set a cookie for all subdomains of a domain the domain has to have a dot in it. For example, if your domain is setcookie.net you can set a cookie .setcookie.net, but you can't set .localhost for all subdomains of localhost because localhost doesn't have a dot in it.

It will work when you host your website with a real domain, but on development (on localhost) you can only set cookie for localhost, meaning that on development (on localhost) if your website has subdomains, the subdomains will not see the cookie because you can't set .localhost cookie.

To test your cookies on development you have to use fake domain, a domain that will redirect to 'localhost'. Above there is a workaround.

Thanks @pavankjadda for replying!

from ngx-cookie-service.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.