In this video, we’ll learn how to validate username and email during registration in our Job Portal App built with Next.js, MySQL, and Drizzle ORM. We’ll check if the email or username already exists in the database and prevent duplicate registration. This is an essential step for data validation and user authentication systems in any real-world project.
👉 What You’ll Learn:
How to check if an email already exists in the database How to verify username duplication Using Drizzle ORM with OR operator to check multiple conditions Displaying error messages for existing users Writing clean and secure server-side validation logic ---------------------------------------------------------------------------------- 📂 Source Code 👉 GitHub Repo: https://github.com/thapatechnical/job-portal-nextjs
⌛TIMELINE⏳ 0:00 USERNAME AND EMAIL WHILE REGISTRATION MUST BE UNIQUE 0:50 WHAT WE WANT - PREVENT DUPLICATE DATA 1:40 Try Yourself - Verify the Process 2:30 How to Check if Email Already Exists 4:40 Add Username Existence Check 5:45 Using OR Operator of Drizzle to Check Multiple Conditions 7:10 Checking if Email Matches 7:50 Same Check for Username 8:20 Test the Registration Functionality 10:00 Clearing Doubts 11:05 Next Video Update
Hello, what's up guys? Welcome to mySQL Present Job Portal Application.
Today is our Day 16 and today we are going to see how we can validate our username and email.
Validation doesn't mean that the user's name should be at least 8 characters or 3 characters or something like that.
Email has to be like this and that. Not that.
The most important thing for us is that when we are registering, does the user already exist with the email ID from which he is registering?
That is, the email ID from which he has already registered, can we allow him to register again?
No, we won't. Why?
Similarly, if there is a full name, then we have a username also.
If there is a full name, then okay.
Then why is there a username?
If we are giving two names to a user, then no doubt one name can be common.
For example, my name is Vinod.
I don't know how many people in the world have the name Vinod, right?
But if I tell my unique name there, it can be Vinod55 or Thapa Technical, Vinod Thapa or Vinod Thapa Technical.
So it will be my unique name.
Whether you see my Instagram or YouTube, everyone has a unique name.
So this is a unique name.
So this is no doubt not the same for any user.
Everyone should have a unique name.
Now, we have already validated that it should be unique in this scheme.
So in this video, we have to see how to check whether the email address and username already exist or not.
And if it exists, then we will throw the error.
Else, we don't have to do anything.
We don't have to register.
We will just give the error.
Either put a new email ID or username.
Simple.
So guys, first of all, we have already seen how to insert.
Even how to get the password value and how to modify it.
Modify means hash.
I have already shown you that.
So please pause the video.
Try it yourself.
Now, how do I fetch the username and email from the database?
And I compare it.
Whether the user typed is the same or not.
If it is the same, then it already exists.
So we will not let them register.
We will throw them in the notification.
That email and username already exist.
Please write something new.
I mean write.
But before that, guys, if you haven't checked out the rest of the videos yet, then please do it.
Alright guys, so let's start the video without any delay.
But before that, if you haven't subscribed yet, then please do it.
Because it takes a lot of hard work.
And my channel is nothing without your support.
So please subscribe.
So see.
I go to my VS Code Editor.
You can see here.
We have already seen so much.
And the beauty is.
I have already shown you how we can show notifications here.
So if I show you this.
This is the data.
What is the user doing?
He is writing.
And I have got it.
Right?
If I show you here.
If I take the register.
This is the register part.
If you see here.
So this is the data.
This is the registration data.
Where I know what the user has written.
Right?
And this is my server action.
Where I can call the database very easily.
And I can fetch all the username and email.
So first of all.
This is our job before hashing the password.
It doesn't make sense to check after hashing the password.
Right?
First check.
If everything is fine.
Then go and hash the password.
Otherwise, why did we suffer so much?
Right?
So I go here.
Simple.
Let's say user.
Because it will be an individual user.
Right?
So what I do.
I want to get the data from the database.
Email and then username.
Right?
I go here.
Await.
And we are using a drizzle.
So drizzle has its own syntax.
db.select.
I won't do anything here.
From.
From where?
Obviously from our users.
Right?
And I have already got the users.
By the way.
You can see.
We have already got the users.
And now in that.
What?
Select.
Star.
Basically we used to write like this.
From.
User.
Where.
Now this where.
This is a condition.
Where it matters the most.
That.
We have a user.
Right?
I could have written this.
User.
Not username.
It's like user.
Not user.
Sorry.
Users.
Whatever the email will be.
If it is triple equal to.
This is the email above.
This email is being talked about.
This is the email.
The user has already written.
We have already got it.
Right?
This one.
This one is the email.
Which I am getting from the database.
Right?
Which I am getting from the database.
So now.
What are we doing here?
Is our email.
And then the email written by the user.
Both are not the same.
If it is the same.
Then it's a mess.
Right?
And.
I don't just have to check this.
I also have to compare the username.
So for that.
What will I do?
In drizzle.
We have syntax.
You.
Keep it in parentheses.
Here we have.
Equal operator.
Okay.
Basically.
Equal.
Method.
You see.
And I got it.
Above.
If you see.
So see.
We have this equal.
Okay.
We get it from drizzle.
So this.
I have done one thing.
And by the way.
Here.
You don't have to write triple equal to.
When you are using equal method.
You have to give a comma.
So in this way.
What did I do?
That.
I checked this.
Whatever email the user has written.
In my database.
The table of users.
It doesn't already exist in it.
Right?
I mean.
Both are the same.
I mean.
It exists.
But.
I don't have to compare so much.
Along with that.
I will copy the same one.
Okay.
I will give a comma here.
And paste.
Now.
Instead of this email.
I will go with user's name.
Right?
Username.
By the way.
Is it?
Yes.
Okay.
And there too.
It will be username.
Right?
Username.
In this way.
Now.
When you write two equal operators.
You can't write in this way.
So what will we do now?
In these two equal operators.
Whether it is username.
Whether it is your email id.
If any of the two matches.
We can't register.
And now.
What are we doing in this?
What is the condition?
Tell me.
In JavaScript.
Which operator is there?
Which makes sure that.
If you pass two conditions.
If any one of them is true.
Then.
Means.
It is true.
You get to know in the result.
Obviously.
Your OR operator.
And here.
By default.
You get only OR.
And you have to import this too.
This is it.
So.
Either email.
Or username.
If any one of them.
Is satisfied.
Means.
User.
Either.
User's name.
Or email id.
Already exists.
And I.
In user.
Now.
What am I doing?
I am destructuring it.
Like this.
Either say Destructor.
Or you hold this.
The first element.
I have already told you this.
In the result case.
Please watch that video.
Basically.
You get data.
In an array.
Okay.
If I tell you.
Then.
You get that data.
Like this.
Where you have.
This is your.
First element.
Here.
You have actual data.
After that.
Others.
Utility things be.
So.
Again.
Like this.
I am just passing the value of the first element in the array
You can understand this
You can watch the video
That's all
In case if my user exists
If everything is correct
If user is true
Now I have to apply the condition
That is user.email
If the email is matched
Equal to this email
I don't have any idea
Why the email was matched
Or the user was matched
That's why whatever I have
I am comparing that user's email
With this email
If there is something like this
Then in return object
Your message and status will come
Something like this
Status error
Error email already exists
I am going to this
Else
If there was no email
Then what happened
Else
Error username already exists
If you do this
Then both email and username
I am refreshing
You can see
Full name and email id
Email id already exists
Also
User name already exists
Let's see
Password leak
Password is also wrong
Let's see
If I hit enter
First
Password are not matching
First
Password is not matching
I told you
Until password is not matched
We will not check
If you see here
This is an if condition
Here is an if condition
First I am checking the password
We have not touched the database
Password are not matching
Let me show you
I hit enter
Email already exists
If there is background noise
I am sorry
and if everything is fine
and if i click on create account
and boom guys see
Registration Completed
and you can see the difference in color
you can see red on error, green on success
and if i refresh
you can see the data
Thapa Technical FFF
you can see the data
so yeah guys this is it
that's all for today's video
but i know you have a doubt
when i
in the return statement
i have passed these two messages
object basically
i have not done anything here
so how can i see that in the notification
guys if you have forgotten
then see this
when we were watching the notification in yesterday's video
we have already handled it
like result.status was success
so show the success message
toast.success or else toast.error
either success or error
if there is no success
so this is how your data works
i know the video was short
but it was very important
and if you think the video is too short
then did you do it by yourself
if you can do it by yourself
then i am very happy
but if you can't do it by yourself
and if you think the video is too short
then this is your problem
because if you can't do it by yourself
then how will you do big things
so whenever i say anything
i tell you in the starting
what we are going to do today
so pause the video and try it by yourself
that's it for today's video
tomorrow we will see
how we can complete the login page
we are going to create the login application
tomorrow
we will do everything
in just one video
that's it for today's video
if you want to join react classes
and learn from me
you can read it
if you like the video
then please like it
and if you have any doubt
then tell me in the comments
i will help you
till then thank you so much
take care