Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right, so let's now create
those new users, based on the model that
we just implemented in the last video.
So, we will do most of the user-related stuff
like creating new users, logging users in,
or updating passwords in the authentication controller.
So all of the stuff that's related to authentication really,
is not gonna be in the user controller
that we actually created before,
but instead we will create an authentication controller.
So all the functions that are related
to authentication will go here.
And this will make a bit more sense later on,
when we then start using all the functions
that I'm gonna put in here.
And now, the first thing that we need to do,
is to import our user,
so our user model of course,
and that is at one level up, then it should be in models,
and then userModel.
All right.
So, let's create and export our very first controller.
And I'm gonna call this one signup.
And so you see that I'm not calling it create user,
like I did in a tour controller,
but really I'm calling it signup because
that's the name that has a bit more meaning
in the context of authentication.
So it's gonna be an async function of course,
because we're gonna do some database operations.
Then request, respond, and next as always,
and then in here is where we create the new user.
And so that's very similar to what we did before.
So this time it's called newUser,
and then remember how we create
a new document based on a model.
So we use the model name of course,
which in this case is User, and then
on that we use the create method.
And then all we need to do is to
pass in an object with the data
from which the user should be created.
And so just like before, that data is actually in req.body.
And so all of this is gonna return a Promise,
and so we need to await that,
and so that's the reason why this is an async function.
And I see that up here, we need exports.
And now in the next step, we will then
actually send that newUser to the client.
So res.status, which is 201 for created,
and then our JSON object.
So that is success again,
and then our data property which is again
called an envelope here,
so a property that is gonna contain the data itself.
And the data is now called user,
when before it was called tour,
and so what we're gonna send in here is the newUser.
Give it a save, and before we try this out,
remember that this is an async function,
and so we need to think about error handling here.
So what do we need to do here?
Well, we need to wrap this entire function
that we just created into the catchAsync function
that we created in the last section.
So remember how we did that in the tourController,
where all of the functions that are asynchronous
are wrapped into this catchAsync function
so that we don't have to write
the try catch block in each and every function here.
Remember that from the last section?
So actually I'm gonna go ahead
and copy this require statement here.
So a bit lazy sometimes,
not feel like writing the same code over and over again,
so catchAsync, and then we need to
close that down right here in the end.
And now, all we need to do is to
actually implement the route so that
this signup handler here then can get called.
And so let's go to our userRoutes,
which we actually already created sometime before,
and we already have some routes in here.
But actually I'm gonna create a new one here now.
And as I mentioned right in the beginning of this video,
the user resource is a bit different
from all the other resources.
Again, because it really has to do with
all things authentication.
And so we have a different controller for that
so the authController, the function names
also have some different names,
and so we will actually also have a special route.
Let's say router.post,
and then we will create a route for signup, so /signup,
and then this is where we will use
that function that we just created before.
And of course before we can do that
we need to require that authentication controller.
So let's just duplicate this line here
and replace user in two places,
and so here we have our authController,
and so let's now use that.
.signup.
So as you see, the signup is really
kind of a special endpoint.
It doesn't fit the REST architecture
that we talked about before,
because in this case it doesn't make much sense.
And so remember how we said that
in some special cases, we of course can create
other endpoints that do not 100% fit that REST philosophy
that is basically implemented here.
So this here of course follows 100% the REST philosophy,
where the name of the URL has nothing to do
with the action that is actually performed.
While up here, of course, it has.
So the name of the route is signup
because we are signing up users.
And also down here we have implemented
all of these different HTTP warps
but in here for signup, we only really need POST.
So we cannot really get data from signup,
or we cannot patch patch a signup, so not update it.
Doesn't really make sense,
and so in this case all we want to do
is to have a route for signup,
where we can only POST data.
Because again, it makes only sense
to actually send data to this route,
so that the new user is then created.
And we will have different routes similar to this one,
like for login, or for reset password,
and all kinds of stuff like that.
And we will also keep these routes here
so the ones in a more REST format,
because there's also the possibility
of a system administrator updating or deleting
or getting all the users based on their ID.
But we will take care of that later,
for now we just want to implement
all the functions that are about authentication.
So basically functions that are
only relevant for the user itself.
So it's not an admin that is gonna sign up a user,
or it's not an admin that's gonna login a user,
but instead, it's the own user that's gonna
sign up himself, or log in himself.
Okay, does that make sense?
So we should have everything in place now,
so that it should now work when we create our new user.
So when we sign up basically, for our application.
So let's try that now.
Let's just copy this URL,
users, and then, signup.
Then we need to specify the Body,
we say raw, and then JSON.
Oh and also, don't forget of course,
to set this one to POST.
So, what data do we need here?
Remember, first one is the name,
then the email, and I'm putting my own here actually,
then the password, and so let's just
do something here, it doesn't really matter.
And again, we're gonna talk about
password management a bit later,
and actually right in the next video.
So now this one here should be the same.
So pass1234, just as a test.
And the photo remember is not required,
and so I'm not doing that.
And so let's see now.
And now we get this error here,
and that's because I misspelled password here.
Okay, so that would be the German way of writing it.
And now it's right.
Okay, and indeed, we get our new user here.
Perfect.
Now let's say that we tried to do this with a GET.
What would we get?
Well, "This route is not yet defined,"
which is because we actually already
kind of defined this route here,
so basically this part here of the route,
so let me quickly show you that,
so it's not in the tourController,
but in the userController.
So basically we have getUser here,
and then "This route is not yet defined."
So that's the one that we're getting,
but of course we're not not really interested in that.
And let's now go ahead and save this here,
to Postman so that we can then easily reuse it.
So Sign Up.
And now just to quickly confirm,
let's actually open it in Compass here as well,
and just see that at this point we only have tours,
and so let's go here to natours, try to reload this,
and that's probably up here,
and so indeed, now we get our users collection here.
So let's open that up, and indeed,
we now get our first document here.
So the user that we just created.
Now in here you can see the plain password,
and so of course that is not ideal.
And of course, that is not ideal,
so in a web application, passwords should never never ever
be stored as plain in a database.
So there's a lot of password management that needs to go on
behind the scenes, and so that is
exactly what we will do in the next video.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.