Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,190 --> 00:00:04,850
Now with the SignUp API route added,
2
00:00:04,850 --> 00:00:07,040
let's go back to the auth form
3
00:00:07,040 --> 00:00:09,900
and make sure that we talk to this API route
4
00:00:09,900 --> 00:00:12,140
when the user authenticates.
5
00:00:12,140 --> 00:00:15,610
For this in the end, we wanna listen to the form submission
6
00:00:15,610 --> 00:00:18,850
And then if we are not in login mode,
7
00:00:18,850 --> 00:00:21,100
which means we are in create mode,
8
00:00:21,100 --> 00:00:22,771
then we wanna send a request
9
00:00:22,771 --> 00:00:27,410
to our backend here to create a new user.
10
00:00:27,410 --> 00:00:29,450
For this I'll add a new function.
11
00:00:29,450 --> 00:00:32,439
The submit handler, let's say
12
00:00:33,620 --> 00:00:36,820
where we get that event which we use to again,
13
00:00:36,820 --> 00:00:38,220
prevent the default
14
00:00:38,220 --> 00:00:41,260
as we did it many times before this course.
15
00:00:41,260 --> 00:00:43,560
And then here I'll first of all check
16
00:00:43,560 --> 00:00:46,390
if we are in log in mode,
17
00:00:46,390 --> 00:00:50,380
which means we want to log user in,
18
00:00:50,380 --> 00:00:52,200
we want to log a user in.
19
00:00:52,200 --> 00:00:54,350
That's something we can't do yet.
20
00:00:54,350 --> 00:00:56,360
So I'll do nothing here.
21
00:00:56,360 --> 00:01:01,350
But if we are in the not login mode, so in the create mode
22
00:01:01,350 --> 00:01:05,720
then we wanna send a request that creates a new user.
23
00:01:05,720 --> 00:01:07,290
For this I'll create a new function
24
00:01:07,290 --> 00:01:09,330
outside of the component function.
25
00:01:09,330 --> 00:01:12,090
We could also create it in a brand new file.
26
00:01:12,090 --> 00:01:14,851
I'll just do it here to keep the component function lean
27
00:01:14,851 --> 00:01:17,780
and I'll name this function,
28
00:01:17,780 --> 00:01:21,660
create user, like this.
29
00:01:21,660 --> 00:01:24,670
And here I expect to get the email and the password.
30
00:01:24,670 --> 00:01:28,270
We will need to retrieve that from the form in a second.
31
00:01:28,270 --> 00:01:30,890
And then here I wanna send a request.
32
00:01:30,890 --> 00:01:32,660
So again we can use the fetch function
33
00:01:32,660 --> 00:01:35,310
as we did multiple times before in the course
34
00:01:35,310 --> 00:01:38,760
and send the request to slash API slash off
35
00:01:38,760 --> 00:01:42,843
slash sign up to target this signup API route.
36
00:01:43,950 --> 00:01:45,880
Now it should be a post request.
37
00:01:45,880 --> 00:01:48,340
So add this second argument here
38
00:01:48,340 --> 00:01:53,050
and set the methods to post and then add the body.
39
00:01:53,050 --> 00:01:55,260
And the body will be in JSON format
40
00:01:55,260 --> 00:01:58,520
and I'll add an object with email and password,
41
00:01:58,520 --> 00:02:01,303
so with that data we get as parameters.
42
00:02:02,490 --> 00:02:07,380
Then let's also add some headers to pass extra information
43
00:02:07,380 --> 00:02:10,470
about the fact that we're sending JSON data
44
00:02:10,470 --> 00:02:13,880
by adding that content type header like this
45
00:02:13,880 --> 00:02:15,963
And we configured this request.
46
00:02:16,840 --> 00:02:17,841
Now I will turn,
47
00:02:17,841 --> 00:02:20,660
create user into an async function
48
00:02:20,660 --> 00:02:23,300
so that again we can use await here
49
00:02:23,300 --> 00:02:25,640
and of course then we can
50
00:02:25,640 --> 00:02:27,980
wait for our response
51
00:02:29,700 --> 00:02:31,580
and then handle that response.
52
00:02:31,580 --> 00:02:33,570
Now, for example, by getting the data
53
00:02:33,570 --> 00:02:38,430
out of that response with response JSON and awaiting debts
54
00:02:38,430 --> 00:02:41,090
since that all's returns a promise
55
00:02:41,090 --> 00:02:44,690
and then maybe check if not response, okay.
56
00:02:44,690 --> 00:02:47,959
So if it's not okay, if we got a error status code
57
00:02:47,959 --> 00:02:51,390
and then we could frown a new error
58
00:02:51,390 --> 00:02:54,650
where we use data message as a message,
59
00:02:54,650 --> 00:02:58,663
or if we don't have that just say something went wrong.
60
00:02:59,960 --> 00:03:03,170
And then ultimately I'll return data here though,
61
00:03:03,170 --> 00:03:05,293
if we make it past the safe check.
62
00:03:06,240 --> 00:03:07,800
So that's create user.
63
00:03:07,800 --> 00:03:11,120
Now here in that L's case and submit handler,
64
00:03:11,120 --> 00:03:13,380
we wanna call create user
65
00:03:13,380 --> 00:03:16,189
and pass into email and password.
66
00:03:16,189 --> 00:03:18,380
Now for this we need to get access
67
00:03:18,380 --> 00:03:20,500
to the entered email and password.
68
00:03:20,500 --> 00:03:24,440
And we can use state with two way binding for that.
69
00:03:24,440 --> 00:03:27,510
We did that in the block project, for example
70
00:03:27,510 --> 00:03:30,830
or we use refs for a quick and easy access
71
00:03:30,830 --> 00:03:32,380
And I'll do the latter.
72
00:03:32,380 --> 00:03:36,003
I'll import use ref from react like this.
73
00:03:37,190 --> 00:03:41,340
And then in the component function, just create two refs
74
00:03:41,340 --> 00:03:43,310
or the two inputs we have
75
00:03:43,310 --> 00:03:46,450
and we have to email input ref then
76
00:03:46,450 --> 00:03:51,450
and we also have the password input ref like this then.
77
00:03:54,990 --> 00:03:57,110
Now we just need to wire them up.
78
00:03:57,110 --> 00:04:01,890
So on the email input, we set ref equal to email input ref
79
00:04:01,890 --> 00:04:03,955
and on the password input
80
00:04:03,955 --> 00:04:07,283
we set that equal to password input ref.
81
00:04:09,480 --> 00:04:11,060
Now that we got those refs
82
00:04:11,060 --> 00:04:13,540
we can get to data in the submit handler.
83
00:04:13,540 --> 00:04:16,700
So here we got the entered email
84
00:04:16,700 --> 00:04:19,959
by accessing the email input ref dot current dot value.
85
00:04:19,959 --> 00:04:22,050
As you saw before in the course
86
00:04:22,050 --> 00:04:26,104
and get the entered password
87
00:04:26,104 --> 00:04:30,643
by accessing password input ref dot current dot value.
88
00:04:31,580 --> 00:04:34,453
Now we could add validation here.
89
00:04:36,140 --> 00:04:37,433
That's optional.
90
00:04:39,847 --> 00:04:42,350
We definitely need validation in the API route though
91
00:04:42,350 --> 00:04:44,740
because that validation here on the client
92
00:04:44,740 --> 00:04:46,490
can always be tricked
93
00:04:46,490 --> 00:04:49,540
and we do have validation on the API route.
94
00:04:49,540 --> 00:04:51,400
So we'll save some time here
95
00:04:51,400 --> 00:04:55,580
and save some code and not add validation in this component.
96
00:04:55,580 --> 00:04:56,940
So we'll skip this here.
97
00:04:56,940 --> 00:05:00,240
And instead we'll use that data just like this
98
00:05:00,240 --> 00:05:03,040
and then create a user here.
99
00:05:03,040 --> 00:05:05,270
So for this we pass the entered email
100
00:05:05,270 --> 00:05:07,920
as a first argument to create user
101
00:05:07,920 --> 00:05:10,633
and the entered password as a second argument.
102
00:05:11,810 --> 00:05:15,330
Now of course create user is a async function
103
00:05:15,330 --> 00:05:17,360
and hence returns a promise.
104
00:05:17,360 --> 00:05:21,360
So we can all turn submit handler into async function
105
00:05:21,360 --> 00:05:24,390
so that we can use await in here as well.
106
00:05:24,390 --> 00:05:28,300
And await that response, that result of that operation
107
00:05:28,300 --> 00:05:31,190
maybe all the rapid with try catch
108
00:05:31,190 --> 00:05:34,663
to catch any errors we might be facing here.
109
00:05:35,690 --> 00:05:37,630
And then we can handle these errors.
110
00:05:37,630 --> 00:05:39,870
And for the moment I'll just console log
111
00:05:39,870 --> 00:05:44,010
any errors we might be getting, like this.
112
00:05:44,010 --> 00:05:46,668
And if we are successful though
113
00:05:46,668 --> 00:05:47,890
if we stay in that try block
114
00:05:47,890 --> 00:05:51,830
then we of course know that know a user was created.
115
00:05:51,830 --> 00:05:55,580
So then here I'll just console log the regular result
116
00:05:55,580 --> 00:05:57,080
for the moment.
117
00:05:57,080 --> 00:06:00,990
And we can give the user a better feedback, but we did dive
118
00:06:00,990 --> 00:06:04,250
into those things before in the course already.
119
00:06:04,250 --> 00:06:06,630
And therefore here, I really wanna focus
120
00:06:06,630 --> 00:06:10,060
on the authentication exclusive part.
121
00:06:10,060 --> 00:06:13,520
Hence here for the moment, we will just locked a result
122
00:06:13,520 --> 00:06:16,713
to the console and not do anything else.
123
00:06:17,870 --> 00:06:19,790
Now that should be sending a request.
124
00:06:19,790 --> 00:06:23,120
Of course, we now still need to connect the foreign though.
125
00:06:23,120 --> 00:06:27,123
So we connect on submit to the submit handler like this.
126
00:06:27,980 --> 00:06:31,150
And there also is something we still need to do
127
00:06:31,150 --> 00:06:32,533
on the API route.
128
00:06:33,430 --> 00:06:35,920
Because I'm sending a post request here
129
00:06:35,920 --> 00:06:38,630
and indeed only post requests
130
00:06:38,630 --> 00:06:41,530
should trigger this user creation.
131
00:06:41,530 --> 00:06:44,090
So here in this async function handler
132
00:06:44,090 --> 00:06:49,090
I actually wanna check if request method is equal to post
133
00:06:51,080 --> 00:06:53,950
and only continue in that case.
134
00:06:53,950 --> 00:06:56,223
So if request method is post,
135
00:06:58,188 --> 00:07:01,280
then I will execute that code,
136
00:07:01,280 --> 00:07:03,373
otherwise I don't do anything.
137
00:07:04,560 --> 00:07:07,960
Of course therefore alternatively, if I revert this,
138
00:07:07,960 --> 00:07:10,980
we could all check, if the request method is not post
139
00:07:10,980 --> 00:07:15,320
and then just return and only continue if it is post.
140
00:07:15,320 --> 00:07:19,200
Then we don't need to wrap everything with that if check.
141
00:07:19,200 --> 00:07:20,590
We could have done that before
142
00:07:20,590 --> 00:07:23,363
in the course as well in similar cases.
143
00:07:24,570 --> 00:07:29,250
Okay. So now if we save everything this should work.
144
00:07:29,250 --> 00:07:32,260
If we go back to the auth form
145
00:07:32,260 --> 00:07:35,530
reload and click on create new account
146
00:07:35,530 --> 00:07:38,090
and I opened a developer tools to check
147
00:07:38,090 --> 00:07:41,190
for output and errors.
148
00:07:41,190 --> 00:07:43,810
If I enter some data here
149
00:07:43,810 --> 00:07:46,700
let's say a password which is too short,
150
00:07:46,700 --> 00:07:49,950
then I get back a 422 response
151
00:07:49,950 --> 00:07:53,290
with my error message that the password is too short.
152
00:07:53,290 --> 00:07:56,170
And of course we might wanna show that to the user
153
00:07:56,170 --> 00:07:58,450
but for the reasons mentioned a couple of seconds ago
154
00:07:58,450 --> 00:08:00,509
I'm not doing this here.
155
00:08:00,509 --> 00:08:05,010
So if I now enter a password which is long enough
156
00:08:05,010 --> 00:08:08,280
and I sent this request, that looks better
157
00:08:08,280 --> 00:08:10,890
and I get created user as a response.
158
00:08:10,890 --> 00:08:15,690
And if we have a look into our MongoDB collections here,
159
00:08:15,690 --> 00:08:17,810
we have the auth demo database
160
00:08:17,810 --> 00:08:20,796
and in users we see our user data
161
00:08:20,796 --> 00:08:25,690
and we see that password actually is an object though.
162
00:08:25,690 --> 00:08:27,965
That's not entirely correct.
163
00:08:27,965 --> 00:08:30,150
I'll have to look into that again.
164
00:08:30,150 --> 00:08:31,790
That should not be a object.
165
00:08:31,790 --> 00:08:35,400
It should be a super long, strange looking string
166
00:08:35,400 --> 00:08:37,610
But generally it's working.
167
00:08:37,610 --> 00:08:40,059
If you are getting some error with your connection
168
00:08:40,059 --> 00:08:42,659
to MongoDB or something like this
169
00:08:42,659 --> 00:08:46,080
double check that you also white listed your local IP
170
00:08:46,080 --> 00:08:48,480
under network access by the way.
171
00:08:48,480 --> 00:08:52,710
But with that, let's have a look at our password generation
172
00:08:52,710 --> 00:08:56,500
What's going wrong there in the API route.
173
00:08:56,500 --> 00:08:58,340
I'm creating that hash password.
174
00:08:58,340 --> 00:09:01,352
And yes, that of course returns a promise.
175
00:09:01,352 --> 00:09:03,800
I forgot to await that
176
00:09:03,800 --> 00:09:06,350
And hence I stored the promise in that database
177
00:09:06,350 --> 00:09:08,980
instead of the result of that promise,
178
00:09:08,980 --> 00:09:10,250
which would be that string.
179
00:09:10,250 --> 00:09:13,393
And hence we need to await hash password here.
180
00:09:14,340 --> 00:09:15,240
If we do that
181
00:09:15,240 --> 00:09:19,493
And I then send that same request again for the same user.
182
00:09:20,729 --> 00:09:23,010
Now, if we update MongoDB
183
00:09:25,100 --> 00:09:28,450
we see that here, that's our hashed password.
184
00:09:28,450 --> 00:09:29,640
So that's looking good.
185
00:09:29,640 --> 00:09:31,900
Let me delete the average user.
186
00:09:31,900 --> 00:09:34,230
And that's how we can create users.
187
00:09:34,230 --> 00:09:37,250
Totally on our own with the tools we learned
188
00:09:37,250 --> 00:09:39,173
about before in the course already.
14698
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.