So let's say that the user next six wants to access the API, like maybe they want to go create a new
to do item, they want to, you know, list out there to DOS, whatever it is.
Let's go ahead and first log in as Nick Sixing create to do so that we don't have to do the full Kearl
thing for creating it to do so.
Going to log out here.
Now it has already working for us.
Let's go back to the home page.
There we go.
Now I've properly logged outlet's log in.
I'm going to be Nick six.
Do my classic password.
I'm going to create to do that just says hello.
That's very important.
Save.
OK.
Now that I have this here, I want to be able to see this.
Hello.
Via the API.
And in fact, if you'll notice, if we go to a new tab here and I go to local host slash, you know,
API slash tatoos, we get a big air here, even though I've logged in as Nick if I try to come here
and see what we've got.
It says, hey, you have not provided me a token.
I cannot show you anything here.
Now, if you do want to change this so that you're actually able to still see it here, if we come back
to our settings dot pi, we can add another authentication class, which is we can accept the token
authentication or session authentication, which is basically what we use inside of a browser.
So just by adding this line, if you go ahead and reload this page, you'll notice, hey, it shows
the next six and that's all back to normal.
But that's besides the point.
We want to start.
You know, if we take this to Dew's and, you know, we do a Kerl for this, let's go ahead and do a
Kerl.
For the following, right?
It's going to say, hey, you've not provided some the proper authentication credentials.
And so if we want to fix this and use that token that we created, we can do the Kerl and we're going
to pass in in the header.
Same thing that we did when we said, you know, that we're looking for the application, Jason.
We're gonna do a dash.
And then here we want to specify capital H.
And then inside of the string capital off authorization, you got to really spell it out there.
Authorization, Colon Space, Tolkan Space.
And then we're going to provide our token there.
So it's really important authorization.
Colon space, tokens, space.
And then let's go ahead and move over to our admen.
Okay.
So I'm going to copy.
This string of letters and numbers copy this.
Come back, paste this in here.
And when I hit enter.
Look at that.
We get the proper information.
We get this helo that I had just created just by providing this Tokino.
And I can use this token to any of the things, create stuff, you know, delete, update, whatever
it is that we want.
And I want to point out here is that, you know, if we try to do the classic authentication, the basic
authentication, if I do you and I say, you know, I want to provide Nick six and the password just
like this.
This is not going to be accepted.
We have said we can only do session or token authentication.
Now, this brings up a situation where, you know what?
If someone already has a user account but they want to go get what their token is or even, for example,
Nick at six, he already got a token.
But let's say, you know, the application has been deleted off the user's phone and they need to install
again for some reason.
Nick six needs to get their token.
How do we get that back?
They don't sign up again.
If they try and sign up again, will say, hey, that usernames already token.
Well, we need to do is have a u r l where the user can get that token.
Now, for me, I think the proper name for that is log in.
Some people argue that it should be, you know, get token, generate token, whatever it is.
But for me, I think it sort of signifies that you're, quote unquote logging in via the API.
You're trying to get that token to say that you can now interact with the API.
So let's go ahead and move back to Adam and let's go to our your URLs.
And I'm going to specify here.
This will just sort of help keep things organized, I'm going to say that this is our to DOS and that
this down here is our API.
This is our authentication.
Let's just keep it off.
And I'm going to copy this path and I'm going to create one for Log-in, which again, all this means
is that it's going to get a token.
So we'll go ahead and save this.
Come back to our views and I'm going to copy everything that we have for the sign up, including that
over R.F. Exempting.
We want the same situation when someone is going to be logging in, a.k.a. getting their tokens.
So let's change this to log in.
Now, there is some changing that we have to do with the code here.
So we still want to make sure that it's a post.
That's definitely true.
We don't want to do this try except thing.
We are going to use one of those eventually.
But for the time being, let's just go ahead and get the data, which that's going to be the same as
last time.
We're gonna use that Jason parser.
Get that information in this time.
We're gonna try and get the user object using the authenticate function.
So this is something that we're gonna have to go grab from the other Vieuxtemps PI.
So if we go ahead and look for this authenticate here, we're gonna go ahead and copy this.
And come back to our views, that pie, we'll paste this in, we don't need the log in or the log out
so we can go ahead and get rid of that.
We just need to authenticate.
And what we're gonna do is authenticate with.
And again, this is another line that we can come grab from this log in.
We're gonna say we want to authenticate with the username and the following password.
So we'll go ahead and copy this and let's come back to our code and will replace that there.
OK.
So make sure that we are now using data instead of the request up post.
Wherever you see, request up post will place that with data.
Also here will replace that.
So someone passes in a username and a password.
We attempt to authenticate that user.
Now this is where we're going to take things a little bit differently here.
So I'm going to keep that Jason response because we're going to use in a second.
But first, let's make sure that the user exists or TSA if user is none.
Meaning that we weren't able to authenticate the user.
Then we need to tell the user things didn't really work out here.
So I'm going to copy the same air that we have above there.
I'm going to pace it down below, except for instead of that user names already taken.
We're going to say something like, I could not log in.
Please check.
Username and password.
OK, so we've got that situation handled.
We need to have the ELT's situation, right, which the Elsas to say, OK, we did get back a user.
So what should we do?
Well, ultimately, what we want to do is return back the user a proper token.
So we're going to say token.
But the question is, how do we go about getting that token?
And in this case, it's not a tool one for a create, it's just a two hundred.
But the question is, how do we get the token object?
Because there's sort of two situations here.
The first one is, is that we'll just be able to find a token that already exists in the database.
So, for example, for next six, there's already a token.
So for next six, all that we have to do is just say, oh, well, the token is equal to the same thing
that we did up here, except it's a get, not a create.
We're gonna say token is equal to objects dot get where users equal to user.
Simple enough, right.
Is just saying go and grab that.
The problem is what happens if Nick not Nick six or you know, Joe, Mark, Sally, whoever it is tries
to log in.
That doesn't already have a token.
Well, we're gonna get an error here.
So what we're gonna do is gonna wrap this.
We're gonna do a try colon tab.
This over there, we're gonna do an except to say, OK, if you don't have one, then this is really
where we copy this line of code directly and just say, go ahead and create a new token.
So whether we pull one from the database or we create a brand new one, we're gonna go ahead and return
this.
So there you have it.
This is how our log in works.
Let's go ahead and test it out here.
So I'm going to go ahead and save what we've got here and we're gonna come back to our terminal.
Now we've got to do a test.
Now, this is gonna be a little bit of Kerl work.
So let's go ahead and copy what we had for an existing post here and let's sort of work on this.
So instead of doing sign up, we want to do log in.
We still want the application slash Jason and we want to do the user name and password.
Yep.
This all looks good.
Okay, so I'm gonna copy this line of code here.
OK.
And we'll see.
This should be working for Nick one, right.
Since he does not have a token.
It should be generating one.
So let's go ahead and go to the terminal here.
I'm going to paste this in.
Hit enter and look at that.
Nick one gets a token and we should be able to if we do this, go ahead and hit up.
Nick six, for instance.
OK.
And look at that.
That's the exact same token as was there before.
And let's go ahead and log in with the original Nick.
OK.
Just like that.
Oh, look, we got to talk it.
So this is so cool.
I want to I want to see if I can get Nick's current outstanding to do so.
Let's go ahead and move back a little bit here.
Right, Kay?
So I want to go look at the tattoos, I'm going to delete the previous token and let's pass in the token
for our NIC user.
And look at that ad in API to my site.
We've done it so to fully come, you know, all the way full circle here.
I'm going to copy what we've what we have here.
OK.
Well, at least next token here.
Let's go ahead and copy that.
In a pace this now.
In fact, let's go ahead and copy the whole thing here, we're gonna need this to create the monster
curl.
I'm going to attempt to vía our API complete.
The very last to do that.
We have that to do of one K.
So let's go ahead and paces here.
So it's going to be a post.
Remember, we want to do a post and we want to go to API slash to dos slash and then give the I.D. which
it's that last one has an idea of one.
Right.
Add an API to my Web site.
So we're gonna do that.
And we want to complete it.
Now remember, what's great about this is we don't have to pass in any sort of data.
So in fact, we can go ahead and get rid of all of this.
Okay.
All we need to do is provide that.
We want the authorization for this particular token.
So I'm going to copy and paste that there.
So now if we take this entire line here, copy this.
Paste it in to the terminal hit enter.
Post is now.
Oh, it's not a post.
It's a put or a patch.
So let's go.
Well, I'm gonna it's gonna take forever.
I would do it that way.
Let's just do this.
Let's change this to a put.
Okay.
And we will copy this line.
Come back to the terminal paste in and look at that.
If we go ahead and go back, let's do the.
Get here.
We'll get an empty list.
There's no more to do.
Items to be completed.
And if we really want to see what this looks like here, let's go back to our to do site.
Right.
So let's log in the via the original nick.
And let's do the password here.
Let's look at the completed.
We've done it.
We have added an API to our Web site.
How good does that feel?
You've got to be do with us feeling great right now.
Well, I'm just excited.
Think about it.
OK, let's go ahead.
Wrap everything up.
I'll see you soon.