1
00:00:02,000 --> 00:00:04,000
Now creating users works,

2
00:00:04,000 --> 00:00:07,000
but at the moment, we can create the same user,

3
00:00:07,000 --> 00:00:10,000
with the same email address, multiple times.

4
00:00:10,000 --> 00:00:13,000
And that's typically not what we want to do.

5
00:00:13,000 --> 00:00:15,000
Therefore, to avoid that we do that,

6
00:00:15,000 --> 00:00:18,000
in our signup handler function here,

7
00:00:18,000 --> 00:00:23,000
we should check if a user for a given email already exists.

8
00:00:24,000 --> 00:00:26,000
So once we connected here to the database,

9
00:00:26,000 --> 00:00:30,000
before we hash the password and store the user,

10
00:00:30,000 --> 00:00:33,000
I want to check if a user already exists.

11
00:00:33,000 --> 00:00:36,000
I want to get a existing user by using

12
00:00:36,000 --> 00:00:41,000
db.collection('users') and calling find there,

13
00:00:42,000 --> 00:00:45,000
or findOne to be precise.

14
00:00:45,000 --> 00:00:49,000
FindOne allows us to find one document,

15
00:00:49,000 --> 00:00:51,000
and we pass a object to findOne

16
00:00:51,000 --> 00:00:56,000
where we describe how to search for that document.

17
00:00:56,000 --> 00:00:58,000
Key value pairs here,

18
00:00:58,000 --> 00:01:02,000
and the keys are the keys we have in our documents.

19
00:01:02,000 --> 00:01:05,000
So email, password, or underscore id.

20
00:01:05,000 --> 00:01:07,000
And we can then search for email,

21
00:01:07,000 --> 00:01:09,000
and the values are the values

22
00:01:09,000 --> 00:01:12,000
we want to search for in the database.

23
00:01:12,000 --> 00:01:15,000
So in this case, I want to search for documents,

24
00:01:15,000 --> 00:01:18,000
which already have the email we're getting here

25
00:01:18,000 --> 00:01:20,000
stored in the email field.

26
00:01:21,000 --> 00:01:24,000
Now we should await this, because that it returns a promise.

27
00:01:24,000 --> 00:01:27,000
And then it's either undefined,

28
00:01:27,000 --> 00:01:31,000
if we did not find a user for that email,

29
00:01:31,000 --> 00:01:33,000
or it is the user object.

30
00:01:33,000 --> 00:01:37,000
So if it is not undefined, if it's truthy,

31
00:01:37,000 --> 00:01:40,000
so if it is a object with that user data,

32
00:01:40,000 --> 00:01:43,000
then we know that the user already exists.

33
00:01:43,000 --> 00:01:45,000
So that's then when I want to return

34
00:01:45,000 --> 00:01:48,000
so that we don't execute the other code.

35
00:01:48,000 --> 00:01:51,000
And I want to send back a different response,

36
00:01:51,000 --> 00:01:56,000
a response with a status of still 422, maybe,

37
00:01:56,000 --> 00:01:59,000
and some data where I have a message where I say

38
00:02:00,000 --> 00:02:04,000
user exists already, or something like this.

39
00:02:05,000 --> 00:02:07,000
Now one thing we also want to do here,

40
00:02:07,000 --> 00:02:09,000
which I haven't done before,

41
00:02:09,000 --> 00:02:12,000
is close our database connection.

42
00:02:12,000 --> 00:02:14,000
We should always do that.

43
00:02:14,000 --> 00:02:15,000
So we should use that client

44
00:02:15,000 --> 00:02:18,000
and call close once we're done here,

45
00:02:18,000 --> 00:02:21,000
and also here at the bottom, of course.

46
00:02:21,000 --> 00:02:22,000
We want to do that.

47
00:02:23,000 --> 00:02:26,000
But now with those changes made, we should not be able

48
00:02:26,000 --> 00:02:29,000
to create the same user more than once.

49
00:02:29,000 --> 00:02:31,000
If I try to send this request,

50
00:02:31,000 --> 00:02:35,000
or the already existing email address again,

51
00:02:35,000 --> 00:02:40,000
I get a 422 error with the user exists already message.

52
00:02:41,000 --> 00:02:43,000
If I use a different email address

53
00:02:43,000 --> 00:02:46,000
which does not exist yet, it should work though.

54
00:02:46,000 --> 00:02:47,000
And it does.

55
00:02:48,000 --> 00:02:51,000
And in the database, if we refresh it,

56
00:02:51,000 --> 00:02:56,000
we indeed see that every email address exists only once.

57
00:02:56,000 --> 00:02:58,000
So that's now also working as it should.

