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

v18 SSR, providers not passing cookies on the server #332

Open
louismeeckers opened this issue Nov 24, 2024 · 2 comments
Open

v18 SSR, providers not passing cookies on the server #332

louismeeckers opened this issue Nov 24, 2024 · 2 comments

Comments

@louismeeckers
Copy link

Describe the bug

I have my access_token stored as a cookie in my angular v18 SSR app.
ngx-cookie-service-ssr logs correctly the cookies on the browser but an empty object on the server.

Is that incorrect way to process in v18 ?
Thank you in advance for your help !

Steps to Reproduce

I have installed ngx-cookie-service-ssr and when I console log in the app component the cookies, I get the cookies in the browser :
console.log(this.cookieService.getAll());

{
    "access_token": "eyJhbGc..."
}

However, the same corresponding log on the server is empty:

{}

As describe in the README, I need to set the REQUEST and RESPONSE in the providers of server.ts :

server.get('*', (req, res) => {
  res.render(indexHtml, {
    req,
    providers: [
      { provide: APP_BASE_HREF, useValue: req.baseUrl },
      { provide: 'REQUEST', useValue: req },
      { provide: 'RESPONSE', useValue: res },
    ],
  });
});

This solution still print an empty object on the server.

For this reason, I made some research and added the following line at the top of my server.ts:

export const REQUEST = new InjectionToken<Request>('REQUEST');
export const RESPONSE = new InjectionToken<Response>('RESPONSE');

and set the providers like below so that ngx-cookie-service-ssr can catch them :

providers: [
          { provide: APP_BASE_HREF, useValue: baseUrl },
          { provide: REQUEST, useValue: req },
          { provide: RESPONSE, useValue: res },
],

(fyi: console log req.cookies in server.get section print them correctly, so no issue on that side)

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

--

Expected behaviour

Printing the cookies with ngx-cookie-service on the server

What version of the library you see this issue?

18.0.0

What version of Angular are you using?

Angular 18

Screenshots

No response

Desktop? Please complete the following information

No response

Mobile? Please complete the following information

No response

Anything else?

No response

Copy link

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

@blakeoxx
Copy link

blakeoxx commented Jan 2, 2025

This seems like it may be resolved by the solution to #266 . Does updating to the v18 backport provided in this comment help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants