Relative Content

Tag Archive for angulartypescriptcookiesangular-router-guardsdata-persistence

Persisting user and user profile in Angular and data changes after consulting it

Yesterday I encountered a very peculiar bug which I have not been able to solve. I essentially created services to handle user authentication and data management. I also created an additional service to handle cookies for data persistence. The issue begins when a Rout Guard executes the cookie service.
This is mainly because the cookie service uses document.cookies as this is what chatgpt suggested. I really didn’t consider this to be appropriate, knowing this was a potential issue. I also didn’t like the ngx-cookie-service approach to this solution. That’s why I opted to do it myself. When I execute the code, everything works as expected. I’m able to log in, logout and move around the page. The guard does its job as expected. However, I get an error saying the document isn’t available in the Guard’s context. Here is the error code