Hey everybody.
Welcome back.
Joining me here with slopestyle common in this lesson we are 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 TTP request because this particular
route which is what they're called in the API the endpoint user slash add 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 that we're going to need to account for in X code.
And the next thing is that our body is a little bit bigger.
So we have a name so there's the user name of the users that is displayed next to in there and the messages
in the chat window and 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 Text value doesn't really matter.
But that's going to be that's going to represent the R.G. 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 email 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 chase on data and the data that we're going
to be interested in getting is the avatar color name email and name.
But also this guy right here the ID.
All right.
So we're going to need to take all that in stride we're gonna need to pass a body we're going to need
to receive some data.
And so we need some way to save all of this information for the user.
So I'm thinking 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.
We're going to say 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 out and talk about this Requip 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 a 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 said.
And that's going to be Avatar color and we're going to initialize that to an empty string.
I'm just going to copy and paste this a few more times.
So then we also have a tar name.
This one here is e-mail and this one here has a name that should cover all five of these that we can
expect to receive as a response from this API endpoint.
All right.
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 user data and we are 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 are going to set the user data service properties
to those values.
Okay.
So we need a function that we can call from other classes that will actually do the setting here.
So you can see Heidi this is going to be a string.
When we say avatar actually this is 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.
OK.
So once we have this and we just close this bottom pane going to see a self-taught ID is equal to the
ID that has passed into this function.
Self-taught Avatar color is equal to the color that is passed into this function.
Self-taught this avatar name is equal to the avatar name that is passed into this function.
Sulphite email is here to the email that is passed into this function and self that name is equal to
the name that is Peston 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.
We'll just call that set have 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 the 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 several variables 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 pane 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
handler that we are ready.
So like usual we're just going to lower case our e-mail that lower case I'm just going to copy from
up here actually.
Or go
and then we're going to need a body to 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 Connect 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 of 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 avatar name Avatar color.
All right.
So the body looks good.
Oh I need a comma here.
There you go.
Next up we need a header.
And remember and like we were just looking in postman this time or header's 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.
Author is zation.
All right.
And the value is capital bear B.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 off service dot instance dot off token.
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 because it's kind of a pain to write out here.
You get that.
And there you go.
There is our second key value pair.
So now this header with the authorization 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.
Let's go ahead and craft our album of our request.
All right so we're you are going to say Elmo year your 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.
I said this is a fancy web request.
We're going to say OK we haven't created the euro yet.
So is jump over to Constance do you.
Are you at Constance.
All right.
So this is going to be called Let your 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 going to say that.
Kufi this constant jump back into service and paste that right here for the euro.
And then the method is still dot post I believe Let's double check that.
Post the parameters is body encoding is Jason in Kooning default headers is header and the response
type is response Jason because we want the response that the HTP request we turn to us in the form of
g on and then I press return on the completion on the closure.
And I want 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 responsa dot result.
Nope nope no response out result.
Dot error is nil.
Then we're going to do some stuff else.
We're going to see completion is false and we are going to debug.
Print the errors or response stories salt error as any can there we.
But if it is successful what are we going to do.
We are going to get our response Giese on right here and we're going to parse it out and said it said
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 are logging user we are going
to get back some data.
We're going to turn that data into a chase object and then parse it out from from there.
So I'm going to say guard Hulet and chase on equal.
Sorry.
We're going to say guard data equal response that data helps return.
Then with that data we're going to create a J some object using Swiftie chastens we can say that just
aren't equal Jaison that we're going to do an initialiser and I wish you would show up here.
All right here's just one data.
You know 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 going to unwrap that and if it doesn't exist it's not going to crash because 50 handles that force
all is going to do his set id equal to an empty string which is just fine for us.
So then we're going to say let color equal J song and that key is Avatar color or you say that string
value.
Now we're going to say let's have a tar name equal to a song that is going to be you have a higher name
string value than the email we're going to see that you know people song and it's key is called e-mail
top string value and they're going to say let name equal sun.
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 going to 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 the avatar name.
Come on e-mail 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 age 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 as 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.
I saw some pretty cool stuff.
So you know 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 God let nothing 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 e-mail we still hearty had.
And then here we need a couple of variables.
All right.
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 default 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.
Right.
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 with that created we can set that here for the avatar name avatar 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
great 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 is a problem
here we need self because we are in a closure is when we say self name and self that Avatar color that
we go that's you get rid of that error.
And if success what can we do.
Well this is 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 segue with identifier unwind because
we have gone through the three stages we've registered a user logged it 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 let's print some stuff
just to also make sure that we're saving correctly to our humanity to service this 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 in I don't have an account sign up now.
All right so here we go and give this name going to call this Johnny adds user.
What a cool name.
All right.
E-mail 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 this just run through one more time.
We are sending
this username this e-mail and this password into this function right here to count preste which then
goes through the three faces that we created the register the user logging the user and this latest
one to create user.
All right.
At which point it should dismiss and print out a couple of things.
All right there we 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 see the value of the 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 it in 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.
Journey testen to e-mail.
Y 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.
Had.
And now it 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 off 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 mean to say get ad start get commit dash em and this is less than 12.
And I'm going to push this push origin less and dash to.
All right.
We'll see you all in the next one.