Hey everybody.
Welcome back Johnny be here with slopestyle common in this lesson we're completing the third stage of
creating a user.
We've already done the register user logging user where we get the token and now we're going to be using
that token to do the third stage which is to actually create the user.
OK.
So to get started let's go ahead and check out postman and see what we're going to be working with.
All right so first off if we look in headers we have something new.
We have an authorization header.
OK.
So this is a this is where we include the token inside the header of our HTP request because this particular
route which is what they're called in the API the endpoint user slash ad is locked down meaning that
unless this HTP request has an authorization header with a valid token it won't get through the it won't
get through the guards that are on the API.
It will kick it back with an unauthorized unauthorized request.
OK.
So that's the first thing that's different.
Now we're going to need to account for an ex code.
The next thing is that our body is a little bit bigger.
So we have a name so is the user name of the users that is displayed next to in there in the messages
in the chat window.
We have the e-mail we are passing an avatar name.
So this corresponds to the type of Avatar asset that they have selected.
And then the Avatar color which right here is just a string called hex value doesn't really matter.
But that's going to be that's going to represent the b values that are randomly generated once we get
to that point here in several lessons.
All right so the the the four things that we're going to need to worry about name e-mail avatar name
and Avatar color.
That's what we are passing as a body.
And so if we go ahead and send this we are also receiving a sim Jaison data and the data that we're
going to be interested in getting as the Avatar color name email name.
But also this guy right here Heidi.
All right.
So we're going to need to take all that straight we're going need to pass a body we're going to need
to receive some data.
And so we need somewhere to save all of this information for the users.
So anything can we create another service and we're going to call that the user data service.
OK.
So it's just going to be a swift file.
I call this user data service and I save that and this is going to be a class called user data service
and it's going to be a singleton like our other office service is a static that instance equal to user
data service.
And then we're going to have in here the variables that we that we see here in Postman.
So these five right here.
And so we're going to say public private set var ID is equal to an empty string.
And so before we move on this go ahead and talk about this real quick what we're saying is we're going
to create a public Geter variable.
So it's public in that other classes can read it but other classes aren't allowed to set this variable
directly.
That's what this part here means private set.
So other classes are welcome to look at it and see it but only this file is able to directly modify
the value that ID is set to.
OK.
And that's just being good little programmers and encapsulating our data and protecting it from being
unwontedly changed by their classes.
Right so now we're going to say public private set of our.
And that's going to be Avatar color and we're going to initialize that to an empty string and just going
to copy and paste this a few more times.
So then we also have a Taar name.
This one here is e-mail and this one here is a name that should cover all five of these that we can
expect to receive as a response from this API endpoint.
All right so that's good but now we need a way to set these right because we made these private sets.
So we're going to create a nice little function here and call the set user data service.
And let's just call it that user data and we're going to need to pass into it because what we're going
to do is we're going to create an office service homophile request here in the service and then we're
going to parse out the data that we receive and then we're going to set the user data service properties
to those values.
Okay.
So we're gonna need a function that we can call from other classes that will actually do the setting
here.
Caesareans see Heidi this is going to be a string.
We see Avatar actually this assit color and that's a string.
We got the tar name and that's a string.
We've got e-mail and that's a string.
And then we have a name and a string.
So we're just setting up all of the variables that we're going to need to pass into this function so
that we can set these guys right here.
So once we have this and we just close this bottom pain we're going to see a self that ID is equal to
the ID that is passed into this function.
Self-taught Avatar color is equal to the color that is passed into this function.
Self-taught labs avatar name is equal to the avatar name that is passed into this function.
Sulphite email is equal to the email that is passed into this function and self that name is equal to
the name that is passed to this function.
All right.
So that is are you a data service.
And just because I have the advantage of having done this before I also know that we are going to need
a function to update the avatar name.
I will just call that set a tar name and we'll pass in an avatar name of type string.
And with that we are going to say self-taught avatar name is equal to the avatar name that is passed
into it.
All right.
So just something that we're going to use later on.
All right.
So let's go ahead and then jump into our service starts with swift file and actually code up this add
user function.
So we are going to call it phunk create user and we are going to be calling this from the create account
screen.
So we are going to need to pass in to this function of several variables where we need to get the name
are going to need the email which is a string.
We're going to need the avatar name which is a string.
We're going to need to close this right hand painting over here.
We're going to need to know the Avatar color which is also a string.
And then we are going to need our completion handler like usual put in our it's keeping and completion
Hendler that we are ready.
So like usual we're just going to lower case our e-mail to lower case I'm just going to copy from up
here actually go
and then we're going to need a body.
I want to copy this one here.
This body's a little bit longer.
All right let's go ahead and run back to postman and show you what we need we need name email avatar
name and Avatar color.
All right so we have Shogunate cut that right here.
Change it to name.
And this is going to be name email is the lowercase imho.
We're going to need avatar name.
And that's going to be the avatar name then.
But our color and that is a tar color.
And I'm just going to double check these all match up name email name email that make sure that the
spelling is correct avatar name Avatar color name Avatar color.
All right.
So the body looks good oh I need a comma here.
There we go.
Next up we need a header.
And remember and like we were just looking in postman this time or headers a little bit different.
We have an extra key value pair with the authorization header and the bearer token.
OK so easy enough.
We've already created a barrier or a header before so let's just say that header is equal to and this
time the key is called authorization.
It's built that very wrong offer is zation.
All right.
And the value is capital bear B.A.A. rdr.
And then you have to have a space and then what comes after that is the token.
So we're going to do some string interpolation and say auth service to instance that off tokens.
OK.
And then we have still our other one which is the content.
You know what I'm going to copy this from the constants.
It's kind of a pain to write out here.
You got that.
And there you go.
There is our second key value pay.
So now this header with the authorization Abair and the Content-Type looks just like the one that we
have here.
So perfect.
All right so now we have our body we have our header just go ahead and craft our album of our request.
All right so we're you are going to say Alamo fire that request and pick this guy here that we've been
using with all of our fancy upgrade options that we can use just like getting the car with all of the
options.
This is a fancy web requests.
We're going to say OK we haven't created the euro yet.
So let's jump over to Constance do you.
Constance.
All right.
So this is going to be called Let you are Owl user underscore add.
And this is going to be similar to our other ones.
But instead of a log in it's user slash add.
All right.
So that's good.
Good to see that Cuppy this constant jump back into office service and paste that right here for the
euro.
And then the method is still docked post I believe plus double check that you post the parameters.
Is body encoding.
Is Jason in Kooning default the headers header and the response type is response Jason.
Because we want the response at the HTP request we turn to us in the form of to sign and then press
return on the completion on the closure.
And I'm going to rename that to be response and get rid of this code placeholder and then like we've
been doing we're going to say if response dot result.
Nope no response out result.
That error is nil.
Then we're going to do some stuff else we're going to say completion is false and we are going to Debug.
Print the errors or response stories solt error as any.
And there we go.
But if it is successful what are we going to do.
We are going to get our response on right here and we're going to parse it out and said it set those
properties equal to the properties the corresponding properties in our user data service that we just
created.
All right.
So let's let's go ahead and do that just like we did with our register or logging user We're going to
get back some data.
We're going to turn that data into adjacent object and then parse it out from there.
So I'm going to say guard Hulet and chase on equal.
Sorry we're going say guard data equal response that data helps return then with that data we're going
to create a space object using Swiftie chastens we can say that just on equal Jaison that we're going
to do an initialiser and see I wish you would show up here.
All right here's just one data.
Now you go and the data that we want his rate data.
All right so now all we've got to do is create some variables that we can then pass into our function
that we created here.
So user data.
All right.
So we're going to say let the equal Jason know we're going to drill into the juice on and find it by
its key which is underscore ID as you can see here in post man underscore ID is its key and we're going
to see a string value.
So we are going to unwrap that and if it doesn't exist it's not going to crash because 50 handles that
for us.
All this going to do is set id equal to an empty string which is just fine for us.
So then we're going to say let color equal J-Zone and that key is Avatar color or you say that string
value.
Now we're going to say left have a higher name equal J song that is going to be you have a higher name
string value than the email we're going to see that you know a song and it's key is called e-mail top
string value.
They're going to say let name equal sun where it's key his name string value.
So there are the five variables that we want that come as a response in this if I call then what are
we gonna do with these real we're going to call user data service dot instance and does set user data
id..
And we're going to pass in those that we just created so Id color have a tar name come on.
Email and I want to build that and then say completion is true.
Say that and let's see if these errors go away here.
All right yeah.
So these errors went away.
And yeah that's that's the that's the whole create user function.
So just to run over one more time what is happening.
We are calling a create user function that will be called in our create account VC we are passing into
that function and name email avatar name and Avatar color.
We have a body that is being sent in this TTP request.
We have a header that is also being sent with the HTP request.
But this time we have an authorization header so that's kind of new and exciting.
Then we have then we're creating the request here.
We are sending it to a specific you or L that we that we know it's a post we're sending the body and
the headers and we're getting response we're getting J song has a response and then we are parsing out
the response and setting those values equal to their corresponding properties in our user data service
which we can then use throughout the rest of the app.
So some pretty cool stuff.
So let's see if it works.
We're going to have to jump into our create a PC now and instead of printing out this what we are going
to do is call a service instance does it create user.
And we are going to we haven't gotten the name yet.
Actually that's you know that's that's from this guy right here.
So we're just going to do the same thing that we did here.
So I'm going to say Garlett name equal.
This is going to be user name user name.
Text that text and username text that text.
All right so now we have a name variables.
Just pop that in right here.
Name email we still hearty had.
And then here we need a couple of variables right that we're going to keep track of the avatar name
and color.
All right.
So right now for now we're just going to create a couple of variables different variables so we're going
to be able to give the user the option to select a avatar image and an avatar background color.
If they don't choose to then we're going to have a supplied default image right there.
So that's going to be called her name and the default is just profile default.
And that's actually if you go look at your assets.
That's the name of this guy right here.
So that's our default profile image.
If the user does not pick one for themselves.
All right so that created we can set that here for the avatar name so that her name and then we need
also a default avatar color.
And this is going to look a little bit strange to you right now perhaps but what this is is it's a string
array of the red green blue Alpha properties of a color.
All right.
So the default we're just going to put 0.5 zero point five 0.5 and 1 and this just gives a default like
gray color.
OK.
And we'll talk more about this here in a bit.
And what how this how this is handled later on OK.
But for now we can put in the avatar name and the have a tar color just so that we have values to put
in there.
And then for our completion handler going to press return here for the bhool I'm going to rename it
to success.
And then the code just going to get rid of that and we're going to say if success looks like we've got
a problem here we need self because we are in a closure is when we say self not have a name and a self
Avatar color.
There we go that's you get rid of that error.
And if success what can we do.
Well this is the end of this is the end of the of the three stages.
So at this point what we want to do is say self-taught perform Segway with identifier unwind because
we have gone through the three stages we've registered a user logged in and created that user.
So at that point we would dismiss the create account view and then go through the process of updating
the UI up pulling down the channels updating the chat DC and all of that kind of stuff.
But for now we're going to be doing is just dismissing it.
And let's go ahead and print some stuff just to also make sure that we're saving correctly to our user
data services printout and user data service for instance.
Let's say name and then user data service that instance dot avatar name as well.
All right so are we going to see this and run it.
And I think I think that should be it.
I don't think we're forgetting anything.
All right so here we go.
The big test loggin I don't have an account sign up now.
All right so here we go and give this name going to call this Jhonny adds user.
What a cool name.
All right.
Email is with x x dot com password is 1 2 3 4 5 6.
So here we go.
When I create this account just to when I click on it just run through one more time.
We are sending
this username this e-mail and this password into this function right here create account preste which
then goes through the three faces that we created to register the user logging the user and this latest
one to create user.
All right.
At which point he should dismiss and print out a couple of things.
All right there you go.
So we printed out the instance name and the avatar name.
I actually don't see the avatar name here.
Let's see here solve the avatar name profound default.
That's interesting.
Go ahead and check out over here in our office service OK.
Ah I missed this even though I thought I was double checking avatar name.
So.
But you'll see the value of this dot string value instead of instead of crashing.
All it did was return an empty string.
Even though this did not exist.
All right well what it was before an A and E did not exist.
So let's try that one more time.
You guys probably saw that and were like I was going to crash Johnny.
All right let's do this.
Log in.
Don't have an account.
Johnny testin to e-mail.
Y at y dot com.
One two three four five six.
And here we go.
That we go to business.
We have our user name and our avatar name.
Let's go and check out the Safari lab.
And let's go ahead and go back to our database change check.
And now if this is the first time you're doing it you should have for the first time some users right
here.
All right so let's go ahead and check this out.
There it is.
Johnny testing to so I say guys we are through the process of creating a new user.
We have registered users we have logged in users at which point we get the token which allows us to
access all of the other locked down routes that the API provides.
We created ourselves a nice user data service so that we can use these properties elsewhere in the app.
And yeah I mean we're getting very close to being able to start sending messages adding channels seeing
chats from other people.
So you guys are really enjoying this and have fun.
I know that I am.
So before we sign off I'm just going to go ahead and add our progress.
I'm going to say get ad start get commit dash em and this is lesson 12.
And I'm going to push this get push origin less and dash to.
All right.
I will see you all in the next one.