1
00:00:07,180 --> 00:00:12,520
Welcome back everybody Johnny B here with Davis stuck come and in this lesson we are going to pick up

2
00:00:12,520 --> 00:00:17,560
right where we left off and we are going to do that next step in creating a user which is to log in

3
00:00:17,560 --> 00:00:18,340
the user.

4
00:00:18,520 --> 00:00:25,050
OK so we have our register user function right here and now we are going to create our logon functions

5
00:00:25,060 --> 00:00:32,250
so right below it I'm just going to say phunk log in user.

6
00:00:32,420 --> 00:00:33,060
OK.

7
00:00:33,380 --> 00:00:38,580
But before we get started let's go ahead and check out our post man request here.

8
00:00:38,590 --> 00:00:40,720
We finished up the originator user.

9
00:00:40,720 --> 00:00:45,290
Now let's check out the luggin user and you'll see that it's pretty similar.

10
00:00:45,290 --> 00:00:51,670
It has the same header type and actually has the same body type as well.

11
00:00:51,670 --> 00:00:53,530
The URL is different.

12
00:00:53,560 --> 00:00:57,450
So actually this Gladding create are you are logging yourself right now.

13
00:00:57,460 --> 00:01:03,520
When a copy of this account slash logging in let's jump back into Constans in here we're going to need

14
00:01:03,520 --> 00:01:15,680
a new you or else to say let your child log in Loeb's log in equal in that it is string and interpolate

15
00:01:15,910 --> 00:01:21,300
the base your L and then add on a counter slash logon.

16
00:01:22,080 --> 00:01:28,580
And just to reiterate once again these your L's are going to a specific location on the API.

17
00:01:28,770 --> 00:01:34,620
And and so that so that the API knows what to do with the specific information and the requests that

18
00:01:34,620 --> 00:01:36,150
it is receiving.

19
00:01:36,150 --> 00:01:40,790
All right so let's jump back to the service and back in the postman.

20
00:01:40,860 --> 00:01:42,480
And so it was pretty similar.

21
00:01:42,660 --> 00:01:48,300
You know the same header the same body but what's different is what the API is doing with it.

22
00:01:48,300 --> 00:01:50,950
So let me see if I got the.

23
00:01:51,000 --> 00:01:55,110
Me Make sure you got mongered D and our local host up and running.

24
00:01:55,110 --> 00:01:55,860
They are.

25
00:01:56,030 --> 00:02:04,160
So if I send this we should actually be doing a logging request on our local hosts once we send.

26
00:02:04,710 --> 00:02:05,610
And sure enough here we go.

27
00:02:05,610 --> 00:02:08,970
We had a successful request.

28
00:02:08,970 --> 00:02:17,040
You can see here the status 200 which means that everything went ok and we receive as a response from

29
00:02:17,040 --> 00:02:18,190
our API.

30
00:02:18,360 --> 00:02:26,330
AJ saw an object with a key of user and a value of the user's e-mail.

31
00:02:26,550 --> 00:02:28,190
But the important one here is the token.

32
00:02:28,260 --> 00:02:28,740
OK.

33
00:02:28,860 --> 00:02:35,130
So remember in less than six we were talking about web requests and API and how some things are locked

34
00:02:35,130 --> 00:02:38,770
down and so you have to have an authorization token in the header.

35
00:02:38,940 --> 00:02:45,210
And this is the authorization token that we are going to need to be able to add to create to finish

36
00:02:45,210 --> 00:02:52,660
the creation of a user process to do sending a message to retrieve messages for the channels et cetera.

37
00:02:52,830 --> 00:02:54,020
So this is very important.

38
00:02:54,200 --> 00:02:54,540
OK.

39
00:02:54,570 --> 00:03:00,340
So let's go ahead and jump back in to execute and and code this out.

40
00:03:00,660 --> 00:03:07,980
So the ALM of our request is actually pretty similar to the register user function.

41
00:03:07,980 --> 00:03:15,320
We are going to need to pass in an e-mail which is of type string a password which is also of type string.

42
00:03:15,510 --> 00:03:21,000
And again we are going to be using our completion handler so that we know when and if the request was

43
00:03:21,060 --> 00:03:30,680
successful from us to completion at escaping and completion handler.

44
00:03:31,320 --> 00:03:36,530
And then like we saw in post-bin the header and body are actually the same.

45
00:03:36,520 --> 00:03:41,530
So I can we can actually just copy this and we'll grab the lowercase e-mail as well.

46
00:03:42,380 --> 00:03:45,320
And you see that we are now duplicating stuff right.

47
00:03:45,320 --> 00:03:48,400
So let's go ahead and make a constant for this header.

48
00:03:48,420 --> 00:03:52,500
So I'm just going to copy this header here or I'm going to cut it actually.

49
00:03:52,570 --> 00:03:58,410
Now we're going to jump into Constans And then here I'm going to make a new section for headers

50
00:04:01,320 --> 00:04:04,440
and I'm going to say let.

51
00:04:04,540 --> 00:04:10,680
I'm going to make this capitalized to fit our constant convention and then I'm going to copy it and

52
00:04:10,680 --> 00:04:15,470
jump back into our servers and see how you fix this up a little bit.

53
00:04:15,480 --> 00:04:22,770
I'm going to replace the header right here in the register user Allum of our request with header and

54
00:04:22,770 --> 00:04:25,200
then right here we can delete this as well.

55
00:04:25,440 --> 00:04:27,670
Let's clean this up a little bit.

56
00:04:27,800 --> 00:04:29,400
Bodies OK.

57
00:04:30,120 --> 00:04:35,150
And then we were ready to create our Alamo fire request.

58
00:04:35,160 --> 00:04:41,220
So just like we did with the register user We're going to say Alamo fire DOD request.

59
00:04:41,420 --> 00:04:48,320
So now we are creating our web request and the URL that we want it to go to is the one that we just

60
00:04:48,320 --> 00:04:59,750
created which is you are l underscore the log in the method it is still a post the parameters the body

61
00:05:00,430 --> 00:05:10,830
and coding is just on encoding that default and the headers is header that constant that we just created.

62
00:05:11,120 --> 00:05:14,810
And in the register we did a dot response string.

63
00:05:14,810 --> 00:05:15,280
Right.

64
00:05:15,640 --> 00:05:21,100
And that I told you several times that normally we're going to be using with response Jaison.

65
00:05:21,110 --> 00:05:27,770
So that's the one that we want this time response Dusan because the response is specified in the API

66
00:05:27,770 --> 00:05:28,850
to be of type.

67
00:05:28,910 --> 00:05:37,070
Jason OK so we're going to select that one and press return again and rename the data response to response

68
00:05:37,700 --> 00:05:40,410
and just get rid of that code.

69
00:05:40,660 --> 00:05:41,120
OK.

70
00:05:41,270 --> 00:05:46,570
So at this point we have received a response from the API.

71
00:05:46,790 --> 00:05:52,750
So first off we want to do a check to make sure that we don't have any errors organise if response.

72
00:05:52,790 --> 00:05:58,340
Result that error is equal to nil meaning there were no errors.

73
00:05:58,340 --> 00:06:00,040
Everything looks good.

74
00:06:00,200 --> 00:06:08,980
So we're going to do some stuff here but else we are just going to say completion is false.

75
00:06:08,990 --> 00:06:14,570
And so that's our completion handler and we're letting any of the functions any of the locations that

76
00:06:14,570 --> 00:06:18,730
call this function know that the request failed basically.

77
00:06:18,910 --> 00:06:20,590
And then we're going to do a Debug.

78
00:06:20,600 --> 00:06:22,330
Print to see what happens.

79
00:06:22,330 --> 00:06:29,070
We're going to say Response Now result that an error as any.

80
00:06:29,060 --> 00:06:31,080
And that'll just print out the error.

81
00:06:31,250 --> 00:06:37,670
But if we were successful we're going to see completion is true.

82
00:06:38,090 --> 00:06:44,300
So you know in the end the last function that we created the Registered User we didn't really need to

83
00:06:44,300 --> 00:06:46,380
do anything with the response did we.

84
00:06:46,430 --> 00:06:49,810
You know we just it was just to register user.

85
00:06:49,810 --> 00:06:53,650
The API didn't actually send any useful data back to us.

86
00:06:53,660 --> 00:06:58,910
But this time here in postman we see that we're getting some information back we're getting a solid

87
00:06:58,970 --> 00:07:04,500
object and now we need to know how to handle receiving.

88
00:07:04,500 --> 00:07:06,130
Jay song from an API.

89
00:07:06,190 --> 00:07:09,350
OK so this is going to be called Chase song parsing.

90
00:07:09,350 --> 00:07:09,840
All right.

91
00:07:10,040 --> 00:07:11,770
So strap in.

92
00:07:11,930 --> 00:07:13,580
I'm going to show you a couple of different ways.

93
00:07:13,580 --> 00:07:18,740
The first is kind of the more lower level at the basics.

94
00:07:19,370 --> 00:07:21,700
At the base level of J-Zone parsing.

95
00:07:21,880 --> 00:07:25,350
And it's important to know how to do it this way.

96
00:07:25,430 --> 00:07:32,390
But then I'm going to show you how to do it with Swiftie Jaison which I think is a much easier way of

97
00:07:32,510 --> 00:07:32,990
doing it.

98
00:07:32,990 --> 00:07:33,640
All right.

99
00:07:33,890 --> 00:07:43,670
So what we're going to do is inside of our response result is is based on value but we need to kind

100
00:07:43,670 --> 00:07:46,500
of turn that into something that we can use here and it's code.

101
00:07:46,670 --> 00:07:48,990
So what we're going to do is a rented flat.

102
00:07:49,870 --> 00:07:52,700
Jaison is equal to a response.

103
00:07:52,760 --> 00:07:55,180
Result that value.

104
00:07:55,620 --> 00:07:58,720
And we're going to cast that as a dictionary.

105
00:07:58,780 --> 00:07:59,440
OK.

106
00:07:59,710 --> 00:08:02,750
Because it is a dictionary of type string.

107
00:08:02,930 --> 00:08:10,070
So I remember looking at Tower Jason objects in the past I know that the key is always a string but

108
00:08:10,070 --> 00:08:16,300
the value could be anything it could be you could be a a ball it could be a string it could be an end

109
00:08:16,300 --> 00:08:17,860
to it could be another just an object.

110
00:08:17,860 --> 00:08:19,670
So we're going to cast it as any.

111
00:08:19,670 --> 00:08:25,100
All right so now we have a choice on variable.

112
00:08:25,130 --> 00:08:30,290
The syntax for working with Jigsaw dictionary is similar to that.

113
00:08:30,350 --> 00:08:32,490
I mean we've cast it as a dictionary.

114
00:08:32,510 --> 00:08:38,280
So really all we have to do to get to it is say if you let email.

115
00:08:38,390 --> 00:08:46,160
So you know coming over here to postman we know that we have an object with a key of user and then the

116
00:08:46,160 --> 00:08:48,460
value is going to be a string of our e-mail.

117
00:08:48,500 --> 00:08:56,450
OK so when I say that e-mail is equal to Jaison and then we're going to drill into that sound object

118
00:08:56,480 --> 00:09:01,680
and we're going to look for the key called What was it called.

119
00:09:01,790 --> 00:09:03,080
It was the user.

120
00:09:03,080 --> 00:09:03,590
OK.

121
00:09:04,010 --> 00:09:05,620
So user.

122
00:09:06,050 --> 00:09:10,980
And we're going to cast that as a string.

123
00:09:11,540 --> 00:09:19,400
And if if we if we get that if there is in fact an object inside of the J sign dictionary called user

124
00:09:19,700 --> 00:09:25,850
then we're going to set the this variable right here.

125
00:09:25,850 --> 00:09:32,640
Self-taught user e-mail equal to that values an answer itself that user e-mail is equal to e-mail.

126
00:09:33,080 --> 00:09:34,890
Not too bad.

127
00:09:34,940 --> 00:09:40,430
So then we're going to also do the same thing with the token that we see here.

128
00:09:40,610 --> 00:09:43,170
So now we're looking for this carrot here token.

129
00:09:43,180 --> 00:09:49,820
So again if you let tokin is equal to Jaison the dictionary that we just created from the Jewson data

130
00:09:50,210 --> 00:09:57,270
and we're going to search for the token key and if we find it we're going to cast it as a string.

131
00:09:57,680 --> 00:10:03,770
And if we are successful if we get it then we're going to say self-taught off token is equal to that

132
00:10:03,770 --> 00:10:05,420
token right there.

133
00:10:05,940 --> 00:10:06,160
All right.

134
00:10:06,200 --> 00:10:09,180
And that's it.

135
00:10:09,530 --> 00:10:15,020
Then once that's successful we can say self-taught is locked in is equal to true because because now

136
00:10:15,020 --> 00:10:17,200
we have successfully logged in user.

137
00:10:17,450 --> 00:10:17,990
So that's it.

138
00:10:18,020 --> 00:10:20,530
That is our logging user function.

139
00:10:20,540 --> 00:10:24,980
In fact we can use this right away if we jump over to our create account.

140
00:10:24,990 --> 00:10:33,020
VC I remember the flow is register user logging user create user cerate here instead of just printing

141
00:10:33,020 --> 00:10:33,710
registered user.

142
00:10:33,710 --> 00:10:41,900
We're going to call our next API call in lines and say Office service and instance start and we're going

143
00:10:41,900 --> 00:10:44,780
to say logon user and we are going to pass in.

144
00:10:44,780 --> 00:10:52,610
Again the email the pass word which is just pass and then the completion handler again we're going to

145
00:10:52,610 --> 00:11:05,650
say success in order to get rid of this and we're going to see if success may just fall back up if success.

146
00:11:05,670 --> 00:11:13,440
Let's go ahead and print this print log in user.

147
00:11:13,680 --> 00:11:18,410
They say a comma and we're going to then also print just to make sure that it worked.

148
00:11:18,410 --> 00:11:23,190
We're going to see our service start instance dot Hauf token.

149
00:11:23,240 --> 00:11:23,520
All right.

150
00:11:23,540 --> 00:11:30,350
I'm going to save that and let's run it and put this back on the success and run it.

151
00:11:32,570 --> 00:11:38,390
We've got our app open a quick log in don't have an account set up here.

152
00:11:38,490 --> 00:11:44,050
And real quick let's jump back into safari Let's go to lab.

153
00:11:44,280 --> 00:11:47,970
And this was the one that we created last time that register.com and I want to use it again.

154
00:11:47,970 --> 00:11:51,900
So I'm actually just going to delete this object here.

155
00:11:51,900 --> 00:11:52,460
OK.

156
00:11:52,690 --> 00:11:59,910
And so that just removes that account object from our database and you can do that with pretty much

157
00:11:59,910 --> 00:12:03,900
any of the collections and objects.

158
00:12:03,900 --> 00:12:07,650
In fact let's just let's just take a look real quick look at what we got here.

159
00:12:07,650 --> 00:12:12,110
Just to kind of go over what we've been doing right now is registering users.

160
00:12:12,130 --> 00:12:14,190
Those are going to be here and accounts.

161
00:12:14,250 --> 00:12:19,590
They're also collections here for channels and messages and users that are generated because of the

162
00:12:19,590 --> 00:12:23,940
work that I've been doing and the while I was creating this app.

163
00:12:24,460 --> 00:12:31,580
But you can actually delete whole collections and you can edit or delete objects inside of the collections.

164
00:12:31,590 --> 00:12:32,210
OK.

165
00:12:32,220 --> 00:12:36,020
So just a quick look at some of your options and lab.

166
00:12:36,510 --> 00:12:41,630
But anyway so back to exclude and our simulator.

167
00:12:41,910 --> 00:12:49,840
So I'm going to write here I'm going to say register at test dot com.

168
00:12:49,980 --> 00:12:55,390
I'm going to put in a password one two three four five six and I'm going to see create a count.

169
00:12:55,470 --> 00:13:04,980
And if all goes according to plan here in just a second we should see that it logs in and then it should

170
00:13:04,980 --> 00:13:06,600
print out the last token as well.

171
00:13:06,600 --> 00:13:08,090
And I think it did just right here.

172
00:13:08,120 --> 00:13:09,690
Case so this.

173
00:13:09,960 --> 00:13:12,040
And sure enough logged in user.

174
00:13:12,060 --> 00:13:14,150
And here is the token.

175
00:13:14,520 --> 00:13:17,240
So let's check in in labs

176
00:13:19,950 --> 00:13:23,490
and going to refresh this.

177
00:13:23,760 --> 00:13:28,040
And once again there it is registred test dot com.

178
00:13:28,080 --> 00:13:35,190
All right so now we have two out of three of our functions required to create a new user we are registering

179
00:13:35,190 --> 00:13:35,900
a user.

180
00:13:36,120 --> 00:13:43,860
And now we are logging in that user so that we get the token so that we can then do the final step of

181
00:13:43,950 --> 00:13:45,630
adding the user.

182
00:13:45,630 --> 00:13:46,600
All right.

183
00:13:47,160 --> 00:13:52,230
So before we finish up here I told you I was going to show you what I think is an easier way of working

184
00:13:52,230 --> 00:13:53,320
with the sun.

185
00:13:53,430 --> 00:14:00,190
So let's jump back into our create a count or not create countless go to our service that's swift and

186
00:14:00,270 --> 00:14:05,120
I am just going to comment this out right here.

187
00:14:05,460 --> 00:14:06,430
Okay.

188
00:14:07,050 --> 00:14:15,030
And first off we're going to import so if the Jews want to import Swiftie Jaison can and just go ahead

189
00:14:15,030 --> 00:14:22,340
and check out their get hub page if you just google for Swiftie and.

190
00:14:22,780 --> 00:14:23,970
It took me right to get her.

191
00:14:23,990 --> 00:14:28,200
But if you just google it and then click on it should be the top resort forget hub and you can come

192
00:14:28,200 --> 00:14:30,790
over here and take a look at it.

193
00:14:30,920 --> 00:14:36,750
Ken talks about how why the typical way of Giese on handling and swift is not so good.

194
00:14:36,750 --> 00:14:38,310
Show some examples.

195
00:14:38,310 --> 00:14:40,480
Kind of like what we were just showing you.

196
00:14:40,500 --> 00:14:45,940
You have to you know you have to cast it into a dictionary.

197
00:14:46,160 --> 00:14:47,250
See where to go.

198
00:14:47,730 --> 00:14:52,680
Yeah you have to cast it into a dictionary and you have to do a whole bunch of if let's to unwrap you

199
00:14:52,680 --> 00:14:53,980
have to cast it.

200
00:14:54,270 --> 00:15:00,170
And so what's with the jokes and does it safely handles all of that for you.

201
00:15:00,180 --> 00:15:08,610
So for instance you you can do something simple just like this instead of having to the flats.

202
00:15:09,060 --> 00:15:13,240
It handles all of that for you under the hood.

203
00:15:13,280 --> 00:15:18,570
And so we're going to rewrite this section right here using Swiftie Jaison.

204
00:15:18,770 --> 00:15:24,740
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

205
00:15:24,740 --> 00:15:30,440
going to guard that data equal response

206
00:15:32,820 --> 00:15:33,790
data.

207
00:15:34,120 --> 00:15:37,680
Kelce returned.

208
00:15:38,050 --> 00:15:38,480
OK.

209
00:15:38,560 --> 00:15:44,640
So we need the data because what Jason does is it creates a chase an object out of the response data.

210
00:15:44,650 --> 00:15:45,580
So then I'm going to say.

211
00:15:45,650 --> 00:15:51,090
And you saw an equal and I'm going to say capital Jason and doing initialiser.

212
00:15:51,370 --> 00:16:01,180
And what we want right here is to just pass in data and the data that we're passing in is data.

213
00:16:01,380 --> 00:16:09,880
Ok so now we have a chase on object Swiftie J song object and so simply the next couple of steps is

214
00:16:09,880 --> 00:16:17,650
just setting self-taught user email equal to a song and we're going to drill down into it and get the

215
00:16:17,980 --> 00:16:19,260
user.

216
00:16:19,630 --> 00:16:29,710
And what I really like about Swifty Jason is that you can say string value OK and this automatically

217
00:16:30,040 --> 00:16:34,210
safely unwraps this value for you.

218
00:16:34,210 --> 00:16:38,350
So you could do the flats and just do that string.

219
00:16:38,710 --> 00:16:45,070
But what this does for you is it'll say it automatically unwraps it for you or it will just send it

220
00:16:45,070 --> 00:16:47,040
to a empty string.

221
00:16:47,140 --> 00:16:47,660
All right.

222
00:16:47,860 --> 00:16:53,110
So that's super helpful because you know there are times when you need to know if something is nil and

223
00:16:53,110 --> 00:16:54,200
to do something about it.

224
00:16:54,310 --> 00:17:02,890
But most of the time if it doesn't exist then you probably just want it to be an empty string or zero.

225
00:17:02,890 --> 00:17:06,630
If it's for numbers and so I think that this is super helpful.

226
00:17:06,730 --> 00:17:18,720
So we're just going to do an essay self-taught off token is equal to g song and that is tokin thought

227
00:17:18,910 --> 00:17:22,120
string value and that's it.

228
00:17:25,220 --> 00:17:36,150
So these four lines versus these one two three four five six seven eight nine I guess that's technically

229
00:17:36,200 --> 00:17:38,460
eight eight lines.

230
00:17:38,550 --> 00:17:39,880
You don't have to do the if let's.

231
00:17:39,900 --> 00:17:44,730
You don't have to cast into dictionaries you don't have to cast as strings.

232
00:17:44,730 --> 00:17:49,670
I think it's a super sleek way of handling working with Jason.

233
00:17:49,860 --> 00:17:56,210
And this is this is this right here is about as easy as it comes working with Jason the the old way.

234
00:17:56,630 --> 00:18:01,960
If you if you have nested chasing objects or arrays it gets a lot hairier.

235
00:18:01,980 --> 00:18:06,460
And the benefits of working with the J-Zone become even more apparent.

236
00:18:06,540 --> 00:18:07,120
All right.

237
00:18:07,500 --> 00:18:16,590
So so you know it's important to know how to do it this way and just because you might not be in an

238
00:18:16,590 --> 00:18:21,870
environment where they want to use with the chase on they me you know really want you to do it the old

239
00:18:21,870 --> 00:18:23,620
fashioned way which is fine.

240
00:18:23,640 --> 00:18:27,030
So just wanted to show you both ways so that you have options.

241
00:18:27,180 --> 00:18:32,990
But I am going to continue using with the J song for the rest of this course.

242
00:18:33,090 --> 00:18:39,960
So I'm going to delete that other stuff and just to make sure that we are still working correctly.

243
00:18:39,960 --> 00:18:44,280
I'm going to run this again and test this one more time.

244
00:18:44,900 --> 00:18:45,420
OK.

245
00:18:45,510 --> 00:18:50,210
So I'm going to log in and have an account sign up here.

246
00:18:50,430 --> 00:18:59,280
Let's create a new one so register to at test dot com and one two three four five six and create an

247
00:18:59,280 --> 00:18:59,920
account

248
00:19:02,550 --> 00:19:05,390
and there we go logged in user and those are off tokin.

249
00:19:05,570 --> 00:19:12,800
All right so our Jason Reflektor worked perfectly and we are good to go on to our third and final stage

250
00:19:12,800 --> 00:19:14,490
of creating a user.

251
00:19:14,820 --> 00:19:21,890
So yeah in this lesson we learned about working with Jason when we talked about it using the the old

252
00:19:21,890 --> 00:19:25,590
way as well as a different way with Swiftie Jaison.

253
00:19:25,640 --> 00:19:27,910
So I hope you guys liked both ways.

254
00:19:27,930 --> 00:19:34,790
Look Mitry to know the first way that I showed you because it's very important that you understand what

255
00:19:34,790 --> 00:19:36,580
kind of what's going on under the hood.

256
00:19:36,940 --> 00:19:41,720
But Yesso this has been some good stuff and you guys are enjoying it so far.

257
00:19:41,720 --> 00:19:48,700
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

258
00:19:48,870 --> 00:19:55,550
to get and get commit Tasche this is less and 11.

259
00:19:56,080 --> 00:19:56,520
All right.

260
00:19:56,660 --> 00:20:01,880
So you guys are excited to finish up the account creation process in the next lesson and I will see

261
00:20:01,880 --> 00:20:03,080
you there.

