angular / universal-starter

Angular Universal starter kit by @AngularClass
2.02k stars 688 forks source link

How-to: Authentification in Angular Universal #523

Open stephanegg opened 6 years ago

stephanegg commented 6 years ago

Angular Universal allows us to store auth token on the client side (in the localstorage wrapped in isPlatformBrowser condition) but how to retrieve it on the server side? What are the best practices? I guess that using Http Interceptor is key but what is left is still unclear for me.

m98 commented 6 years ago

Is not it possible to store a cookie for your access token, and accessing it from the server, and browser?

You just need to check the platform, and use the correct way to access cookies based on platform type

samber commented 6 years ago

LocalStorage is not available server side. You have to store your auth token into cookies.

stephanegg commented 6 years ago

Sure, do you have any example on how to send the auth cookie on browser side and get it back server side? It would be really helpful (for me and a lot of people I think as it s not really well documented)

samber commented 6 years ago

@stephanegg https://github.com/angular/universal-starter/issues/373#issuecomment-366254611

canbakis commented 6 years ago

I am also struggling with this issue and I have problems with these parts:

I tried to use @Inject(REQUEST) but gave me a an error. I would also need a way to delete the cookie if the authentication expires.

Gorniv commented 5 years ago

ngx-cookie good work with SSR look https://github.com/Angular-RU/angular-universal-starter/blob/master/src/app/home/home.component.ts#L21

cj-wang commented 5 years ago

@canbakis hope this helps https://github.com/angular/universal-starter/issues/373#issuecomment-527021529