In this video (Part 36 of our Next.js + MySQL Job Portal App series), we’ll refactor our authentication system by creating separate reusable components for both the login form and registration form. Instead of writing everything inside one page, we’ll modularize our code for better structure and readability.
We’ll also integrate the currentUser function to fetch logged-in user data and apply role-based redirection — sending applicants to their dashboard and employers to theirs.
By the end of this video, you’ll have a cleaner authentication flow that’s easy to maintain and scale. ---------------------------------------------------------------------------------- 📂 Source Code 👉 GitHub Repo: https://github.com/thapatechnical/job-portal-nextjs
⌛TIMELINE⏳ 0:00 – Introduction to Job Portal App in Next.js 0:12 – Overview of today’s coding goals 0:30 – Creating registration form as a separate component 4:10 – Fetching currentUser data in registration page 5:20 – Building login form as a reusable component 7:00 – Code cleanup and removing redundant line 8:30 – Next video update and conclusion
Hello, what's up guys? Welcome to the next session of MySQL Presents Job Portal Application.
Today is day number 36 and we are going to see refactoring of our code.
Let me tell you what it means.
Ultimately, we have to create all this in this week.
You can see it in the settings like this.
I have come to the applicant's dashboard.
That's why he left. We saw it in yesterday's video.
So, what we have to do today is to refactor the code.
Let me tell you how to refactor it.
First of all, if you look closely,
the registration and login page in our code,
what have we done to it?
Like, we left the login and then the page.
This is not right.
It's the same with the register.
We left the register and then the whole code here.
So, at least today,
the login and then the register page,
we will keep it on a separate file
where it is easy for us.
So, if you come here,
let me show you here.
So, we have one folder
which is called Features.
In that, we have this Auth.
So, our login and registration comes under Auth.
So, I am thinking,
why don't we make our main form
of registration and login as a component
and keep it inside.
So, this will make my login and registration page
a little simpler.
Along with that, we have to check out
whether the user has already logged in.
That is, the getCurrentUser
which we were seeing since then,
like that function,
we have to implement it.
What if the user comes to the registration page
but the user has already logged in
because it is auto-login for 30 days.
So, we are going to check out
all those features in this video.
First of all, let's make it a little better
by combining it here and there.
In which we have this Auth.
I am creating a component in Auth.
Basically, the name of the folder is Components.
Components.
Now, first of all,
I am going to create my registration page.
Register
form
or registration form
TSX
Now, my registration page
Look carefully, guys.
I am going to copy this page.
Here,
I am going to paste
the registration form here.
I am going to remove it.
Now, look at one thing here.
This code here
and that is this registration form.
If you remove it for now,
there is no tension.
You can write registration form here.
Okay.
You can write registration form here.
Okay.
Now, at this point,
register user action.
No doubt, we need it now.
But our path has changed.
So, register user action
is done in this way.
My work is done.
Whatever was there, I have kept it as a component.
Now, I will go to the register page.
This is my registration page.
I am going to remove
all these from here.
I am going to remove it.
Okay.
This is my code.
I am going to remove it.
You can see how it is happening.
I will remove all this.
I don't need all this.
Something like this.
Now, I am going to remove this code.
Now, my registration page
is very simple.
Here,
my registration
registration form
I have got it.
If I do this much,
let's check how it looks.
Guys, now I am going to register.
As you can see,
this is the part.
You have to return JSX.
This is the rule.
Now, you can see it.
A while ago, my registration page
was like this.
Now, it is so sweet and simple.
Not only this,
I am going to do one more thing.
If you remember,
on the employer's dashboard,
it is not here.
I guess, it is on the layout.
Yes.
I am copying this getCurrentUser.
I am copying this code.
Our page,
not the employer's dashboard,
my registration page,
I am going to paste it here.
First of all, copy this
getCurrentUser.
Now, if it is await,
what should you do?
You should make an async.
You have to take the redirect
from the next navigation header.
You have to take it from there.
This is it.
It is very simple.
I told you,
I will make the registration,
but if the person has already done it,
if he comes to do the registration again,
why should we let him do it again?
He has already done it.
I will remove this line.
I will keep this rule.
If the user is present,
and if the user's role is applicant,
he will go to the dashboard.
In yesterday's video,
I told you by creating route groups.
If his role is employer,
he will go to the employer's dashboard.
Now, you can see
how sweet and simple
your registration page is.
There is no need to cancel it.
Remove it.
This is my work.
I have to do the same
with my login page.
I will copy this.
I want you to pause the video
and try it yourself.
Now, I will go to
components.
I will go to login
form.tsx
I will paste it here.
I have to do login form.
Let it be login form.
There is no tension here.
Let it be as it is.
Come to the login page.
I will do one thing.
I will zoom react arrow function
component with export.
I will zoom it.
I will do login
and then page.
There is nothing to do.
Remove it.
Here, you do
login
and this is your form.
I have done login form in this way.
There is no need for it.
I will copy this functionality
here.
I will paste it here.
Again, you will need getCurrentUser.
You will need getCurrentUser.
If you have created await,
you have to give async here.
If there is no user,
send it to login page.
This is your next forward slash navigation.
In this way.
If there is an applicant in the role,
if there is an employee in the dashboard,
I will go here.
Refresh it.
There is nothing to see here.
Let's do one thing.
If there is no user,
redirect to login.
We are already in login page.
Remove it.
I will go here.
Refresh it.
Let's do one thing.
I think we don't need registration page.
We are already in registration
and login page.
If there is a user,
and if there is an applicant,
he will go to dashboard.
If there is an employee,
he will go to dashboard.
Otherwise, directly register.
The user is not present.
Login page is the same.
If user is coming to login page,
and if his data is not present,
then login.
I guess, it is done.
Sign in here.
3 days back, there was a problem
that I was not able to sign in.
I was thinking why is this happening.
The rule is
if user does not exist
in login and registration page,
if user does not exist,
then don't go to login page.
Because I am already in login
or authentication page.
If I was in dashboard
or employee dashboard,
then I would have said,
user is not present.
Then you can access this page.
Why should I go to login page?
It doesn't make any sense.
This is it.
I hope you liked the video.
You can see my registration page.
You can see my login page.
We have made a component.
I think it is very nice.
That's it for today's video.
I know the video was short, sweet and simple.
But it was very important.
How you can improve your port
for readability and scalability.
That's it for today's video.
It is going to be fun from tomorrow.
Because finally,
we are going to work on our dashboard.
But before that,
if you have any doubt,
please check out my previous videos.
If you have any doubt,
please comment and let me know.
I am here to help you.
If you like the video,
please like it.
If you don't know how to support me,
then I don't need anything.
Just subscribe to my channel.
I upload awesome videos daily.
See you in next video.
Thank you so much.
Take care.