Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,040 --> 00:00:04,310
Now creating users works,
2
00:00:04,310 --> 00:00:07,020
but at the moment, we can create the same user,
3
00:00:07,020 --> 00:00:10,210
with the same email address, multiple times.
4
00:00:10,210 --> 00:00:13,539
And that's typically not what we want to do.
5
00:00:13,539 --> 00:00:15,800
Therefore, to avoid that we do that,
6
00:00:15,800 --> 00:00:18,640
in our signup handler function here,
7
00:00:18,640 --> 00:00:23,640
we should check if a user for a given email already exists.
8
00:00:24,090 --> 00:00:26,820
So once we connected here to the database,
9
00:00:26,820 --> 00:00:30,640
before we hash the password and store the user,
10
00:00:30,640 --> 00:00:33,980
I want to check if a user already exists.
11
00:00:33,980 --> 00:00:36,980
I want to get a existing user by using
12
00:00:36,980 --> 00:00:41,980
db.collection('users') and calling find there,
13
00:00:42,680 --> 00:00:45,560
or findOne to be precise.
14
00:00:45,560 --> 00:00:49,120
FindOne allows us to find one document,
15
00:00:49,120 --> 00:00:51,870
and we pass a object to findOne
16
00:00:51,870 --> 00:00:56,450
where we describe how to search for that document.
17
00:00:56,450 --> 00:00:58,240
Key value pairs here,
18
00:00:58,240 --> 00:01:02,420
and the keys are the keys we have in our documents.
19
00:01:02,420 --> 00:01:05,650
So email, password, or underscore id.
20
00:01:05,650 --> 00:01:07,830
And we can then search for email,
21
00:01:07,830 --> 00:01:09,870
and the values are the values
22
00:01:09,870 --> 00:01:12,600
we want to search for in the database.
23
00:01:12,600 --> 00:01:15,150
So in this case, I want to search for documents,
24
00:01:15,150 --> 00:01:18,100
which already have the email we're getting here
25
00:01:18,100 --> 00:01:20,623
stored in the email field.
26
00:01:21,640 --> 00:01:24,900
Now we should await this, because that it returns a promise.
27
00:01:24,900 --> 00:01:27,570
And then it's either undefined,
28
00:01:27,570 --> 00:01:31,030
if we did not find a user for that email,
29
00:01:31,030 --> 00:01:33,760
or it is the user object.
30
00:01:33,760 --> 00:01:37,770
So if it is not undefined, if it's truthy,
31
00:01:37,770 --> 00:01:40,440
so if it is a object with that user data,
32
00:01:40,440 --> 00:01:43,460
then we know that the user already exists.
33
00:01:43,460 --> 00:01:45,670
So that's then when I want to return
34
00:01:45,670 --> 00:01:48,640
so that we don't execute the other code.
35
00:01:48,640 --> 00:01:51,410
And I want to send back a different response,
36
00:01:51,410 --> 00:01:56,250
a response with a status of still 422, maybe,
37
00:01:56,250 --> 00:01:59,950
and some data where I have a message where I say
38
00:02:00,860 --> 00:02:04,223
user exists already, or something like this.
39
00:02:05,560 --> 00:02:07,610
Now one thing we also want to do here,
40
00:02:07,610 --> 00:02:09,410
which I haven't done before,
41
00:02:09,410 --> 00:02:12,130
is close our database connection.
42
00:02:12,130 --> 00:02:14,260
We should always do that.
43
00:02:14,260 --> 00:02:15,730
So we should use that client
44
00:02:15,730 --> 00:02:18,790
and call close once we're done here,
45
00:02:18,790 --> 00:02:21,690
and also here at the bottom, of course.
46
00:02:21,690 --> 00:02:22,783
We want to do that.
47
00:02:23,770 --> 00:02:26,960
But now with those changes made, we should not be able
48
00:02:26,960 --> 00:02:29,680
to create the same user more than once.
49
00:02:29,680 --> 00:02:31,860
If I try to send this request,
50
00:02:31,860 --> 00:02:35,840
or the already existing email address again,
51
00:02:35,840 --> 00:02:40,840
I get a 422 error with the user exists already message.
52
00:02:41,030 --> 00:02:43,290
If I use a different email address
53
00:02:43,290 --> 00:02:46,940
which does not exist yet, it should work though.
54
00:02:46,940 --> 00:02:47,773
And it does.
55
00:02:48,758 --> 00:02:51,500
And in the database, if we refresh it,
56
00:02:51,500 --> 00:02:56,350
we indeed see that every email address exists only once.
57
00:02:56,350 --> 00:02:58,763
So that's now also working as it should.
4430
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.