So we worked a lot on the products and I hope it's now clearer how you can do these basic crud operations
from within a driver with your connected mongo database.
Now let me also talk a bit about authentication and users because you might note that there is a logout
button
and actually if we click it,
well then we can sign up or log in.
Now important, this authentication here is totally related to my app,
I want users to be able to sign into my store,
this is not related to the mongo database at all,
this is really important to understand.
People will not log into my database here,
they can't enter the credentials
we set up on the cluster.
So what we configured here for the users, we're not talking about these users at all,
our app will not create users here or use these users for signing in. These are the users, the clients
connecting to our database and in our case, our client that connects is our node backend, not the react
frontend and certainly not the users of our app.
I just want some users in my app and therefore, I will store the user data in my database
but they will not be able to access the database,
that is really important to understand.
Now with that, I can go into the auth.js file, the routes and there, I already have some code in place
for signing users up and for logging users in. Now for signing users up which is a sensible thing to start
with, I
of course also want to connect to my mongo database, then use a different collection, may be named users
and there, the user data should be stored, the email and the password.
Now the password will also be hashed first,
I already have some logic in place which takes the password and hashes it. This is the encryption at rest
or a part of it, the part where we do encrypt some data on our own
so that even if our database gets compromised, the passwords are not stored as plain text there.
So inside of here, I now have my stored, my hash password here
and I can store it to mongodb along with the email and that is exactly what we will do now
and of course as always, feel free to pause the video and try writing this code on your own because in the end
you'll just insert a document here, nothing more.
Now let's do it together.
First of all, I'll import my db helper by requiring it from the db.js file,
the same I did in products.js and then here where I have my hash password, I will call getDb then
call db,
so get access to the database itself and call collection to get access to a collection and I will get
access to the users collection which will be created dynamically if it doesn't exist yet.
Now there I simply want to insert one new element and that one new element here will simply be my user
data.
So there I will store my email,
so I'll name that key e-mail, you could name it however you want and I'll take the e-mail value which I
extract up there
and then, I will also add password where I take the hashed pw value,
so the hashed pw value is something I get here from that hashing package which does the hashing for
me, so I'll store that too.
So this will insert a document and I will then add a then block to do something once this insertion
completed.
Now in the error case, I'll copy the code from this catch block which is related to catching errors during
the password hashing process,
now this catch block here is related to catching errors related to inserting the document into the database
or here, I succeed,
so here I should successfully add my user to the database.
So let's maybe console log the result here and then here, I will use a token based authentication approach
which means I'll create a token which I return to my client side app, to the react app which then could
theoretically handle that token to authenticate itself to my backend for future requests and this is
a bit beyond the scope of this course,
if you want to learn more about this, check out my upcoming or already released
nodejs course where I will cover this
and also a free series on my YouTube channel or on my website, academind.com, we build a node restful
API there and there, I also have this authentication process in there,
you'll find a link to that in the last lecture of this module.
So here in the result, in the then block basically, I know that we inserted our document, our user,
so in that case I will send
this success response and I will create that token,
so let's copy that here.
Now create token as a side note is this function which just creates so called json web token
and again this is the part you can learn more about in my node course or in the free series,
so I just return that token here then and I can also return the email of which this was created on the
user object.
So now we have all the code in place we need to create a new user entry in the database hopefully,
so let's give this a try.
Let's save that file
and restart our backend server and then here on the frontend,
I switch to sign up and I'll enter some dummy email address and some password and actually, I can tell
you that I will enter tester here as a password
and then click sign up.
Now this seems to work and on the backend here, I see a log with some data about the operation,
one document was inserted, this is the ID
and this is the document which was inserted
and we can also confirm this in the shell by accessing users and finding all users
and there you also that encrypted password, that is not the password I inserted but the encrypted
form,
so that is that encryption in place
and that is our own encryption in place here I should say.
So this insertion works,
now we do have an issue
if I do enter that same data again. The same password is ok but the same user is not that great because
now if I check my users collection, you actually see the same e-mail in there twice and
that is not something you want typically.
So in the next lecture, let me show you how we can fix this and how we can ensure that email is unique
and maybe you also already know this solution.