1
00:00:00,141 --> 00:00:01,782
- [Instructor] Hi and
welcome back to the course.

2
00:00:01,782 --> 00:00:02,823
In this video,

3
00:00:02,823 --> 00:00:05,926
we're going to start
using SQLite with our API.

4
00:00:05,926 --> 00:00:09,388
And we're going to sign users in.

5
00:00:09,388 --> 00:00:13,619
So we're going to be able
to log in using SQLite.

6
00:00:13,619 --> 00:00:16,433
Now, what that's going to mean,

7
00:00:16,433 --> 00:00:19,335
is that we are going to store users

8
00:00:19,335 --> 00:00:20,263
in SQLite

9
00:00:20,263 --> 00:00:22,035
and when the user

10
00:00:22,035 --> 00:00:24,362
calls the auth endpoint,

11
00:00:24,362 --> 00:00:27,693
we are going to retrieve
their username and password

12
00:00:27,693 --> 00:00:28,880
from the database.

13
00:00:28,880 --> 00:00:31,168
We're going to compare
that username and password

14
00:00:31,168 --> 00:00:34,139
to the username and password
they're sending in the request,

15
00:00:34,139 --> 00:00:36,255
and then we're going to see if they match,

16
00:00:36,255 --> 00:00:37,419
and if they do,

17
00:00:37,419 --> 00:00:40,502
we're going to send back a jwt token.

18
00:00:41,880 --> 00:00:42,713
Okay,

19
00:00:42,713 --> 00:00:43,974
so just to recap,

20
00:00:43,974 --> 00:00:45,381
what we've got right now,

21
00:00:45,381 --> 00:00:48,697
is a class user that has an id,

22
00:00:48,697 --> 00:00:50,864
a username and a password.

23
00:00:52,152 --> 00:00:54,913
And then we have our security dot py file,

24
00:00:54,913 --> 00:00:57,227
and there we have our in memory database,

25
00:00:57,227 --> 00:00:59,272
which is none other than a python list,

26
00:00:59,272 --> 00:01:01,363
that has a single user,

27
00:01:01,363 --> 00:01:04,575
where the username is Bob
and the password is asdf.

28
00:01:04,575 --> 00:01:07,589
Then we have a couple of mappings,

29
00:01:07,589 --> 00:01:11,498
which allow us to retrieve by username

30
00:01:11,498 --> 00:01:13,831
and to retrieve users by id.

31
00:01:15,113 --> 00:01:17,391
When the user authenticates,

32
00:01:17,391 --> 00:01:20,801
i.e sends in the auth endpoint,

33
00:01:20,801 --> 00:01:22,233
with a username and password,

34
00:01:22,233 --> 00:01:24,071
what we do is we pass in
the username and password,

35
00:01:24,071 --> 00:01:25,103
here,

36
00:01:25,103 --> 00:01:27,436
we retrieve the user object,

37
00:01:28,387 --> 00:01:29,645
using the mapping,

38
00:01:29,645 --> 00:01:31,949
and then we compare the users password

39
00:01:31,949 --> 00:01:34,012
to the mappings password.

40
00:01:34,012 --> 00:01:34,944
If they match,

41
00:01:34,944 --> 00:01:36,468
then we assume that everything is right,

42
00:01:36,468 --> 00:01:38,679
so we return the user
and that then gets used

43
00:01:38,679 --> 00:01:41,627
to generate the jwt token.

44
00:01:41,627 --> 00:01:43,767
Whenever they request an endpoint,

45
00:01:43,767 --> 00:01:45,695
where they need to be authenticated,

46
00:01:45,695 --> 00:01:47,866
we use the identity method,

47
00:01:47,866 --> 00:01:50,466
so we get a pay load
coming from the request,

48
00:01:50,466 --> 00:01:52,102
and in that pay load,

49
00:01:52,102 --> 00:01:53,627
we get the identity,

50
00:01:53,627 --> 00:01:55,650
which is the user id,

51
00:01:55,650 --> 00:02:00,154
and there we retrieve the user
object using the id mapping,

52
00:02:00,154 --> 00:02:02,067
and if that doesn't match,

53
00:02:02,067 --> 00:02:04,698
then we assume that the
jwt token was correct,

54
00:02:04,698 --> 00:02:09,214
and the user therefore
knows that he is logged in.

55
00:02:09,214 --> 00:02:11,545
Okay, so that's what we've got right now,

56
00:02:11,545 --> 00:02:14,813
so what's clear is that we
need a way to retrieve users

57
00:02:14,813 --> 00:02:15,988
by username,

58
00:02:15,988 --> 00:02:19,905
and we also need a way
to retrieve users by id.

59
00:02:21,171 --> 00:02:22,004
Okay,

60
00:02:22,004 --> 00:02:23,567
so the first thing we're going to do is

61
00:02:23,567 --> 00:02:24,523
we're going to set up

62
00:02:24,523 --> 00:02:25,606
our database.

63
00:02:26,661 --> 00:02:30,447
In the test dot py file
that we ran earlier,

64
00:02:30,447 --> 00:02:33,114
we were creating the table here.

65
00:02:34,420 --> 00:02:37,503
And this table contained the user id,

66
00:02:38,761 --> 00:02:40,205
username and password,

67
00:02:40,205 --> 00:02:44,247
and then we also inserted
some sample data.

68
00:02:44,247 --> 00:02:46,776
This is a table that we're
going to use for this video,

69
00:02:46,776 --> 00:02:47,924
and we're going to

70
00:02:47,924 --> 00:02:49,974
make a couple of changes
in the coming videos,

71
00:02:49,974 --> 00:02:51,396
but for now,

72
00:02:51,396 --> 00:02:53,609
make sure to run test dot py,

73
00:02:53,609 --> 00:02:56,459
so your table gets created
and some data goes in it,

74
00:02:56,459 --> 00:02:59,333
so that we can verify our API works.

75
00:02:59,333 --> 00:03:00,530
So we're going to just run this,

76
00:03:00,530 --> 00:03:04,697
and make sure the data db is
created with some sample data.

77
00:03:05,953 --> 00:03:07,319
Okay.

78
00:03:07,319 --> 00:03:08,589
Once that's done,

79
00:03:08,589 --> 00:03:10,278
we should have data dot db,

80
00:03:10,278 --> 00:03:12,024
and some data in the database,

81
00:03:12,024 --> 00:03:14,650
but of course we have
to change our methods

82
00:03:14,650 --> 00:03:18,926
to use the database
instead of using this list.

83
00:03:18,926 --> 00:03:20,464
So lets do that first,

84
00:03:20,464 --> 00:03:23,256
in the user dot py file.

85
00:03:23,256 --> 00:03:24,910
The first thing we're
going to do in this file

86
00:03:24,910 --> 00:03:28,655
is we're going to add the
ability to retrieve user objects

87
00:03:28,655 --> 00:03:30,137
from the database.

88
00:03:30,137 --> 00:03:33,970
And we're going to start
by importing SQLite 3,

89
00:03:33,970 --> 00:03:36,655
and apologies for that
pop-up appearing there,

90
00:03:36,655 --> 00:03:38,936
so we're going to import
SQLite 3 at the top,

91
00:03:38,936 --> 00:03:40,994
just so this class,

92
00:03:40,994 --> 00:03:42,131
now is going to,

93
00:03:42,131 --> 00:03:43,394
well, and the entire file really,

94
00:03:43,394 --> 00:03:45,028
but we're going to right
code in this class,

95
00:03:45,028 --> 00:03:47,039
this class is now going
to have the ability

96
00:03:47,039 --> 00:03:49,039
to interact with SQLite.

97
00:03:50,522 --> 00:03:51,863
Okay,

98
00:03:51,863 --> 00:03:52,965
so we already know

99
00:03:52,965 --> 00:03:55,586
how to retrieve data from SQLite,

100
00:03:55,586 --> 00:03:58,677
so let's just go over it again.

101
00:03:58,677 --> 00:04:00,444
We've got an init method,

102
00:04:00,444 --> 00:04:03,018
which is going to take an
id, username and password.

103
00:04:03,018 --> 00:04:06,459
So we're going to be using
that throughout the video.

104
00:04:06,459 --> 00:04:09,121
The first thing I'm going
to create is a new method,

105
00:04:09,121 --> 00:04:10,538
find by username.

106
00:04:11,770 --> 00:04:15,144
And this method is going to
find a user in the database,

107
00:04:15,144 --> 00:04:17,811
searching by the users username.

108
00:04:18,791 --> 00:04:19,709
Okay,

109
00:04:19,709 --> 00:04:24,699
and the thing is going to
take is self and username.

110
00:04:24,699 --> 00:04:26,661
Okay, so this username parameter,

111
00:04:26,661 --> 00:04:30,322
is going to be the parameter
that we are going to search by,

112
00:04:30,322 --> 00:04:33,541
and self is just what every
method needs to interact

113
00:04:33,541 --> 00:04:36,760
with the object of type user.

114
00:04:36,760 --> 00:04:40,032
And then we're going
to do the usual setup.

115
00:04:40,032 --> 00:04:42,123
First, set up a connection,

116
00:04:42,123 --> 00:04:45,534
which is done with SQLite 3 dot connect,

117
00:04:45,534 --> 00:04:46,836
sorry, dot connect,

118
00:04:46,836 --> 00:04:49,210
and then the database
that we're connecting to,

119
00:04:49,210 --> 00:04:50,210
data dot db.

120
00:04:51,049 --> 00:04:54,049
Then we're going to create a cursor,

121
00:04:55,216 --> 00:04:58,453
which is required to run the commands,

122
00:04:58,453 --> 00:05:00,528
then we're going to create our query.

123
00:05:00,528 --> 00:05:04,695
Which is going to be select,
star, from our users table,

124
00:05:07,680 --> 00:05:10,847
where the username is a question mark.

125
00:05:12,471 --> 00:05:13,727
This where clause,

126
00:05:13,727 --> 00:05:16,128
all it's doing is it's
filtering the results.

127
00:05:16,128 --> 00:05:19,503
So select star from users
is going to select every row

128
00:05:19,503 --> 00:05:21,188
in the database,

129
00:05:21,188 --> 00:05:22,839
a row per user,

130
00:05:22,839 --> 00:05:25,954
and the where clause is
going to limit the selection

131
00:05:25,954 --> 00:05:28,013
to be only those rows where

132
00:05:28,013 --> 00:05:30,858
the username matches a parameter.

133
00:05:30,858 --> 00:05:32,406
Okay,

134
00:05:32,406 --> 00:05:34,307
and then we're going to do that result,

135
00:05:34,307 --> 00:05:36,459
is equal to cursor dot execute,

136
00:05:36,459 --> 00:05:38,453
which is going to run the query.

137
00:05:38,453 --> 00:05:41,370
And we have to pass in a parameter,

138
00:05:42,621 --> 00:05:45,229
so that the username gets a value.

139
00:05:45,229 --> 00:05:48,954
And that is going to be username comma.

140
00:05:48,954 --> 00:05:50,263
Now remember,

141
00:05:50,263 --> 00:05:52,121
the parameters

142
00:05:52,121 --> 00:05:54,176
have to be, even though it doesn't go one,

143
00:05:54,176 --> 00:05:57,272
they always have to be
in the form of a tuple.

144
00:05:57,272 --> 00:06:01,179
Apologies for that, that was an accidental

145
00:06:01,179 --> 00:06:02,815
deletion there.

146
00:06:02,815 --> 00:06:04,857
So, always have to be
in the form of a tuple,

147
00:06:04,857 --> 00:06:07,682
and the only way to make a single

148
00:06:07,682 --> 00:06:09,226
value tuple,

149
00:06:09,226 --> 00:06:10,765
is to use brackets,

150
00:06:10,765 --> 00:06:11,937
put the value,

151
00:06:11,937 --> 00:06:13,089
and then a comma,

152
00:06:13,089 --> 00:06:14,096
and then closed brackets.

153
00:06:14,096 --> 00:06:15,017
So we've got here,

154
00:06:15,017 --> 00:06:15,944
inside brackets,

155
00:06:15,944 --> 00:06:18,391
the value and a comma.

156
00:06:18,391 --> 00:06:19,590
If we remove the comma,

157
00:06:19,590 --> 00:06:21,501
all that we are left with,

158
00:06:21,501 --> 00:06:24,053
is the value inside brackets.

159
00:06:24,053 --> 00:06:27,014
Which renders the brackets useless.

160
00:06:27,014 --> 00:06:30,554
That's usually used to do things like

161
00:06:30,554 --> 00:06:33,887
five plus three times eight for example.

162
00:06:35,208 --> 00:06:36,788
The brackets here

163
00:06:36,788 --> 00:06:39,672
are used so that five plus
three gets evaluated first,

164
00:06:39,672 --> 00:06:41,305
and then multiplied by eight,

165
00:06:41,305 --> 00:06:42,358
if we omit the brackets,

166
00:06:42,358 --> 00:06:44,421
what runs is three times eight first,

167
00:06:44,421 --> 00:06:47,302
and then five plus to it.

168
00:06:47,302 --> 00:06:49,453
So that's essentially what happens

169
00:06:49,453 --> 00:06:50,730
if we just have the brackets.

170
00:06:50,730 --> 00:06:52,647
We're running the username first,

171
00:06:52,647 --> 00:06:54,087
but there's really nothing to run there

172
00:06:54,087 --> 00:06:56,844
so its just kind of pointless
to have them inside brackets,

173
00:06:56,844 --> 00:06:58,284
so we always need the comma at the end

174
00:06:58,284 --> 00:07:00,778
to tell python that this is
not a useless pair of brackets,

175
00:07:00,778 --> 00:07:04,541
this is a tuple that we're creating.

176
00:07:04,541 --> 00:07:07,768
Okay, so now the result has the cursor,

177
00:07:07,768 --> 00:07:10,744
which allows us to iterate over the rows,

178
00:07:10,744 --> 00:07:14,161
or to do row equals result dot fetch one.

179
00:07:16,216 --> 00:07:19,198
That's going to just get the first row

180
00:07:19,198 --> 00:07:21,144
out of that result set.

181
00:07:21,144 --> 00:07:22,426
Now okay.

182
00:07:22,426 --> 00:07:24,593
Now, if there are no rows,

183
00:07:24,593 --> 00:07:27,481
result dot fetch one is
going to return none.

184
00:07:27,481 --> 00:07:28,314
Okay,

185
00:07:28,314 --> 00:07:30,481
so if the row is not none,

186
00:07:32,536 --> 00:07:34,364
and remember if row is not none

187
00:07:34,364 --> 00:07:37,401
can just be simplified to 'if row',

188
00:07:37,401 --> 00:07:40,081
then we're going to
create a variable user,

189
00:07:40,081 --> 00:07:44,248
which is going to be 'user
row zero row one row two,

190
00:07:50,158 --> 00:07:51,991
and else user is none.

191
00:07:53,488 --> 00:07:56,241
And at the end we're going
to close a connection.

192
00:07:56,241 --> 00:07:57,524
Remember we don't have to commit

193
00:07:57,524 --> 00:07:59,861
because we didn't add any data.

194
00:07:59,861 --> 00:08:03,007
And we're going to return the user.

195
00:08:03,007 --> 00:08:05,355
Okay so what we've done here is

196
00:08:05,355 --> 00:08:06,473
initialise a connection,

197
00:08:06,473 --> 00:08:07,661
initialise a cursor,

198
00:08:07,661 --> 00:08:09,094
and then written a query

199
00:08:09,094 --> 00:08:10,879
which is going to search through the table

200
00:08:10,879 --> 00:08:12,149
for a given username,

201
00:08:12,149 --> 00:08:14,483
get the result set for that,

202
00:08:14,483 --> 00:08:16,114
get the first row,

203
00:08:16,114 --> 00:08:18,091
and then, if there was a row,

204
00:08:18,091 --> 00:08:20,224
then we've created a user object

205
00:08:20,224 --> 00:08:22,279
with the data from that row,

206
00:08:22,279 --> 00:08:23,706
that is row zero,

207
00:08:23,706 --> 00:08:25,285
is the first column,

208
00:08:25,285 --> 00:08:26,287
the id,

209
00:08:26,287 --> 00:08:28,760
row one is the second column,

210
00:08:28,760 --> 00:08:31,790
username row two is the third column,

211
00:08:31,790 --> 00:08:35,416
the password, so each of these
has to match the parameters

212
00:08:35,416 --> 00:08:36,999
in our init method,

213
00:08:40,239 --> 00:08:43,698
and if the row was
none, then user is none.

214
00:08:43,698 --> 00:08:45,786
Finally we close the connection
and we return to user

215
00:08:45,786 --> 00:08:49,304
which means we are either
returning a user object

216
00:08:49,304 --> 00:08:50,137
or none.

217
00:08:52,420 --> 00:08:56,247
Now notice that we're not using self

218
00:08:56,247 --> 00:08:58,418
anywhere in this method.

219
00:08:58,418 --> 00:08:59,251
But,

220
00:08:59,251 --> 00:09:01,673
we are using the class name,

221
00:09:01,673 --> 00:09:02,506
user.

222
00:09:03,409 --> 00:09:06,860
So therefore, it is probably
worthwhile making this a class

223
00:09:06,860 --> 00:09:08,220
method,

224
00:09:08,220 --> 00:09:10,138
changing the self for cls,

225
00:09:10,138 --> 00:09:13,346
and then changing the user for cls too.

226
00:09:13,346 --> 00:09:17,399
What that means is that now we
are using the current class,

227
00:09:17,399 --> 00:09:18,836
which is user,

228
00:09:18,836 --> 00:09:21,128
as opposed to hard coding
the class name there.

229
00:09:21,128 --> 00:09:22,634
This is just slightly nicer

230
00:09:22,634 --> 00:09:26,415
if we come to change this in the future.

231
00:09:26,415 --> 00:09:27,248
Also,

232
00:09:27,248 --> 00:09:28,851
notice how the row,

233
00:09:28,851 --> 00:09:30,949
has three values,

234
00:09:30,949 --> 00:09:32,462
id is id zero,

235
00:09:32,462 --> 00:09:33,379
index zero,

236
00:09:34,281 --> 00:09:35,931
username is index one,

237
00:09:35,931 --> 00:09:38,058
and password is index two,

238
00:09:38,058 --> 00:09:41,890
and this zero one and two is in order,

239
00:09:41,890 --> 00:09:44,770
and they match the id
username and password.

240
00:09:44,770 --> 00:09:47,395
So instead of passing row zero,

241
00:09:47,395 --> 00:09:48,774
row one, row two,

242
00:09:48,774 --> 00:09:50,357
we can just do row,

243
00:09:51,769 --> 00:09:54,316
passing it as a set of arguments,

244
00:09:54,316 --> 00:09:55,539
of positional arguments,

245
00:09:55,539 --> 00:09:59,661
which are just going to expand
to be the exact same thing.

246
00:09:59,661 --> 00:10:02,852
So what we've accomplished just is

247
00:10:02,852 --> 00:10:05,736
making things a bit nicer.

248
00:10:05,736 --> 00:10:06,569
Okay.

249
00:10:08,173 --> 00:10:11,457
Okay, so this is our
find by username method.

250
00:10:11,457 --> 00:10:12,769
And now believe it or not,

251
00:10:12,769 --> 00:10:14,496
we are going to do something you should

252
00:10:14,496 --> 00:10:15,939
really not do very often,

253
00:10:15,939 --> 00:10:17,949
which is copy and paste.

254
00:10:17,949 --> 00:10:19,690
So what we're going to
do is copy and paste,

255
00:10:19,690 --> 00:10:21,645
make sure the indentation is correct,

256
00:10:21,645 --> 00:10:23,952
so that these two new lines are indented

257
00:10:23,952 --> 00:10:26,465
to the same as these,

258
00:10:26,465 --> 00:10:28,374
and we're going to rename find by username

259
00:10:28,374 --> 00:10:30,311
to find by id.

260
00:10:30,311 --> 00:10:33,517
The username is going to be underscore id,

261
00:10:33,517 --> 00:10:35,594
and really what we're
going to do is change

262
00:10:35,594 --> 00:10:38,011
the where clause to be my id.

263
00:10:39,039 --> 00:10:41,521
And then the parameter here is going to be

264
00:10:41,521 --> 00:10:43,953
underscore id, which
is the parameter name,

265
00:10:43,953 --> 00:10:46,495
and the same is going
to apply for the rest.

266
00:10:46,495 --> 00:10:47,819
So what's going to happen now,

267
00:10:47,819 --> 00:10:49,853
is we're going to do the
exact same thing as above,

268
00:10:49,853 --> 00:10:52,053
but we're searching by user id,

269
00:10:52,053 --> 00:10:53,848
instead of by username,

270
00:10:53,848 --> 00:10:57,204
and now we have our two mappings.

271
00:10:57,204 --> 00:10:58,690
So what we've got now,

272
00:10:58,690 --> 00:11:00,940
is the mapping on username,

273
00:11:01,810 --> 00:11:04,169
and the mapping on user id.

274
00:11:04,169 --> 00:11:05,050
Because,

275
00:11:05,050 --> 00:11:07,106
what this username allowed us to do,

276
00:11:07,106 --> 00:11:08,511
this username mapping, apologies,

277
00:11:08,511 --> 00:11:11,761
was to retrieve a user by its username.

278
00:11:13,966 --> 00:11:15,184
And now we can do that,

279
00:11:15,184 --> 00:11:18,018
with our find by username method.

280
00:11:18,018 --> 00:11:18,851
Okay.

281
00:11:19,951 --> 00:11:23,701
So the user is no longer
going to be equal to

282
00:11:24,894 --> 00:11:26,913
username underscore mapping dot get,

283
00:11:26,913 --> 00:11:28,358
now it is going to be

284
00:11:28,358 --> 00:11:31,406
is user dot find by username,

285
00:11:31,406 --> 00:11:32,413
username,

286
00:11:32,413 --> 00:11:34,778
the username is this parameter there,

287
00:11:34,778 --> 00:11:36,547
and all we're doing now
is we're going to look

288
00:11:36,547 --> 00:11:37,380
in the database

289
00:11:37,380 --> 00:11:39,429
as opposed to in this mapping,

290
00:11:39,429 --> 00:11:41,318
which uses the list.

291
00:11:41,318 --> 00:11:43,562
So we can just delete all this really,

292
00:11:43,562 --> 00:11:46,875
and remember to also
change the identity method,

293
00:11:46,875 --> 00:11:48,972
to instead of use the user id mapping,

294
00:11:48,972 --> 00:11:52,139
it's going to use the user find by id.

295
00:11:55,995 --> 00:11:56,828
Okay.

296
00:11:58,013 --> 00:12:00,932
So we've not changed the
behaviour of our app at all,

297
00:12:00,932 --> 00:12:02,045
all we have done is

298
00:12:02,045 --> 00:12:06,225
instead of storing things
in an in memory database,

299
00:12:06,225 --> 00:12:10,590
now we're going to store
them on a SQLite database.

300
00:12:10,590 --> 00:12:14,640
And we've allowed our
programme to retrieve users,

301
00:12:14,640 --> 00:12:16,772
from that SQLite database.

302
00:12:16,772 --> 00:12:19,485
What I'm going to do is delete data dot db

303
00:12:19,485 --> 00:12:20,838
just to make sure that there's nothing

304
00:12:20,838 --> 00:12:23,039
sort of old or wrong with it.

305
00:12:23,039 --> 00:12:25,726
Then I'm going to get the terminal,

306
00:12:25,726 --> 00:12:27,955
and I'm going to clear this

307
00:12:27,955 --> 00:12:29,886
and spelling incorrectly,

308
00:12:29,886 --> 00:12:32,719
and make sure you are in the right folder,

309
00:12:32,719 --> 00:12:35,593
which in my case is for section five,

310
00:12:35,593 --> 00:12:37,426
make sure you're in the right folder,

311
00:12:37,426 --> 00:12:39,477
and in this folder I have

312
00:12:39,477 --> 00:12:41,388
the code folder,

313
00:12:41,388 --> 00:12:42,380
the venv folder,

314
00:12:42,380 --> 00:12:43,713
and test dot py.

315
00:12:45,848 --> 00:12:49,681
Now we have to do something
very specific now.

316
00:12:52,368 --> 00:12:54,807
I'm going to run python test dot py,

317
00:12:54,807 --> 00:12:56,748
and this is going to create our

318
00:12:56,748 --> 00:12:58,870
data dot db file.

319
00:12:58,870 --> 00:13:00,111
So if I do ls now,

320
00:13:00,111 --> 00:13:03,028
you'll see the data dot db is here.

321
00:13:04,748 --> 00:13:07,581
When I run python code app dot py,

322
00:13:09,988 --> 00:13:12,637
it's going to know that I am running it

323
00:13:12,637 --> 00:13:14,418
from my current folder,

324
00:13:14,418 --> 00:13:15,568
section five,

325
00:13:15,568 --> 00:13:18,050
and therefore all of the files

326
00:13:18,050 --> 00:13:20,484
that look for data dot db,

327
00:13:20,484 --> 00:13:24,310
are going to look in
the section five folder.

328
00:13:24,310 --> 00:13:25,858
Okay.

329
00:13:25,858 --> 00:13:28,820
If I go into the code folder,

330
00:13:28,820 --> 00:13:31,599
and then I do python app dot py,

331
00:13:31,599 --> 00:13:33,577
we're going to have a problem because

332
00:13:33,577 --> 00:13:38,212
there is not data dot db
file inside the code folder.

333
00:13:38,212 --> 00:13:39,377
Okay.

334
00:13:39,377 --> 00:13:42,389
So the python files are going to know

335
00:13:42,389 --> 00:13:45,521
that I'm running the code
from within the code folder,

336
00:13:45,521 --> 00:13:48,180
it's going to look for
data dot db in there,

337
00:13:48,180 --> 00:13:50,080
and it's not going to find it.

338
00:13:50,080 --> 00:13:53,070
Okay, so make sure to be in the folder

339
00:13:53,070 --> 00:13:55,237
where you ran test dot py.

340
00:13:57,632 --> 00:14:00,465
And then run app dot py like that.

341
00:14:02,016 --> 00:14:04,480
Okay, so now we are running,

342
00:14:04,480 --> 00:14:06,685
and what we're going to do is go ahead

343
00:14:06,685 --> 00:14:08,497
and verify that everything works.

344
00:14:08,497 --> 00:14:11,453
So let's go on to postman,

345
00:14:11,453 --> 00:14:13,134
and what I'm going to suggest you do

346
00:14:13,134 --> 00:14:15,386
is open up the section four

347
00:14:15,386 --> 00:14:17,729
collection that we've got here,

348
00:14:17,729 --> 00:14:20,342
and essentially duplicate it.

349
00:14:20,342 --> 00:14:22,191
So go ahead and press the three dots,

350
00:14:22,191 --> 00:14:23,372
press duplicate,

351
00:14:23,372 --> 00:14:26,761
and then you can rename
this to section five,

352
00:14:26,761 --> 00:14:27,594
and that's just cause

353
00:14:27,594 --> 00:14:29,040
we're going to be making a few changes,

354
00:14:29,040 --> 00:14:30,192
and a few improvements,

355
00:14:30,192 --> 00:14:35,071
so you don't wanna be
changing the previous version.

356
00:14:35,071 --> 00:14:36,638
Now we should be able to

357
00:14:36,638 --> 00:14:38,555
authenticate our users.

358
00:14:40,185 --> 00:14:42,915
We are not able to create new users yet,

359
00:14:42,915 --> 00:14:44,198
that's going to come in the next video,

360
00:14:44,198 --> 00:14:45,893
but we are able to authenticate.

361
00:14:45,893 --> 00:14:47,780
And you have to remember to use

362
00:14:47,780 --> 00:14:50,698
a user that does exist in the database.

363
00:14:50,698 --> 00:14:52,426
So lets go back over to atom,

364
00:14:52,426 --> 00:14:54,405
open your test dot py file,

365
00:14:54,405 --> 00:14:57,244
and make sure you're
using a user that exists.

366
00:14:57,244 --> 00:14:59,672
In this file we've inserted three users,

367
00:14:59,672 --> 00:15:02,589
jose asdf, rolf asdf, and anne xyz,

368
00:15:04,448 --> 00:15:06,509
so make sure to use one of those.

369
00:15:06,509 --> 00:15:07,766
Let's go back to postman,

370
00:15:07,766 --> 00:15:10,099
and I've got Jose asdf here,

371
00:15:11,274 --> 00:15:13,186
and indeed when I press send,

372
00:15:13,186 --> 00:15:14,612
the expected comes out,

373
00:15:14,612 --> 00:15:17,521
which is an access token.

374
00:15:17,521 --> 00:15:19,790
Now we can copy that

375
00:15:19,790 --> 00:15:22,540
and go over to post an item name,

376
00:15:23,551 --> 00:15:25,626
we're gonna put in chair,

377
00:15:25,626 --> 00:15:26,559
send that in.

378
00:15:26,559 --> 00:15:28,778
This method does not accept,

379
00:15:28,778 --> 00:15:30,402
it doesn't require a jwt token,

380
00:15:30,402 --> 00:15:32,229
but the get method does,

381
00:15:32,229 --> 00:15:34,354
so we're gonna put that in.

382
00:15:34,354 --> 00:15:35,654
Press send,

383
00:15:35,654 --> 00:15:37,479
it tells us that the request

384
00:15:37,479 --> 00:15:39,058
does not contain an access token,

385
00:15:39,058 --> 00:15:40,480
So we're going to go into the headers,

386
00:15:40,480 --> 00:15:43,084
make sure to have authorization,

387
00:15:43,084 --> 00:15:45,251
jwt space, and then paste.

388
00:15:47,302 --> 00:15:48,733
And there we have the chair back.

389
00:15:48,733 --> 00:15:51,811
So we know that everything
we've done does work.

390
00:15:51,811 --> 00:15:53,405
It's really important,

391
00:15:53,405 --> 00:15:54,687
and after every video

392
00:15:54,687 --> 00:15:56,647
and after every piece of code you write,

393
00:15:56,647 --> 00:15:58,697
you test these things over and over again,

394
00:15:58,697 --> 00:16:02,604
to make sure that you're
not making any mistakes.

395
00:16:02,604 --> 00:16:06,495
If you went and wrote all of
the code for this section,

396
00:16:06,495 --> 00:16:07,597
that is quite a lot,

397
00:16:07,597 --> 00:16:09,553
and then tested it,

398
00:16:09,553 --> 00:16:12,774
it will be really difficult to
find where you've gone wrong,

399
00:16:12,774 --> 00:16:15,898
so it's a lot better to
do this incrementally.

400
00:16:15,898 --> 00:16:18,398
Build things that work slowly,

401
00:16:19,298 --> 00:16:22,820
and make sure that they work every time.

402
00:16:22,820 --> 00:16:23,653
Okay,

403
00:16:23,653 --> 00:16:25,456
so that's everything for this video,

404
00:16:25,456 --> 00:16:27,364
we are now able to log in

405
00:16:27,364 --> 00:16:28,361
using SQLite,

406
00:16:28,361 --> 00:16:29,362
but obviously,

407
00:16:29,362 --> 00:16:32,231
there is static data
in our SQLite database,

408
00:16:32,231 --> 00:16:36,294
so the next step has to be to
write users to the database,

409
00:16:36,294 --> 00:16:38,336
allow them to sign up.

410
00:16:38,336 --> 00:16:39,474
So without further ado,

411
00:16:39,474 --> 00:16:41,890
let's go and do that in the next video.

