Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Universal set set-cookies in request header

I have anngular Universal SSR application with authentication. I save auth token inside http cookies(set-cookies). Now when I refresh application I see unauthenticated page at first, and after a second it converts to authenticated state.

I understand that this happens because Universals renderer server doesn't pass token from cookies to API when rendering page. So how can I force Universal server to render page in authenticated state?

like image 934
O. Shekriladze Avatar asked Apr 08 '26 04:04

O. Shekriladze


1 Answers

Angular universal does not transfer cookies when using HttpClient (github issue). You can add them manually, using an interceptor.

interceptor.ts

import { REQUEST } from '@nguniversal/express-engine/tokens';

@Injectable()
export class CookieInterceptor implements HttpInterceptor {
  constructor(@Optional() @Inject(REQUEST) private httpRequest) 
  {
  }

  intercept(req: HttpRequest<any>, next: HttpHandler) {
    if (this.httpRequest) //If optional request is provided, we are server side
    {
      req = req.clone(
      { 
        setHeaders: 
        {
            Cookie: this.httpRequest.headers.cookie
        }
      });
    }
    return next.handle(req);
  }
}

You might get the following error

Refused to set unsafe header 'Cookie'

A possible workaround, suggested in this comment, is to bypass xhr2's default security behaviour for unsafe headers

For angular 7 (and I guess 8), the following works

server.ts

import * as xhr2 from 'xhr2';
xhr2.prototype._restrictedHeaders = {};

I haven't tested for angular 9, but I think you need a different workaround for the unsafe cookie header

// activate cookie for server-side rendering
export class ServerXhr implements XhrFactory {
  build(): XMLHttpRequest {
    xhr2.prototype._restrictedHeaders.cookie = false;
    return new xhr2.XMLHttpRequest();
  }
}

@NgModule({
  ...
  providers: [{ provide: XhrFactory, useClass: ServerXhr }],
  ...
})
export class AppServerModule {}
like image 97
David Avatar answered Apr 09 '26 22:04

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!