‫Now before we can actually
‫go build that sign-up feature,
‫we first need to fix a very important bug that we have left
‫in our authentication and authorization code.
‫And let me start by showing you that bug.
‫So if we just log out and then log in,
‫everything is gonna work just fine.
‫And notice how here we don't have anything in our cache now,
‫so this works just great.
‫But watch what happens when we're not logged in
‫and then we try to just access the page like this.
‫So in the root URL.
‫So what happens in this case is
‫that we get immediately redirected to the login page,
‫and we also then get this user in our cache,
‫which is just bull right now.
‫And the reason for this is that
‫if we try to access the root URL,
‫basically what we're accessing is this dashboard here.
‫But this dashboard is of course inside the protected route.
‫And so therefore if we are not authenticated,
‫then we are immediately redirected to the login.
‫Now that check,
‫whether we are authenticated or not,
‫happens here inside useUser,
‫which will call this function, getCurrentUser.
‫So this one right here,
‫which will return null if there is currently no session.
‫Or in other words if there is no logged in user.
‫And so this null here is that null that we get right here.
‫Now all of this is not the problem.
‫So this is not the bug itself,
‫but it's important to understand the bug
‫that we do have here in useLogin.
‫And so the bug that we have here is this line of code.
‫So here it should not be setQueriesData,
‫but actually setQueryData.
‫And also the user that we have here is wrong.
‫So let me actually log that user here
‫to the console so we can check it out later.
‫But the big problem that we have here is that since here,
‫we are not adding the user to the cache,
‫because here we have the wrong function,
‫then here inside of useUser,
‫this user that is then read from the cache
‫because it has the same key will just be null.
‫And so then here we have null.role==="authenticated"
‫which will always be false
‫because user will always be false.
‫And so on false,
‫we of course don't have the role property.
‫And therefore even after here,
‫we basically successfully log in,
‫this isAuthenticated state will still not change to true.
‫And so therefore we are then not redirected
‫to the content itself.
‫So basically to the dashboard.
‫All right.
‫Let me just log in here again just so we can see that user.
‫And so now it did actually work.
‫And the reason for that is
‫that I guess the five minutes have passed,
‫after which React Query automatically
‫deletes all entries here.
‫So probably we didn't have the user there anymore
‫after those five minutes, and so that's why this worked.
‫But anyway, here I just wanted to show you that the user
‫that we are adding to React cache is actually wrong.
‫Because we have this object which contains
‫both the session and the user.
‫So this is not what we want.
‫So let's just log out again. Let's come here again.
‫And so now we will be able to see again
‫that this doesn't work indeed.
‫And so after all this talk,
‫which I think is actually pretty important
‫for you to understand what is actually going on here,
‫let's now finally fix this bug.
‫So here in useLogin, here what we want is setQueryData.
‫So just like this.
‫And then here, what we want is user.user.
‫And so let's reload this.
‫Let's then recreate the situation, and well,
‫I guess we have to log out again.
‫And so here, again,
‫we have that initial situation where here,
‫the user is set to null,
‫which again is not the problem,
‫but the problem is the one that we just fixed.
‫And so now that's working great.
‫Now okay, so hopefully that made sense to you.
‫And as always, I made sure to include this bug here
‫in this video because bugs like this
‫really happen all the time in web development.
‫And I believe it's very important to show you this reality
‫and how we can go through our code in order to fix it.
‫But anyway, with that being said,
‫let's now finally implement our sign-up functionality.