ngx-cookie-service: Angular v17 SSR - InjectionToken for REQUEST/RESPONSE not working?
See comment for resolution
Either I’m doing something wrong or cookies can’t be retrieved from request headers via the SsrCookieService. It seems that SsrCookieService is not able to retrieve the request from the providers.
I have the server.ts and my guard like described below but the bearer token can’t be retrieved. I checked the incoming request in the server and the cookie is available in the header. So I assume the request is for some reason not correctly provided to the SsrCookieService.
Is that a bug or am I doing something wrong?
server.ts
server.get('*', (req, res, next) => {
const { protocol, originalUrl, baseUrl, headers } = req;
commonEngine
.render({
bootstrap,
documentFilePath: indexHtml,
url: `${protocol}://${headers.host}${originalUrl}`,
publicPath: browserDistFolder,
providers: [
{ provide: APP_BASE_HREF, useValue: baseUrl },
{ provide: 'REQUEST', useValue: req },
{ provide: 'RESPONSE', useValue: res },
],
})
.then((html) => res.send(html))
.catch((err) => next(err));
});
Guard
export function authenticationGuard(): CanActivateFn {
return () => {
const authService: AuthService = inject(AuthService);
const router: Router = inject(Router);
const cookieService: SsrCookieService = inject(SsrCookieService);
return authService.getUser().pipe(
map((x) => {
const bearerToken = cookieService.get('access_token');
console.log('Bearer Token: ' + bearerToken);
if (x.role !== 'admin') {
router.navigate(['/home']);
return false;
}
return true;
})
);
};
}
Specifications
- Version: Angular 17.0.0 and ngx-cookie-service-ssr 17.0.0
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 2
- Comments: 25
Hello 👋 @Shamshiel
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
Guys issue is related to https://github.com/angular/angular-cli/issues/26323, while running ng serve it wont work unless in angular v19. You need to
npm run build:ssrand thennpm run serve:ssr. Also make sure you provide request{ provide: REQUEST, useValue: req },withimport { REQUEST } from 'ngx-cookie-service-ssr';Might be related https://github.com/angular/angular-cli/issues/26323
Hi! Can you help me please?