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

Closed
louismeeckers opened this issue Nov 24, 2024 · 3 comments
Closed

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

louismeeckers opened this issue Nov 24, 2024 · 3 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?

@apappas1129
Copy link

There really is no real solution for this library to work for Angular 18. The issue lies on the Angular 18's side (passing custom providers on the CommonEngine does not work anymore). As for this library, the best it can do is to provide a disclaimer in the documentation that it cannot be used in Angular 18.

@pavankjadda pavankjadda closed this as not planned Won't fix, can't repro, duplicate, stale Jan 17, 2025
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

4 participants