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