Skip to content

Possibility to create cookie on server side #266

@Ben555555

Description

@Ben555555

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);
    }
  }

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions