Want to store user registration data in your database using Express.js? 🚀 In this video, we’ll cover the step-by-step process of handling registration in a backend application:
✅ Creating the registration route in Express ✅ Checking form elements in register.ejs ✅ Writing the registration controller function ✅ Validating if the email already exists before inserting data ✅ Storing user registration data securely in the database
By the end of this video, you’ll have a fully functional user registration system in Express.js! Let’s get started! 🔥
⌛TIMELINE⏳ 0:00 🎬 Introduction – How to Store Registration Data in DB 0:40 💾 Get the Source Code (GitHub Link) 1:05 🛠️ Creating the Registration Route 1:40 📝 Checking the register.ejs Form Elements 2:40 🔧 Building the Registration POST Controller Function 3:15 📩 Getting User Data in Express.js 5:00 🔗 Completing the Remaining Registration Code 7:00 📂 Creating a New Auth Services File 7:35 🔍 Checking if Email Already Exists in Database 11:40 💾 Inserting User Registration Data into Database 15:10 🎯 Task for You – Implement This on Your Own!
Hello what's up guys today in this video
we will see how to add registration data in
our database yes guys so today
in this video we will see that whenever a user
registers, how can we store all his data
in our drizzle ray database
I am going to tell in this video today that's
why I said drizzle ray database that
in reality it is not a database it is just a ray it is
working as a middle man in actual
it is getting stored in our my sequel server
but whatever it is today
in this video you will get to learn that how
any user is filling data here you will
get data on your database it
is a very important video
you are going to learn a lot today in this video
and as far as guys are concerned source code is concerned you
can see it here you will find it
come to the comment here if you see then I have added the user
table scheme added which was in yesterday's video
so you can copy paste from here
or you
can check out directly in this way also
please do check out so guys now We will
start the video without any delay
but before that guys if you have read
our technical
youtube0 till now but there are some rules which
we will follow step by step. So let's
do one thing, all this useless stuff that is
visible on the top, shoot them all
first of all right first of all we need to define the
route. Like now there is registration,
as you can see we created the router in this
way, so in the same way
I copy
paste it, I register,
get registration page and here
instead of get I write post register. Okay so once I have done that, hit
this also
comment in this way. So the first
thing was to create a route for the post
register request. Now here you do one thing,
come to your register file.
Okay, here see if everything is equal
or not. Okay, submit. Okay, now this one
D, I do not need it right
now. Don't worry about the remember me one. We
will see further in the code. I mean in the feature
how you can tackle that also. Password.
Guys, make it a password,
okay, the name field is very important, I am
telling you first, so we have an
email, fine, we have a user and that is
but in action, guys, forward,
you have to hit the register route,
okay, so now we have this route
in which we have a post method, so this is the same in the
router, whenever someone hits this route, basically the
post method will be called, so
for that we are going to write our code here,
so that is our controller, okay, so let's
go here,
so here you just have to
define it
in this way, okay, post, this
was its name, or it is a post request,
only a request, we have a response, you
make it a function with a sync operation, okay,
guys, now see, first of all, if any user is
filling data, if
you want to get his data, then in express it's
super easy and that is very simple, you do one thing,
tomorrow enter
a request body, okay, and whatever it is,
for now, race
dot Let us redirect it
to login, okay, for now,
whatever it is, it's okay, I am
doing it in control and save in this way, so guys, let's go to the code,
sign up, for time pass,
I can write anything and click on register,
see, I have come to login, that means
my request, post request,
okay, has been hit, only then I have
come to login, but not that, I want to
show you this and that is, one second it will
be here, I guess or so this is the link, this is also
not there, so guys, let's do one thing, I will just
come here one more time, for you guys,
I will refresh it here,
okay let's see, okay, look, I am coming to the login page, that
means it is working and boom
guys, this is what I wanted to show you,
see, okay, see this, this is
Vinod Bahadur Thapa, this is my email, this is the
password and this sign up, which we are basically
clicking on submit, so the data has
come to me, now I have to store this data here,
where, at this place But
at this place, okay, there is a table of users
there, so how can we do that,
look carefully, on submitting you
got the data, you got the data, it means that
you should store it somewhere,
then the constructor, I work, I am
directly destructuring, I know
it's a name, email and we have a password, okay,
from where are we getting it, I do
n't have any tension with the request body, I do this,
comment, okay, now first of all, whenever someone
registers for the first time,
what should be our first step, it is to check whether the
email ID with which he
is registering, does that email ID already exist, do you
understand what I am saying,
if that email ID already exists
on your database, it means that someone else has
already registered with that email ID,
then we should not let him do that, so
here now what do we have to do, we need to
check whether that email ID
exists or not, so guys cost use it,
we check it and we need to go
with a wait whenever you You use select
and basically it returns a promise so
what did I name the function get user
by email okay here pass your email
this is it alright so now
look here if that user
exists then what we need to do we need to
return redirect to the
same page again and that is your
register okay and in case if he
doesn't exist then what we need to do is
we need to add that data to our database
so now how will we add it to the database
for that again we will need to query the database so
for that also I will create a function
so guys I am writing user will be equal to
await
create user for a second okay I am writing create user
now here I will have to add that data
and I am passing it inside the object
what will happen is that whatever name you are
writing inside it first of all make
sure that your database
field should be there okay so it's a
name then we have an email then we have A
password is okay so if all this is done
then it means my work is done so guys if
this much is done then I will tell you the next
step okay but now
first of all we have to do is that the user exists
no I will
have to code it right so for that we will go here
inside services okay
where will we go inside services because the
convention is this and the result is giving us a service only
that brother you
don't write the code of my sequel you write our code and
finish your work quickly the rest the
code of corresponding my sequel we will
generate something like this so beyond this I will
go you write th dot services
dot just okay here I have come what is this
get user by email in it we are passing an email
export const it is like this okay
and one second we are getting the email there
okay I got the email now
remember this much now what should we do here we are going to
write the query of the database so first of all
guys you will have to import the database so
from where will you do it
dot forward dot dot forward sl we have a
conf this is very important and
obviously you also need a table so we have
I guess is it
user and what was that in the schema this okay it's
your users table yes this is very
important guys remember this method
from dot forward s no dot dot
forward s
l f sma jis okay so this is the database
and this is the table in which we have to add our data
so let me go here so
guys here what do you have to do simple ah
look I am writing this square in brackets
because whenever you
use select method like in my sequel and
in like drizzle then it will return you in guys array I mean
you get the data in array
okay so if we do it in this way then
we will get our first element in ray and we also know that
we are going to get only one data okay
so how will we do it look ah wait one second
wait remember then you will have to create this a sign
okay so look basically we are
doing wait Look how DB works,
if you want to check whether email exists
or not then it is so easy, select
star from table name where email is equal
to email, this is the code, if you remember
like in my sequel we do something like this,
here also it is the same,
select from table name where, okay
now guys here instead of using normal raw object,
we will use this operator which is equal,
okay, this operator we will use
conditional operator you can call it or filter
operator, whatever we call it and now
what will I do here, my
email in user table if it is if it is equal to,
now that I have passed the email,
our work is done, right? And yes one thing
you do not have to use the braces, by the way,
okay, one second, what is it doing below, it
should come here, you understood it, look
what should we do inside the database, select
from user's table, check in it that
this email in the table and the
email written by the user, is it the same or not,
it's my The work is done, now I will
return my user here. Okay, so
my data must have come in the user, let's do one
thing,
what do I do with this for you guys,
for now I am deleting it for a while.
Okay, I will keep it here, it
is necessary to import the control. After doing this,
I will tell you what are
we getting, where is this issue coming here,
auth services, yes, how many times have I
told you to do dot js, otherwise in this way,
whatever warning is there, it will be useless.
Okay, so I go here again, I
write this
[music],
I am pasting anything here. Okay,
now I am hitting enter, so look,
I have come here, if I go here, so
guys, you can see that we are getting undefined
and it's fine, because there is no
data in our database, right? You are not
understanding anything, alright, so now
we have to run this code, that if
everything is set, then this is the name, the email and then
I am sharing the password, so guys,
I go here. Okay,
I will copy and paste this at this place. Okay,
so
create user, okay, this is what create user is.
Okay, name, email and then
I have pasted the password. I am coming here.
Okay, guys, what do I
do here? I will directly return.
Return wait. See how easy it is to
store data in the database. As a result,
inside the database, look simple. This was my sequel
code. Insert into your table name.
And now the name of the table, you will have to paste it here.
User table and values. Okay, and
here in the values, guys, we know
that name is equal to name, email is equal to email.
So I can simply paste it. Okay,
or I can do it. So actually
what was happening was that the name of our database
and its name is something like this. And both the key
and value are the same. So I
can directly write it in this way. Okay, and here there is
another code which I will paste. So
when this is basically added, it will
return its ID to us. There is
nothing simple. It was very easy,
I come here and this is my user
here and one more thing is that
guys, here also the user,
you will have to write it within the score brackets,
basically what are we doing, guys we are
destructing the array, nothing, first
we are getting the data, okay and let me do one more thing,
I will show this to you guys as well, I will show the console
dialog and then the user also,
now our work is done, if everything is set
then it will be redirected to login,
okay, it
was not a big deal, once I
refresh it here, is there anything
here, no, there is nothing here,
okay, this is test
@ com, let me write this and hit enter, so
guys you can see that create user is not
defined, okay, this is what we are talking about, let
me
go back and refresh, so
here again test
@ com, okay,
okay, change password and boom, guys
see, I have come to the login page, okay
now I have to see here, and look user.
I got one in the assert undefined and id,
which means that this particular data,
basically I had written that return id
here, its output has come here
and here obviously undefined will
come because that assert itself, I mean that email did
not match, now if I do one thing,
but before that I would
like to show it to you once and then guys
our data has been added, okay now this
password, now the doubt that you have there is that
brother what is that password @ tha.dj
because we are not throwing any error,
but if I go here in user assert, I
got this data, I mean
if the user asserts then I am getting that data, it
means brother return it, you do
not need to store it in the database,
go out and register again
with a new different email
id, so guys this is it
for today, I hope you will like the video very
much, there are still many things left in it,
hashing the password, doing this, doing that,
we will go step by step. Okay, but today
I told you about registration, so
guys now it's a task for you, so now guys here is
a task for you that
you have to implement this login code, the
user will write his email id, the user will
write his password, so you have to check
whether that email id exists or not,
whether that password matches or not, if
both match then he will be redirected to his home page,
you have to do this
in tomorrow's video, I will do it and tell
you, but you have to do this and you must try it
and comment and paste that code,
I will see below, okay and if you have
any doubt, then comment for that also,
I am here, I will
provide you the source code, but first you
watch the video, understand it, then only go and try to do it,
one thing I would like to say is that it is
important for you to understand why we are doing this here, you can tell me by commenting
why I have written it in this way, this
is okay, so guys this is it, this is all for this video today, I
hope you liked the video very
much, finally Gradually
you are seeing it becoming interesting, you must be
feeling dynamic, so
like the video and share it with your friends, our
next target is 8 lakh subscribers, if you have
not subscribed yet, please do, I will
meet you in tomorrow's video till then
guy, thank you so much, take care
[Music]