Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possibility to create cookie on server side #266

Open
Ben555555 opened this issue Jun 5, 2023 · 10 comments
Open

Possibility to create cookie on server side #266

Ben555555 opened this issue Jun 5, 2023 · 10 comments
Assignees
Labels
enhancement good for contribution good for contribution from community
Milestone

Comments

@Ben555555
Copy link

Ben555555 commented Jun 5, 2023

Is your feature request related to a problem? Please describe

Is it possible to create a cookie on the server side?
When I try this, the cookie does not seem to be attached in the response that is coming from the server:

constructor(protected readonly cookieService: SsrCookieService,
    @Inject(PLATFORM_ID) platformId: Object) {

    if (isPlatformServer(platformId)) {
      console.log('server set');
      this.cookieService.set('test', 'test');
    }
}

Describe the solution you'd like

cookieService.set should also send a "Set-Cookie" header in the response.

Describe alternatives you've considered

TransferState

Additional context

A possible implementation could look like this:

 set(
    name: string,
    value: string,
    expiresOrOptions?: number | Date | any,
    path?: string,
    domain?: string,
    secure?: boolean,
    sameSite?: 'Lax' | 'None' | 'Strict'
  ): void {
    //if (!this.documentIsAccessible) {
    //  return;
    //}
...
    let expires: Date | null = null;

    if (options.expires) {
      if (typeof options.expires === 'number') {
        expires = new Date(new Date().getTime() + options.expires * 1000 * 60 * 60 * 24);
      } else {
        expires = options.expires;
      }
    }

    if (expires) {
      cookieString += 'expires=' + expires.toUTCString() + ';';
    }
...

    if (this.documentIsAccessible) {
      this.document.cookie = cookieString;
    }
    else {
      this.response.cookie(name, value, {
        expires: expires,
        domain: domain,
        path: path,
        secure: secure,
        sameSite: sameSite
      } as CookieOptions);
    }
  }
@github-actions
Copy link

github-actions bot commented Jun 5, 2023

Hello 👋 @Ben555555
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

@pavankjadda
Copy link
Collaborator

@Ben555555 I don't think this is possible. The cookies are set with syntax

Set-Cookie: <cookie-name>=<cookie-value>

This should be part of the response from server, from API request

@Ben555555
Copy link
Author

Ben555555 commented Jun 22, 2023

@pavankjadda It is as you can see in my example:

  constructor(
...
    @Optional() @Inject(RESPONSE) private response: Response
  ) {
..
  }

      this.response.cookie(name, value, {
        expires: expires,
        domain: domain,
        path: path,
        secure: secure,
        sameSite: sameSite
      } as CookieOptions);

It is also implemented like this in the following library, which unfortunately is not maintained anymore: https://github.com/salemdar/ngx-cookie/blob/master/projects/ngx-cookie-backend/src/lib/cookie-backend-writer.service.ts

Without this feature it's not possible to modify or add a cookie on server side which is a very useful feature.

@pavankjadda
Copy link
Collaborator

@pavankjadda It is as you can see in my example:

  constructor(
...
    @Optional() @Inject(RESPONSE) private response: Response
  ) {
..
  }

      this.response.cookie(name, value, {
        expires: expires,
        domain: domain,
        path: path,
        secure: secure,
        sameSite: sameSite
      } as CookieOptions);

It is also implemented like this in the following library, which unfortunately is not maintained anymore: https://github.com/salemdar/ngx-cookie/blob/master/projects/ngx-cookie-backend/src/lib/cookie-backend-writer.service.ts

Without this feature it's not possible to modify or add a cookie on server side which is a very useful feature.

Interesting, let me take a look. Haven't spent much on this so far

@pavankjadda pavankjadda added enhancement good for contribution good for contribution from community and removed need more information labels Aug 22, 2023
@pavankjadda pavankjadda modified the milestone: 16.0.0 Aug 24, 2023
@pavankjadda pavankjadda modified the milestones: 17.0.0, 18.0.0 Feb 8, 2024
@blakeoxx
Copy link

@pavankjadda is this issue still looking for contributions? I'm available to craft a PR if so.

@pavankjadda
Copy link
Collaborator

@pavankjadda is this issue still looking for contributions? I'm available to craft a PR if so.

Sure. Go ahed

@GkIgor
Copy link

GkIgor commented Feb 18, 2024

The Angular team closed my issue saying that cookies do not exist on the server side and the problem is in the way this library is operating. Could you help with my problem?
Issue: angular/angular#54459

@pavankjadda
Copy link
Collaborator

The Angular team closed my issue saying that cookies do not exist on the server side and the problem is in the way this library is operating. Could you help with my problem? Issue: angular/angular#54459

I am on vacation can't check it until next week.

@blakeoxx
Copy link

@GkIgor the Angular team is correct. You need to use the ngx-cookie-service-ssr package instead of the ngx-cookie-service package your reproduction repo currently uses. The ngx-cookie-service package is only intended for use on the client browser and will only attempt to read cookies from the browser's document cookies.

If switching out the packages still doesn't get you any closer, I'd recommend opening a separate issue for Pavan to look at when he returns. This doesn't seem to be related to the issue stated here of not being able to write cookies during SSR.

@gabynevada
Copy link

Just in case, the problem may be related to the new ssr in angular 17.

The current dev server does not use the server.ts file so any providers or changes made to that file won't work on the dev server.

angular/angular-cli#26323

blakeoxx added a commit to blakeoxx/ngx-cookie-service that referenced this issue Feb 19, 2024
blakeoxx added a commit to blakeoxx/ngx-cookie-service that referenced this issue Feb 19, 2024
blakeoxx added a commit to blakeoxx/ngx-cookie-service that referenced this issue May 15, 2024
blakeoxx added a commit to blakeoxx/ngx-cookie-service that referenced this issue May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement good for contribution good for contribution from community
Projects
None yet
Development

No branches or pull requests

5 participants