Ashin
Ashin

Reputation: 13

Angular SSR website redirects to logged out page before going to actual path

I am using Angular Universal to enable the server-side rendering in my project. The issue I am facing is that whenever I hit refresh from a certain logged in page, it immediately shows the logged out version of the homepage for a flash and then goes back to the original path. This issue only happens in the SSR build.

Upvotes: 1

Views: 917

Answers (1)

David
David

Reputation: 34445

You cannot use local storage to save authentication data when using angular universal.

When you refresh the page, the request is sent to angular universal server, which will not have access to the local storage. Your guard will consider the user as not connected, so rendered content will be the one of a disconnected user.

Then, when the page is rendered client side, the client app does have access to the token and will generate again the view, but this time for a connected user.

The solution is to use cookies, which can be accessed server side

Upvotes: 1

Related Questions