Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,110 --> 00:00:04,132
So, let's now connect the front end
2
00:00:04,132 --> 00:00:06,490
to the back end by sending a request
3
00:00:06,490 --> 00:00:09,810
to this Change Password API route.
4
00:00:09,810 --> 00:00:13,530
For this, we got this user profile component
5
00:00:13,530 --> 00:00:15,720
and there the profile form component,
6
00:00:15,720 --> 00:00:19,360
which is responsible for rendering this form,
7
00:00:19,360 --> 00:00:20,800
and it's here where I now
8
00:00:20,800 --> 00:00:22,960
wanna handle the forum submission,
9
00:00:22,960 --> 00:00:27,750
extract the entered values, and then send the HTTP request
10
00:00:27,750 --> 00:00:31,475
or send that extracted data to the parent component.
11
00:00:31,475 --> 00:00:33,636
So to the user profile component,
12
00:00:33,636 --> 00:00:36,704
and then they are send the HTTP requests
13
00:00:36,704 --> 00:00:39,330
depending on where we wanna do that.
14
00:00:39,330 --> 00:00:41,340
So let's start with handling form submission
15
00:00:41,340 --> 00:00:43,117
and getting the entered data,
16
00:00:43,117 --> 00:00:46,350
for this we can add a new function here in the profile form
17
00:00:46,350 --> 00:00:51,350
component, the Submit handler, or however you wanna call it
18
00:00:51,980 --> 00:00:53,820
there we get to the event object
19
00:00:53,820 --> 00:00:58,460
and we can call event prevent default to prevent the browser
20
00:00:58,460 --> 00:01:01,570
default of sending a request automatically
21
00:01:01,570 --> 00:01:02,520
which we don't want.
22
00:01:02,520 --> 00:01:04,438
We wanna control that with JavaScript
23
00:01:04,438 --> 00:01:06,802
and send our own request.
24
00:01:06,802 --> 00:01:08,137
And then we can point
25
00:01:08,137 --> 00:01:13,137
at this submit handler on the, on submit prop on the forum.
26
00:01:14,260 --> 00:01:16,510
That is step number one.
27
00:01:16,510 --> 00:01:18,750
Now for extracting the entered values
28
00:01:18,750 --> 00:01:20,922
we could use use state and listen to changes
29
00:01:20,922 --> 00:01:23,215
on these inputs with every keystroke
30
00:01:23,215 --> 00:01:26,500
with the on change event.
31
00:01:26,500 --> 00:01:28,694
Or we use refs to extract the values
32
00:01:28,694 --> 00:01:30,410
right when we need them.
33
00:01:30,410 --> 00:01:32,260
And I'll do that ladder approach
34
00:01:32,260 --> 00:01:34,500
I'll use that ladder approach.
35
00:01:34,500 --> 00:01:36,810
Hence I'll call use ref here.
36
00:01:36,810 --> 00:01:40,790
And of course, import use ref from react.
37
00:01:40,790 --> 00:01:42,370
To set up a reference
38
00:01:42,370 --> 00:01:47,370
and create a old password ref here
39
00:01:47,550 --> 00:01:49,450
and then duplicate this to also create
40
00:01:49,450 --> 00:01:52,990
a new password to ref.
41
00:01:52,990 --> 00:01:55,760
And now we need to connect these refs to the inputs.
42
00:01:55,760 --> 00:01:57,220
So here with the ref Prop
43
00:01:57,220 --> 00:02:02,090
we connect the new password field to the new password ref
44
00:02:02,090 --> 00:02:05,903
and the old password field to the old password ref.
45
00:02:07,140 --> 00:02:07,973
And now we can use these refs
46
00:02:07,973 --> 00:02:11,400
and submit handler to get the entered values.
47
00:02:11,400 --> 00:02:15,767
So the entered old password can be extracted
48
00:02:15,767 --> 00:02:20,070
by using the old password ref then dot current
49
00:02:20,070 --> 00:02:22,579
as we always do, unwrap stored value
50
00:02:22,579 --> 00:02:27,579
and the same for the entered new password
51
00:02:28,260 --> 00:02:33,260
here we use the new password ref and get the value.
52
00:02:34,210 --> 00:02:36,250
Now we could add validation here.
53
00:02:36,250 --> 00:02:38,943
So we could check if the values are valid.
54
00:02:38,943 --> 00:02:41,663
So that is something we can do here,
55
00:02:42,980 --> 00:02:45,200
but just as before I'll not do it here
56
00:02:45,200 --> 00:02:46,288
because we, in the end
57
00:02:46,288 --> 00:02:49,250
don't proceed on the service side anyways
58
00:02:49,250 --> 00:02:51,432
if the input is wrong and I wanna focus
59
00:02:51,432 --> 00:02:56,020
on that authentication and service side part here.
60
00:02:56,020 --> 00:02:59,640
So I will just assume that the entered inputs are correct,
61
00:02:59,640 --> 00:03:00,930
and with that assumption
62
00:03:00,930 --> 00:03:04,931
we now can send the HTTP request or pass that data
63
00:03:04,931 --> 00:03:09,424
to a parent component, which then sends the HTTP request.
64
00:03:09,424 --> 00:03:10,942
It's up to you, what you wanna do.
65
00:03:10,942 --> 00:03:13,196
I will go for the parent component here
66
00:03:13,196 --> 00:03:16,730
and they offer in the profile form component function.
67
00:03:16,730 --> 00:03:19,233
I'll accept this props argument.
68
00:03:20,500 --> 00:03:23,540
And then here call, let's say a function which we get
69
00:03:23,540 --> 00:03:28,540
on the on Change Password Prop.
70
00:03:28,970 --> 00:03:30,350
The prop name is of course
71
00:03:30,350 --> 00:03:32,800
up to you since it's your component.
72
00:03:32,800 --> 00:03:35,700
And as a argument to dysfunction
73
00:03:35,700 --> 00:03:37,469
which I expect to get on this prop
74
00:03:37,469 --> 00:03:41,720
I pass an object with the old password, which is set equal
75
00:03:41,720 --> 00:03:44,819
to the entered old password and the new password
76
00:03:44,819 --> 00:03:48,393
which all set equal to the entered new password.
77
00:03:49,730 --> 00:03:51,260
Now we just need to make sure
78
00:03:51,260 --> 00:03:54,350
that we set such a on Change Password Prop
79
00:03:54,350 --> 00:03:56,684
on the profile form component when we use it.
80
00:03:56,684 --> 00:03:58,310
And that is something we do
81
00:03:58,310 --> 00:04:00,720
in the user profile component
82
00:04:00,720 --> 00:04:02,965
here we use the profile form component
83
00:04:02,965 --> 00:04:06,560
here We add the on Change Password Prop
84
00:04:06,560 --> 00:04:09,249
and then point out a function which will be passed
85
00:04:09,249 --> 00:04:12,833
into the profile form through that prop.
86
00:04:13,770 --> 00:04:15,639
So here we can add the let's say
87
00:04:15,639 --> 00:04:18,613
Change Password handler function
88
00:04:18,613 --> 00:04:23,613
and pass this as a value into profile form.
89
00:04:24,570 --> 00:04:27,140
And here we get the password data.
90
00:04:27,140 --> 00:04:32,140
So there's object with the old and new password.
91
00:04:32,610 --> 00:04:33,683
We get that here.
92
00:04:33,683 --> 00:04:38,560
And then in here, we can send this HTTP request
93
00:04:38,560 --> 00:04:41,990
and we do this with the fetch function again.
94
00:04:41,990 --> 00:04:46,990
And the URL is slash API slash user
95
00:04:47,340 --> 00:04:48,994
to target this user folder
96
00:04:48,994 --> 00:04:53,063
in the API folder slash changed dash password
97
00:04:53,063 --> 00:04:56,368
to target this change password file.
98
00:04:56,368 --> 00:04:58,507
Now we need to configure this request
99
00:04:58,507 --> 00:05:00,580
with this second argument.
100
00:05:00,580 --> 00:05:03,720
And for example, change the request method.
101
00:05:03,720 --> 00:05:07,066
Here we expect to get a patch request.
102
00:05:07,066 --> 00:05:09,520
Otherwise we don't continue.
103
00:05:09,520 --> 00:05:11,109
And hence, when we sent this request
104
00:05:11,109 --> 00:05:14,523
we need to set the method here to patch.
105
00:05:15,500 --> 00:05:17,190
Then we wanna add a body.
106
00:05:17,190 --> 00:05:21,150
So data to that request in JSON format
107
00:05:21,150 --> 00:05:24,370
And you're all just forward my password data
108
00:05:24,370 --> 00:05:25,560
which is this object
109
00:05:25,560 --> 00:05:29,220
with an old password and new password field.
110
00:05:29,220 --> 00:05:32,340
And you have to make sure that you have old password
111
00:05:32,340 --> 00:05:35,201
and new password written like this in his object.
112
00:05:35,201 --> 00:05:37,486
So did you use these property names
113
00:05:37,486 --> 00:05:40,371
because it's old password and new password
114
00:05:40,371 --> 00:05:42,550
which I try to extract
115
00:05:42,550 --> 00:05:45,700
in my API route on that incoming body.
116
00:05:45,700 --> 00:05:48,900
So if you picked different names here in this object
117
00:05:48,900 --> 00:05:51,307
which you create in the profile form component
118
00:05:51,307 --> 00:05:55,248
then you need to extract that same, these same names
119
00:05:55,248 --> 00:05:57,037
in the API route .
120
00:05:57,037 --> 00:05:59,760
Here, I'm using equal names everywhere
121
00:05:59,760 --> 00:06:01,310
and they offer this should work
122
00:06:02,555 --> 00:06:04,280
now last but not least
123
00:06:04,280 --> 00:06:05,570
I'll add my headers here
124
00:06:05,570 --> 00:06:10,290
and set the content type to application JSON,
125
00:06:10,290 --> 00:06:13,873
to make it clear that this request carries JSON data.
126
00:06:15,590 --> 00:06:19,140
Now that returns a promise and to use sync await all
127
00:06:19,140 --> 00:06:21,270
turn this into an sync function
128
00:06:21,270 --> 00:06:24,483
and await this and get my response here.
129
00:06:26,630 --> 00:06:31,010
And then here, I will simply get my response data
130
00:06:31,010 --> 00:06:33,930
by a waiting response thought, JSON
131
00:06:33,930 --> 00:06:37,160
and then I'll just log that data here.
132
00:06:37,160 --> 00:06:39,870
And of course we typically want to give the user
133
00:06:39,870 --> 00:06:41,120
more feedback.
134
00:06:41,120 --> 00:06:44,070
Logging to the console is not something we wanna do
135
00:06:44,070 --> 00:06:45,940
because the user will and see this.
136
00:06:45,940 --> 00:06:46,773
But this is only for us
137
00:06:46,773 --> 00:06:49,833
as a developer here to check whether it works.
138
00:06:50,760 --> 00:06:54,514
So now, if we save all of that, this should work.
139
00:06:54,514 --> 00:06:58,260
And if we now go to this profile page and reload it
140
00:06:58,260 --> 00:06:59,910
if I open the developer tools,
141
00:06:59,910 --> 00:07:01,860
we can test this now.
142
00:07:01,860 --> 00:07:03,326
For this, I'll enter a new password
143
00:07:03,326 --> 00:07:07,331
and I'll start by entering an incorrect old password.
144
00:07:07,331 --> 00:07:09,507
If I now click on change password
145
00:07:09,507 --> 00:07:14,507
I get a four Oh three error, which says invalid password
146
00:07:15,050 --> 00:07:15,910
which makes sense
147
00:07:15,910 --> 00:07:18,443
because I entered an incorrect old password.
148
00:07:19,380 --> 00:07:21,393
Now I wanna try the correct old password.
149
00:07:21,393 --> 00:07:24,614
But before I do that, let's go to MongoDB Atlas
150
00:07:24,614 --> 00:07:29,614
and find those documents here, again, to update this.
151
00:07:30,750 --> 00:07:33,260
And let's try to memorize this string
152
00:07:33,260 --> 00:07:34,750
of course not the entire string
153
00:07:34,750 --> 00:07:37,411
but we see that it starts with this strange part
154
00:07:37,411 --> 00:07:39,261
has a dot then lower case C K
155
00:07:39,261 --> 00:07:44,242
uppercase AP ends with w eight,
156
00:07:44,242 --> 00:07:47,400
these are things we can memorize.
157
00:07:47,400 --> 00:07:48,960
And if everything works
158
00:07:48,960 --> 00:07:52,379
now that I update the password with a correct old password
159
00:07:52,379 --> 00:07:54,946
this string should change now because we override
160
00:07:54,946 --> 00:07:59,946
that existing password with a new hashed password.
161
00:08:00,170 --> 00:08:01,820
So for now click Change Password
162
00:08:01,820 --> 00:08:04,433
with the correct old password
163
00:08:04,433 --> 00:08:07,993
I get back password updated, which looks good.
164
00:08:09,010 --> 00:08:10,879
And if I now find documents again here,
165
00:08:10,879 --> 00:08:15,760
indeed that is a new hash password.
166
00:08:15,760 --> 00:08:19,440
And if I now log out here and I try to log in again
167
00:08:19,440 --> 00:08:23,520
with the old password, that should fail
168
00:08:23,520 --> 00:08:25,904
and it looks like it does because I'm not redirected.
169
00:08:25,904 --> 00:08:30,904
If I would still be logging my, my data here, we should see
170
00:08:32,756 --> 00:08:36,450
that we have an error there, but I'm not logging it anymore.
171
00:08:36,450 --> 00:08:38,900
We're not handling this case that it fails.
172
00:08:38,900 --> 00:08:40,501
Hence we get no feedback on the UI
173
00:08:40,501 --> 00:08:42,320
but we clearly are not locked
174
00:08:42,320 --> 00:08:44,640
in because we're not redirected.
175
00:08:44,640 --> 00:08:47,280
But if I now try the correct new password
176
00:08:47,280 --> 00:08:49,605
and I click log in, now I am redirected.
177
00:08:49,605 --> 00:08:52,920
So now logging indeed work
178
00:08:52,920 --> 00:08:54,790
and I can then navigate around
179
00:08:54,790 --> 00:08:57,220
as I did before reload this page.
180
00:08:57,220 --> 00:08:58,538
And that all works.
181
00:08:58,538 --> 00:09:00,453
And that's now how we can also work
182
00:09:00,453 --> 00:09:03,257
with our protected API route
183
00:09:03,257 --> 00:09:05,548
which also requires authentication
184
00:09:05,548 --> 00:09:08,150
but which now checks authentication
185
00:09:08,150 --> 00:09:11,390
on the server site instead of on the client side
186
00:09:11,390 --> 00:09:14,963
or for the client side pages as we did it before.
14490
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.