Persisting User Login
In this lesson, we're going to persist the login information
Get the project source code below, and follow along with the lesson material.
Download Project Source CodeTo set up the project on your local machine, please follow the directions provided in the README.md
file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.
This lesson preview is part of the The newline Guide to Fullstack ASP.NET Core and React course and can be unlocked immediately with a single-time purchase. Already have access to this course? Log in here.
Get unlimited access to The newline Guide to Fullstack ASP.NET Core and React with a single-time purchase.

[00:00 - 03:18] Although we see the user logged in right now, but if we refresh the page We see login again This is because Redux stores in memory which removes it after a refresh Now what we can do is inside user slice we can create a new reducer function. So let's go to the code and let's go back to user slice and We can create a new reducer function and for now, let's call it get user This will have just the state This function will check if the token is still there inside local storage So let's make a new variable user and let's check if there is User inside local storage. So what we can check is local storage Get item and check the user Now if there is a user we want to store it inside Redux. So now what we'll do is if user in this case we want to put state dot user to be this User but we won't be able to do it because this user is now stringified. So we have to pass it so let's write Jason dot pass and We can now export this function. So here Let's write Get user and now we can use this reducer function inside app dot tss file so let's open app dot tss and Here let's write dispatch and we can use get user and Before we go back to the browser what we can do is we can push the user to the profile page when they log in or They register. So let's go back to the sign-in component and Inside this function first of all we need history so I can create history is equal to use history and Here we can write history dot push and we will push the user to the profile page And we can apply the same thing for registration. So let's go back to the register component and Let's copy this part and paste it here Put use history and here Same thing history dot push to the profile page Now let's go back to the browser to see if this is working as expected. So let me open the browser and Now let's try to log in again As you can see it takes us to the profile page and if we refresh we still see the menu rather than login Which means this function is working fine