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
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.tsGuard
Specifications