8

I'm trying to implement SSR for my angular app. I followed the universal starter guide (https://angular.io/guide/universal).

My angular app consumes a rest API which need an access_token (client_credentials grant_type) for all requests. Im using cookies to store it on the client side.

I know that cookies can't work from scratch on the server side. So i tried some packages (ngx-cookie-service) but i can't find one for SSR and angular 7...

Lately i found this package (ngx-auth) but i'm not sure i can use it for that purpose...

I can't find any documentation on the universal website, if someone have successfully implemented cookies for universal using angular 7...

A big thanks !

lovis91
  • 1,988
  • 2
  • 14
  • 24

2 Answers2

7

Ok i finally succeed using @gorniv/ngx-universal, following https://github.com/Angular-RU/angular-universal-starter

If needed i can provide an implementation example with angular 9.

lovis91
  • 1,988
  • 2
  • 14
  • 24
  • 1
    use https://github.com/Gorniv/ngx-universal/blob/master/lib/src/cookie/README.md - fork from ngx-cookie with fix for SSR – gorniv Jul 31 '19 at 17:18
  • instead of the fork you can use fix from issue: https://github.com/salemdar/ngx-cookie/issues/104#issuecomment-568108352 – Dukeatcoding Feb 04 '20 at 11:27
0
here's my solution : 

  getssrCockie(){
    if (this.req !== null) {
      const cookies = new Cookies(this.req.headers.cookie);
      const clubInfo = cookies.get('clubInfo')
      return clubInfo
    } else {
      const cookies = new Cookies();
      const clubInfo2 = cookies.get('clubInfo')
      return clubInfo2
    } 
Farouk Mhamdi
  • 311
  • 2
  • 8