Great.
And up next, I want to set up a function in the utils that automatically attaches the cookie to our
response.
Why?
Well, because we'll have to use it in three places.
We'll attach Cookie.
When we register login and also update user.
Now can you let's say copy and paste this logic?
Sure.
But since we're repeating, we might as well set up the function in the utils.
So first let's go to utils.
Let's create a new file.
In my case, that is going to be a Dutch cookie.
That's Cookie over here.
JS And as I say, not I didn't set up the proper extension here.
So.
James.
And then what's interesting, we need to pass in the response.
Now we don't have to return it from this utils function, but we do need to pass in response from the
controller.
So that way we have access to it and then we automatically add to that particular response and why we're
doing that?
Well, because we'll call this attach cookie.
In the controllers and notice we'll pass through things.
We'll pass in the response that we want to send back to that particular URL.
And also we pass in the token.
So those are the two things that will pass into function.
And then in the function, we're pretty much repeat the functionality we set up in the last video.
The difference is that now we need to pass both of these things as a parameter, and it's always my
preference if I have multiple.
When I basically set up the object so that way I don't need to worry about the order of those parameters.
So let's go to our controller.
And in order to speed this up, I'll just grab all of this code like so that in the attached cookies,
let's set up the function.
And we're looking for here.
And I'll write away export as default.
Since knowing me, I'll forget about that.
And then I'll run around like a headless chicken looking for the error.
Copy and paste.
So we create one day.
That's beautiful.
And now let's set up those parameters.
First, we want access to response and second to the token.
And yes, we don't need to return this restart cookie.
It's automatically added to our response.
And we have processed data entry that looks about right, so everything else can stay.
We just want to go to.
Fourth controller we want to import.
So in here, we're looking for that attached cookie.
Since I'm exporting as default.
I can go.
Just.
Patch cookies.
Remember you need to add this JS.
And then where we have a register, basically right after the token, we want to go with attach cookies
and we want to go with ROAS and we also want to pass in the token.
And we want to do that in login as well.
So let's find where is our login.
Over here again, invoke it.
And the same goes for update user.
Since we create that token.
Correct.
And let's invoke that.
And I mean, it doesn't really matter.
Let's test out the register.
Maybe.
So let me go here.
Let me log out.
And then instead of the log in one, let's go with the register.
So I'm going to create some dummy user here.
Let's say that it's going to be Susan.
Sure's and art.
Hopefully it's not taken already.
And let's see.
Submit.
Yep.
User created.
And then if we take a look.
So I have no jobs, Nothing like that.
But if I take a look at the application.
Yep.
The token is present.
And again, we can test out all of those routes in the postman.
But just to save some time, I'll just do it with register.
Again, the end result is following.
Where you know all of these roots.
Now we attach that cookie to the response.
And then from that point on, every time the browser is going to make a request back to the server,
it will automatically include that token cookie in the request, which is again, really, really,
really, really, really cool because you'll see that it just allows us to write less code on our frontend.