- [Instructor] Now let's go ahead
and test our implementation and do it in Postman.
So the first thing we're going to do
is make sure our server is running
so I'm going to do npm start.
And if you see any error messages
because you made some typos
or there's something that's missing,
read the console for the error messages
and go back to those videos
just to make sure.
All right, so let's switch to Postman.
So the first thing we're going to do
is try to get the list of contacts
without entering any token
as if we're unauthorized.
So let's go ahead and do that.
So let's send for contacts
and again, we are getting the unauthorized user.
So that's normal behavior.
The second thing we're going to do now
is create a new user.
So I'm going to change this it auth
and register like so.
And we need a few things when we register in the Body.
So we need a username
and if you're not sure,
what you can do is go back to the code here
and look into your user model.
And you're going to see that we need a username,
email and a password.
So let's go ahead and do that.
So we're going to do a username
so I'm going to do emmanuelhenri
and then email, I'm going to do me@me2.com.
This is not a real email by the way
and then a password.
And again, I'm entering password and not hash password
because password is the data that we're sending
to the APIs and then the APIs use hash password
inside of the database.
So I'm going to do something like test1234.
And I'm going to register.
So this is a POST and not a GET
so we need to select the right method.
So POST and then let's send it
and then it says that it has created a user so we're good.
All right, so now we need to log in
before we can do anything in this API.
So let's go ahead and log in
and remember, the route for login
is simply login.
And again, this is a POST
and we can reuse the same information
so we can go ahead and send this to the APIs.
And now we're getting a token.
So in theory, your front end would leverage that token
to make transactions with the API.
So what we're going to do is simulate this through Postman
by you want to copy everything
in between the quotation marks like so
and then what we're going to do
is for example, get the list
of contacts, so we're going to go to GET.
Do contacts like we've done before like so.
And now we need to enter a few things.
So there's nothing that we need
for a get contact
so we can remove that data here.
And then we send some things inside of our headers.
So we need to send an authorization.
And we need to add JWT
and then paste the token.
So if you remember,
this is the process that we've entered
inside of our controller
and this is where we're going to check
if there is a user logged in through the command
that we've created inside of our index here.
So this.
So this is going to check that we have JWT plus the token
that we have and this is the keyword
that will check our token here.
So let's go back to Postman.
And with these commands, we can basically make a transaction
with the API and we should be good to go.
So let's go ahead and send.
And if you're seeing a list of contacts,
that means that you've sent the right information
to the server.
So therefore, the authorization token
with JWT, if you're seeing the list of contacts
inside of your database, it's all good.
So you have your own Node base authentication system
with an API attached to it.
Let's move on.