So we learned a lot about how we work with sessions and cookies and how we can use it to store data
and what potential issues are if we store mongooses model data in there because we don't store to full
mongoose model but only the data but not the magic Moffats.
So we have to recreate that mongooses more than we have to really fetched data.
Now one thing I just noticed is in the news if I go to my product detail there does add to cart of course
all this should only be rendered if I am authenticated so I'll grab that link from product list and
replace this year to make sure this works correctly because previously if I clicked on details even
though I'm not locked in I did see my button there.
Now if I log in and I go to the perhaps details I see it here I don't see it.
If I'm locked out.
Now one thing you might notice is if you do logon like this you sometimes might end up in a scenario
where often logging in the view didn't update accordingly.
Now I fail to reproducers at the moment but you might see this that you log in and still some items
are missing and you need to reload the page to get there.
The reason for this is that in off Chia's when I have post log in here I do set my session and when
I then redirect when I send the response the session middleware goes ahead and creates that session.
And that means that right said to Mongo DVH because we used to Mangu to be sessions store and it sets
a cookie.
Now the problem we can face here is writing data data to a database like what would it be.
Can take a couple of milliseconds or depending on your speed even a bit more.
Molly seconds.
The redirect is fired in the pen and from that though so you might redirect too early.
Now to be sure that your session has been set.
You can use request session here and call the safe method.
You normally don't need to do that but you need to do it in scenarios where you need to be sure that
your session was created before you continue because you're you can pass in a function that will be
called.
Once you're done saving the session you'll get an error here.
If an error exists most of the time that should not be the case and that in here you can safely redirect
and you can be sure that your session has been created here.
So now if that if I walk out and I log in again here now it will only continue once that session has
really been created.
Normally you don't need to call that but you need to call it if you need that guarantee which typically
is the case when you do redirect for example because in such scenarios the redirect will be fired independent
from the session being saved.
And therefore the redirect might be finished and the new page might be rendered before your session
was updated on the server and in the database.
That's something to keep in mind and that is why I wanted to show you this method to.