So let's get started working on the back and forth on occasion, and this is going to take a while,
but let's get started.
And the first thing I'm going to do is create a route to a nonexistent handler.
So I will come down here and create a mock post because our request from the front end is opposed and
we're calling API APIs authenticate.
That's the URL we put in our JavaScript Fernet and we're going to go to app to create off Tolkan, which
might not create the token if the authentication doesn't pass.
But that's what we're going to try to do in that handler and we don't have the handler yet.
So let's go over to handlers Dash API Dutko and I'll go to the very bottom of this file and we'll create
a new handler here func and it has the receiver of App Pointer to application.
As always, I'm going to call this create auth token right there and we'll give it its response writer
and its pointer to a request.
Oops.
OK, now what are we going to do in here?
Well, we're actually going to be getting username and password as JSON now.
I could if I wanted to actually read the request body, create a struct, read the input into it with
the decoder or we're going to be doing that an awful lot, actually a great deal.
So perhaps it makes sense at this point to actually call the utility to read that for us.
So one of the first is create the variable we're going to pass to that utility and I'm going to do it
this way over and I'll just call it user input, because this is what they're inputting and it's going
to be a struct.
And I'll give it to members email, which will be string.
And I'll say in JSON that's going to be called email.
So we're doing this in one step instead of creating a type.
And we're also going to have a password, which again, will be a string.
And we're going to read that from JSON and it will be called password induced.
OK, so I have this and of course, that's an empty variable right now.
And what I want to do next is to actually create a helper function that will allow us to read just generic
JSON.
We we pass some a pointer to some variable to this function along with the other things it needs.
It reads it and we're done.
So I'll go back to my API folder and I'll create a new file in there.
I'm not going to call it Helper's because these will be helper functions and this package is main and
I'll create a function here which will have the receiver of the pointer to application.
And I'm going to call this read Jason JSM like that.
Actually, those are all capsule's megamall caps region and it's going to take three arguments w which
will be a response rate or.
Which we might need her, which is the pointer to a request which we definitely need, and then we'll
just have something called data, which is just going to be an interface because we don't know what's
coming for the third parameter and it will potentially return an error.
Now, the first thing I'll do is just put some sanity checks in place.
I'm going to say I'm not going to handle anything bigger than, say, one megabyte.
So I'll create a variable called Max Bytes and I'll give it the value of one zero four eight five seven
six.
So one megabyte, that's how many bytes we're going to allow.
And now we'll take our request body and that's going to be equal to HDB Max Bytes reader.
And it requires three arguments are heard on body and then Max Bytes, but that needs to be an integer.
Sixty four.
So we'll just cast it within sixty four.
It's sixty four and we'll pass it max bytes and that will ensure we don't get some malicious user trying
to pass us, pass us a massive request.
But OK, now we'll create a decoder so I'll call it deck for Decoder and that's a sign of the value
of Jason Dot new decoder and will hand it our body which has been sanitized to be no more than one million
size.
And we'll try decoding error, a sign the value of Delcourt Decode and we're going to hand it data that
third parameter in our function request.
So regions' third parameter.
And that can be any format we want and it's going to try to decode it.
And if it can't succeed, we need to hand back an error.
So if error is not equal to nil, just return the error.
Otherwise it's managed to decode it.
Now, as strange as this might seem, we're going to decode it again.
Error is equal to now.
DeCourcy decode into a reference to struct.
With two sets of parentheses, OK, curly braces, and I will say if error is not equal to Yossef,
then we're going to return.
Another error will return errors.
The new Lordi must only have a single Jason value like that, OK?
The reason being, I have to this has to be a you instruct the reason being I'm going to assume that
any time we try to decode a JSON file that we're only going to have one entry.
We don't want multiple entries in there.
And I see you didn't import IO for me, so let me try that again, Io.
And it didn't import errors, so let's import the errors, error errors there, and it should have imported
that form and it did.
So now we have a region, JSON function almost complete.
All we have to do here now say return nil.
And because we've actually received our data interface as a reference to a variable, we're actually
just changing a pointer value and this is a really clean way to read any kind of JSON we get as a request
body, assuming that that request body has only a single JSON value.
OK, so now we have available to us.
So let's go back to our handlers API.
And let's try reading that value, so all we have to do at this point is to say error is a sign the
value of APT read Jason and it requires a response writer our request and a reference to user input.
And we'll check for an error if error is not equal to nil.
Now, up until this point, we've been doing really basic error checking and just writing to the log
and returning something like that.
So let's get a little more sophisticated here now because we're actually trying to log a user in.
We want to send back meaningful information.
So I'm going to go back to my helper's don't go and I'm going to create another function here.
Once that was an error string.
You should not end with punctuation, of course.
Not much better.
I'm going to create another function here, but I'm going to call it bad request again, Funk with the
receiver of app application and I'll just call this that request.
And it will take a few arguments w which will be a response rate, response rate or two minutes.
And again, the request, which is a pointer to a request.
And we'll also take an error, which I'll call her of type error, and it will return potentially an
error.
And once again, I'll just create a payload ver I'll call it payload.
This will be what we'll be sending back to the user and it will be a struct with her and it will have
say error, which is a boolean.
And in JSON, I'll call that.
Error probably don't need that, but I'm going to put it in there and a message which will be a string
and Jason, I'll call that message.
Scroll up and give ourselves some room now, populate that payload with some information, so payload
error is equal to true.
There is an error and payload.
The message is going to be equal to a third error, which returns a string, which is perfect.
And now I'll just write some Jason.
Now, eventually, I'm probably going to also add a write Jason function because we'll be doing this
all the time, writing Jason to the end user.
But right now, I'll just write it hard coded so I'll have my note and error.
It's a variable I'm declaring and we will.
Jason Marshall Marshall in debt just to make it a little prettier and easier to read.
And we'll give it the variable payload.
That's what we want to marshall with no prefix and I'll put a tab in things, OK?
And we'll check for an error.
If there is not equal to nil, then I'll return the error.
Otherwise I'll just write this up to the end user.
So w that header the set and we're going to set the content type two application JSON and we'll write
it out.
Right.
And I will return nil because there's no error.
OK, so we have this bad request I can use now.
So let's go back to our handlers API and now if there's an error, I can just say after that request
and give it the arguments at once, which are w are the error.
And of course you have to return after that because we've got to stop at this point.
But now we're sending useful information back to the user if something goes wrong.
OK, now at this point, if everything's gone according to plan, I should have that user input populated
with an email and a password.
So what I'll do is write that back to the end user just to make sure everything works.
We're not actually doing any creation of an authentication token yet.
I just want to wire things up and make sure things are talking to each other as they should.
So I'll create a variable which again, I'm going to call payload
and that will be a struct.
And I'll give it this information error,
which will be a boolean, and I'll call it Jason and Jason, I'll call it error.
And again, I'll just have a message.
Of course, this is going to change when we're handing tokens back, but I just want to make sure things
work.
Jason, I'll call that message now.
Populate that.
So payload error equals false because nothing went wrong.
And the payload message.
We'll be equal to success like that.
And now I'll just mercial this and write the information out to the end user, so again, OK, and I'll
ignore the error in this case because I'm just making sure that things work.
Jason Mercial in debt and we're going to mercial payload with no payload, with no prefix and a tab
to edit it and set our header header set content type application, slash Jason
and write it up.
Right.
Oh.
So we have our handler, we have our bad request and we have a means of reading user input, which I
didn't actually if I didn't read JSON right here.
All of that is set up in a room, should be connected to it.
And it appears to be.
So let's start our application.
Make start.
And I'll go back to my Web browser.
I will open my console JavaScript console, I'll click on log in and let's see what we get.
So first of all, let's make sure our validation works.
You may noticed I added a div call me six just to make this a little narrower because it's fairly large
on a laptop.
You can look at the template and see that in the download source when you download the source code.
So let's make sure our validation works.
Looks good.
Now let's give it any email address.
Have here dot com and the password is test and we'll watch the JavaScript console and see if we get
our information displayed properly.
And there it is.
No error message, she says.
OK, so now I know that things are are connected to each other.
They're all wired up properly.
So now we can start to work on actual authentication.