abacritt / angularx-social-login

Social login and authentication module for Angular 17
630 stars 388 forks source link

State persistent after page refersh #766

Open Nikhil-Wagh opened 2 months ago

Nikhil-Wagh commented 2 months ago

The user state is lost when I refresh the page. The user is logged out and is again required to complete the login flow.

How to avoid this?

zooli123 commented 1 month ago

I have the same issue. I could workaround this by saving the id and the token to a cookie each in the subscription to the authState. Once I have them in cookies, I just add the jwt as Bearer to every message I have to my server and it is authenticated. And also connected everything related to my state with these cookies instead of a variable in the memory.

this way your FE session stays, after refreshing the page the variable gets nulled out, but you have everything you need amongst your cookies (or localStorage, depending on where do you store the user data)

I have another problem with this solution though I couldn't find a solution for: I cannot call refreshAuthToken() after a page refresh, as socialLoginService doesn't use my cookies, but the cleared variable, and it causes a nullptr exception in the social-login library code.... also when I don't do any refresh but call refreshAuthToken() it sets the same jwt as before, so I cannot extend the expiration date.