Welcome back everybody Johnny B here with Davis stuck come and in this lesson we are going to pick up
right where we left off and we are going to do that next step in creating a user which is to log in
the user.
OK so we have our register user function right here and now we are going to create our logon functions
so right below it I'm just going to say phunk log in user.
OK.
But before we get started let's go ahead and check out our post man request here.
We finished up the originator user.
Now let's check out the luggin user and you'll see that it's pretty similar.
It has the same header type and actually has the same body type as well.
The URL is different.
So actually this Gladding create are you are logging yourself right now.
When a copy of this account slash logging in let's jump back into Constans in here we're going to need
a new you or else to say let your child log in Loeb's log in equal in that it is string and interpolate
the base your L and then add on a counter slash logon.
And just to reiterate once again these your L's are going to a specific location on the API.
And and so that so that the API knows what to do with the specific information and the requests that
it is receiving.
All right so let's jump back to the service and back in the postman.
And so it was pretty similar.
You know the same header the same body but what's different is what the API is doing with it.
So let me see if I got the.
Me Make sure you got mongered D and our local host up and running.
They are.
So if I send this we should actually be doing a logging request on our local hosts once we send.
And sure enough here we go.
We had a successful request.
You can see here the status 200 which means that everything went ok and we receive as a response from
our API.
AJ saw an object with a key of user and a value of the user's e-mail.
But the important one here is the token.
OK.
So remember in less than six we were talking about web requests and API and how some things are locked
down and so you have to have an authorization token in the header.
And this is the authorization token that we are going to need to be able to add to create to finish
the creation of a user process to do sending a message to retrieve messages for the channels et cetera.
So this is very important.
OK.
So let's go ahead and jump back in to execute and and code this out.
So the ALM of our request is actually pretty similar to the register user function.
We are going to need to pass in an e-mail which is of type string a password which is also of type string.
And again we are going to be using our completion handler so that we know when and if the request was
successful from us to completion at escaping and completion handler.
And then like we saw in post-bin the header and body are actually the same.
So I can we can actually just copy this and we'll grab the lowercase e-mail as well.
And you see that we are now duplicating stuff right.
So let's go ahead and make a constant for this header.
So I'm just going to copy this header here or I'm going to cut it actually.
Now we're going to jump into Constans And then here I'm going to make a new section for headers
and I'm going to say let.
I'm going to make this capitalized to fit our constant convention and then I'm going to copy it and
jump back into our servers and see how you fix this up a little bit.
I'm going to replace the header right here in the register user Allum of our request with header and
then right here we can delete this as well.
Let's clean this up a little bit.
Bodies OK.
And then we were ready to create our Alamo fire request.
So just like we did with the register user We're going to say Alamo fire DOD request.
So now we are creating our web request and the URL that we want it to go to is the one that we just
created which is you are l underscore the log in the method it is still a post the parameters the body
and coding is just on encoding that default and the headers is header that constant that we just created.
And in the register we did a dot response string.
Right.
And that I told you several times that normally we're going to be using with response Jaison.
So that's the one that we want this time response Dusan because the response is specified in the API
to be of type.
Jason OK so we're going to select that one and press return again and rename the data response to response
and just get rid of that code.
OK.
So at this point we have received a response from the API.
So first off we want to do a check to make sure that we don't have any errors organise if response.
Result that error is equal to nil meaning there were no errors.
Everything looks good.
So we're going to do some stuff here but else we are just going to say completion is false.
And so that's our completion handler and we're letting any of the functions any of the locations that
call this function know that the request failed basically.
And then we're going to do a Debug.
Print to see what happens.
We're going to say Response Now result that an error as any.
And that'll just print out the error.
But if we were successful we're going to see completion is true.
So you know in the end the last function that we created the Registered User we didn't really need to
do anything with the response did we.
You know we just it was just to register user.
The API didn't actually send any useful data back to us.
But this time here in postman we see that we're getting some information back we're getting a solid
object and now we need to know how to handle receiving.
Jay song from an API.
OK so this is going to be called Chase song parsing.
All right.
So strap in.
I'm going to show you a couple of different ways.
The first is kind of the more lower level at the basics.
At the base level of J-Zone parsing.
And it's important to know how to do it this way.
But then I'm going to show you how to do it with Swiftie Jaison which I think is a much easier way of
doing it.
All right.
So what we're going to do is inside of our response result is is based on value but we need to kind
of turn that into something that we can use here and it's code.
So what we're going to do is a rented flat.
Jaison is equal to a response.
Result that value.
And we're going to cast that as a dictionary.
OK.
Because it is a dictionary of type string.
So I remember looking at Tower Jason objects in the past I know that the key is always a string but
the value could be anything it could be you could be a a ball it could be a string it could be an end
to it could be another just an object.
So we're going to cast it as any.
All right so now we have a choice on variable.
The syntax for working with Jigsaw dictionary is similar to that.
I mean we've cast it as a dictionary.
So really all we have to do to get to it is say if you let email.
So you know coming over here to postman we know that we have an object with a key of user and then the
value is going to be a string of our e-mail.
OK so when I say that e-mail is equal to Jaison and then we're going to drill into that sound object
and we're going to look for the key called What was it called.
It was the user.
OK.
So user.
And we're going to cast that as a string.
And if if we if we get that if there is in fact an object inside of the J sign dictionary called user
then we're going to set the this variable right here.
Self-taught user e-mail equal to that values an answer itself that user e-mail is equal to e-mail.
Not too bad.
So then we're going to also do the same thing with the token that we see here.
So now we're looking for this carrot here token.
So again if you let tokin is equal to Jaison the dictionary that we just created from the Jewson data
and we're going to search for the token key and if we find it we're going to cast it as a string.
And if we are successful if we get it then we're going to say self-taught off token is equal to that
token right there.
All right.
And that's it.
Then once that's successful we can say self-taught is locked in is equal to true because because now
we have successfully logged in user.
So that's it.
That is our logging user function.
In fact we can use this right away if we jump over to our create account.
VC I remember the flow is register user logging user create user cerate here instead of just printing
registered user.
We're going to call our next API call in lines and say Office service and instance start and we're going
to say logon user and we are going to pass in.
Again the email the pass word which is just pass and then the completion handler again we're going to
say success in order to get rid of this and we're going to see if success may just fall back up if success.
Let's go ahead and print this print log in user.
They say a comma and we're going to then also print just to make sure that it worked.
We're going to see our service start instance dot Hauf token.
All right.
I'm going to save that and let's run it and put this back on the success and run it.
We've got our app open a quick log in don't have an account set up here.
And real quick let's jump back into safari Let's go to lab.
And this was the one that we created last time that register.com and I want to use it again.
So I'm actually just going to delete this object here.
OK.
And so that just removes that account object from our database and you can do that with pretty much
any of the collections and objects.
In fact let's just let's just take a look real quick look at what we got here.
Just to kind of go over what we've been doing right now is registering users.
Those are going to be here and accounts.
They're also collections here for channels and messages and users that are generated because of the
work that I've been doing and the while I was creating this app.
But you can actually delete whole collections and you can edit or delete objects inside of the collections.
OK.
So just a quick look at some of your options and lab.
But anyway so back to exclude and our simulator.
So I'm going to write here I'm going to say register at test dot com.
I'm going to put in a password one two three four five six and I'm going to see create a count.
And if all goes according to plan here in just a second we should see that it logs in and then it should
print out the last token as well.
And I think it did just right here.
Case so this.
And sure enough logged in user.
And here is the token.
So let's check in in labs
and going to refresh this.
And once again there it is registred test dot com.
All right so now we have two out of three of our functions required to create a new user we are registering
a user.
And now we are logging in that user so that we get the token so that we can then do the final step of
adding the user.
All right.
So before we finish up here I told you I was going to show you what I think is an easier way of working
with the sun.
So let's jump back into our create a count or not create countless go to our service that's swift and
I am just going to comment this out right here.
Okay.
And first off we're going to import so if the Jews want to import Swiftie Jaison can and just go ahead
and check out their get hub page if you just google for Swiftie and.
It took me right to get her.
But if you just google it and then click on it should be the top resort forget hub and you can come
over here and take a look at it.
Ken talks about how why the typical way of Giese on handling and swift is not so good.
Show some examples.
Kind of like what we were just showing you.
You have to you know you have to cast it into a dictionary.
See where to go.
Yeah you have to cast it into a dictionary and you have to do a whole bunch of if let's to unwrap you
have to cast it.
And so what's with the jokes and does it safely handles all of that for you.
So for instance you you can do something simple just like this instead of having to the flats.
It handles all of that for you under the hood.
And so we're going to rewrite this section right here using Swiftie Jaison.
So the first thing that we're going to do is we need to get the data out of the web response so we're
going to guard that data equal response
data.
Kelce returned.
OK.
So we need the data because what Jason does is it creates a chase an object out of the response data.
So then I'm going to say.
And you saw an equal and I'm going to say capital Jason and doing initialiser.
And what we want right here is to just pass in data and the data that we're passing in is data.
Ok so now we have a chase on object Swiftie J song object and so simply the next couple of steps is
just setting self-taught user email equal to a song and we're going to drill down into it and get the
user.
And what I really like about Swifty Jason is that you can say string value OK and this automatically
safely unwraps this value for you.
So you could do the flats and just do that string.
But what this does for you is it'll say it automatically unwraps it for you or it will just send it
to a empty string.
All right.
So that's super helpful because you know there are times when you need to know if something is nil and
to do something about it.
But most of the time if it doesn't exist then you probably just want it to be an empty string or zero.
If it's for numbers and so I think that this is super helpful.
So we're just going to do an essay self-taught off token is equal to g song and that is tokin thought
string value and that's it.
So these four lines versus these one two three four five six seven eight nine I guess that's technically
eight eight lines.
You don't have to do the if let's.
You don't have to cast into dictionaries you don't have to cast as strings.
I think it's a super sleek way of handling working with Jason.
And this is this is this right here is about as easy as it comes working with Jason the the old way.
If you if you have nested chasing objects or arrays it gets a lot hairier.
And the benefits of working with the J-Zone become even more apparent.
All right.
So so you know it's important to know how to do it this way and just because you might not be in an
environment where they want to use with the chase on they me you know really want you to do it the old
fashioned way which is fine.
So just wanted to show you both ways so that you have options.
But I am going to continue using with the J song for the rest of this course.
So I'm going to delete that other stuff and just to make sure that we are still working correctly.
I'm going to run this again and test this one more time.
OK.
So I'm going to log in and have an account sign up here.
Let's create a new one so register to at test dot com and one two three four five six and create an
account
and there we go logged in user and those are off tokin.
All right so our Jason Reflektor worked perfectly and we are good to go on to our third and final stage
of creating a user.
So yeah in this lesson we learned about working with Jason when we talked about it using the the old
way as well as a different way with Swiftie Jaison.
So I hope you guys liked both ways.
Look Mitry to know the first way that I showed you because it's very important that you understand what
kind of what's going on under the hood.
But Yesso this has been some good stuff and you guys are enjoying it so far.
I'm going to go ahead and commit our stuff that we've been working on so far as I want to say get status
to get and get commit Tasche this is less and 11.
All right.
So you guys are excited to finish up the account creation process in the next lesson and I will see
you there.