question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

shared cookie between domain and its subdomain is not available

See original GitHub issue

Describe the bug

I use Angular 12 library and “ngx-cookie-service”: “13.1.2”. I tried to add a cookie (contains user data) for a domain (localhost) and its subdomains (sub1.localhost, sub2.localhost).

Angular app uses AuthService and runs on localhost. It sets a cookie: setUserData(userData: string) { this.cookieService.set(this.COOKIE_NAME, JSON.stringify(userData) , 36000000, '/', this.DOMAIN ); }

but the app running on sub1.localhost is not able to retrieve the cookie data (which were set for localhost): getUserData(): string { let userData = this.cookieService.get(this.COOKIE_NAME); return userData; }

import { Injectable } from '@angular/core';
import { CookieService } from 'ngx-cookie-service';

@Injectable({ providedIn: 'root' })
export class AuthService {

  private DOMAIN: string = 'localhost';
  private COOKIE_NAME: string = 'NGX-COOKIE-NAME';

  constructor(private cookieService: CookieService) { }

  getUserData(): string {
    let userData = this.cookieService.get(this.COOKIE_NAME);
    return userData;
  }

  setUserData(userData: string) {
    this.cookieService.set(this.COOKIE_NAME, JSON.stringify(userData)
      , 36000000, '/', this.DOMAIN
    );

  }

  clearUserData() {
    let c = this.cookieService.get(this.COOKIE_NAME);
    if (c != null)
        this.cookieService.delete(this.COOKIE_NAME
            , '/', this.DOMAIN
        );
  }

}

I run the app using a command (it is added to package.json scripts as “s” - you can run it by “npm run s”): "ng serve --host localhost --disable-host-check --port 4200 --open"

Also, is it possible to set a cookie from sub1.localhost on localhost domain?

Steps to Reproduce

  1. Download the code from Stackblitz: https://stackblitz.com/edit/angular-viq7wd?file=src%2Fapp%2Fauth.service.ts
  2. Run the code using “npm run s” command (the command is added to package.json file). After that the app should be running on localhost:4200.
  3. Provide any value using input element labeled by “cookie value” and press “Save cookie button”. After that a new cookie should be set for localhost domain.
  4. In address bar change url from ‘localhost:4200’ to 'sub1.localhost:4200. After that the app should restart.
  5. Click “Read cookie” button to retrieve cookie data set in step 3. Issue: the cookie data are not able to retrieve; localhost domain cookie is not visible for sub1.localhost domain.

Please provide a link to a minimal reproduction of the bug. StackBlitz, CodePen or CodeSanBox

https://stackblitz.com/edit/angular-viq7wd?file=src%2Fapp%2Fauth.service.ts

Expected behaviour

I’d like to be able to manage one shared cookie between domian and its subdomains.

What version of the library you see this issue?

13.1.12

What version of Angular are you using?

Angular 12

Screenshots

No response

Desktop? Please complete the following information

Windows 10, Google Chrome 96.0.4664.45, 64-bit

Mobile? Please complete the following information

No response

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
pavankjaddacommented, Jan 23, 2022

@pavankjadda I did what you wrote. I removed this.DOMAIN from AuthService. But it did not solve this bug. Do you have modified-working example i.e. which may be based on my provided stackbliz example?

Code uploaded here. Follow instructions from README.md

0reactions
pavankjaddacommented, Jan 22, 2022

@pavankjadda I did what you wrote. I removed this.DOMAIN from AuthService. But it did not solve this bug.

Do you have modified-working example i.e. which may be based on my provided stackbliz example?

I might be. I uploaded to GitHub.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Share cookies between subdomain and domain
I have two questions. I understand that if I specify the domain as . example.com (with the leading dot) in the cookie that...
Read more >
Can't share cookie between domain and subdomain - MSDN
1. Look, to share a cookie between multiple asp.net applications, as in different ones. · 2. If it is the same application (one...
Read more >
What is the most secure way to store cross subdomain cookies
If you want to share cookies across subdomains, but leave out other subdomains, you should explicitly state which subdomains you want to read ......
Read more >
Working with Subdomains locally, and sharing cookies across ...
To set a cookie that will be shared across the setting domain and all sibling and child domains, set the cookie's domain to...
Read more >
Setting cookies to subdomains in JavaScript
There's an easier solution though: just set a cookie to the domain and check if the browser actually set that cookie. If it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found