In this video, we’ll continue building our Next.js + MySQL Job Portal Application, and learn how to store a session token securely in the browser cookie after user login.
We’ll also understand the meaning of Secure and HttpOnly flags, how to check cookies in your browser, and why cookies are important for maintaining user sessions without re-login. ---------------------------------------------------------------------------------- 📂 Source Code 👉 GitHub Repo: https://github.com/thapatechnical/job-portal-nextjs
⌛TIMELINE⏳ 0:00 – What is a Cookie 1:50 – How to See Cookies in Browser 2:20 – Storing Token in Browser Cookie (Next.js Example) 4:05 – What is Secure Flag in Cookies 4:40 – What is HttpOnly Flag 5:50 – Checking Session Token After Login (String or Not) 7:15 – Task for You: Implement Session Management in Registration
Hello, what's up guys? Welcome to the next session of mysql present job portal application.
Today is our day 24 and today we are going to see cookie in our next portal application.
I mean job portal application.
So what is cookie?
See guys, it's very simple.
What is cookie? More important than that is why we are studying about cookie in this video.
So in yesterday's video, I told you that whenever you log in, we are creating a session.
And whatever I have stored in the database.
To create a session, we need an IP address, an ID, a user agent.
And user's ID so that we know who this guy is.
And most importantly, we created an expiry date.
Here you can see when it will expire.
Now see for yourself.
This is November 11, 26.
On November 26, whoever it is at exactly 7 o'clock, it is going to expire at that time.
Okay, so much is happening.
This is a sub-aside and now our data is in the database.
Tomorrow, let's say a user comes back after two days.
And opens my application.
Then we will not log in again.
We will redirect them to the page they want to see.
How is this possible?
Because now what we have to do is,
this token which is stored in our database.
I have to store this token in my browser's cookie as well.
So that if that user comes back to log in to that page.
Or basically visits that page.
Then we will add that cookie to our request header and pass it to the server.
Then whoever is there will see it.
Then how to see it, that's another story.
But at least I have to pass the cookie to that request header.
So that we can compare whether it is the same user or not.
Whether this token matches with the token in your database or not.
If it does and the date is not yet expired.
Then log in.
Why do you have to work so hard to log in again.
That's why we need cookies here.
And by the way, if you click on the i icon.
Then you can see cookies and site data here.
And when you right click, inspect, go to application.
Here also you can see local storage.
Then you can see that we get cookies here.
You can find it from many different places.
But today's video is going to be very easy.
Because we have seen a lot in yesterday's video.
So there is nothing to do.
Again, it's a login part.
You come here.
See, this is the actual function.
Where you generate token, generate IP address.
Generate user agent.
Then you call a function.
By which our data will be stored in the database.
Once this is done.
Then we don't just store it in the database.
We also need it in cookies.
So if you want to store it in cookies.
By default, Next.js gives us a function called cookies.
You can see.
Next forward slash headers.
I mean, it's so easy in Next.js.
By default, you are getting cookies.
So whatever cookies you get.
Create cookies.
Or cookie store.
Whatever you want.
Store it there like this.
Now you don't have to do anything.
This is your cookie store constant variable.
Okay.
Now.
In this way, you got an instance of cookies.
In which you can use all the properties and methods of the cookie function.
So here is a method.
Whose name is set method.
Okay.
If I hover.
Then you can see.
This is a method.
If I do this.
Then you can see.
Type.
One second.
Remove this.
So here you can see.
There is a method.
Key.
String.
Then value.
And then some partial options.
Means.
Now you have to pass a string.
Okay.
Whatever your name of the cookie.
Then what value you want to store.
You have to pass that.
And then you can pass some options.
Like.
I want.
I am keeping the name of this session.
What have I kept?
Yes.
I have kept session.
What value do you want to pass?
I don't want to do anything.
I want to send tokens.
When you are storing on the cookie.
Your plain.
Your cookie.
Without hash.
You can easily pass that.
So I passed that.
Along with that.
You have to pass some options.
Yes.
Obviously.
So we have one.
A property called secure.
So.
Guys.
What you have to do.
You have to make it true.
So here.
To write secure as true.
Means.
Whenever.
Your connection will be secure.
That is like.
HTTPS.
Means.
Whenever.
It will be a secure connection.
Then.
We.
Basically.
Any user.
Is coming back to your page.
So.
In the request header.
This cookie.
It will be included then.
Otherwise.
It will not.
Means.
Your connection.
It is necessary to be secure.
Second.
HTTP only.
I am making it true.
Now.
This HTTP only.
This property.
To make it true.
You know.
First.
With the help of javascript.
We can easily disable.
Our javascript code.
Right.
Like.
In console.
Document.
Cookie store.
Document.
Cookie.
Something like that.
Through which.
If you don't make it true.
By default.
You can get the cookie.
But.
We are disabling all of them.
Means.
No.
This is a good thing.
Preventing from.
Unwanted users.
Hackers.
Take this.
And then.
Guys.
Finally.
Its.
Session.
Basically.
Max is a property.
We are saying.
For 30 days.
Means.
As I was doing here for 30 days.
Our session.
Will remain on our database for 30 days.
Similarly.
Our cookie will also be for 30 days.
The day it was created.
That day.
I just.
By not storing on the database.
Along with.
Our cookie.
By creating it.
Also.
For 30 days.
I am giving expiry date
That's it
By doing this
Automatically
Your cookie will be created
You don't have to worry
When user login
I mean not login
It will be checked later
We will see authentication later
But at least
While login
You can store the data
7.04
Inspect
We have this application
Cookies
Alright, we have a session
By default over here
But if I create one more time
My one more session is done
My cookie is done
Oh yes
7.15
2025
11.26
Let me refresh
Did you see
My one more session is done
See
My one more session is done
So this is the beauty
My one more session is done
And now
7.15
I am saying 15 minutes
Till then I don't have any tension
You can see
7.15
So yeah guys
This video is too short
I know but this is the most important
Don't just store in database
We have to store session data in cookie
So that later we can compare
Without login
Or without registration
We can implement this
Alright
We will see that later
But now
It's a task for you
It's done on login
But when I go to register
I have to create session
I have to store this data in cookie
So whatever we have done
With login form
Same you have to do
With registration form
And this is your task
If you think this is too childish
Let me see how much you do
Do comment
Because this is too important
So take this as a task
And do it
And paste your code in comment
I will see
Rest
If you have any doubt
Do comment
If you like the video
If you don't like
Do dislike
Your opinion is very helpful for my channel
If you haven't subscribed
Please do it
Till then guys
Thank you so much
Take care