1
00:00:01,090 --> 00:00:04,570
Let's get started with the user registration.

2
00:00:04,960 --> 00:00:08,800
So back to the controllers and the users.

3
00:00:08,830 --> 00:00:13,810
Let's start with the first one, and that is the register controller.

4
00:00:14,170 --> 00:00:18,460
So in here you're going to have a couple of steps.

5
00:00:18,490 --> 00:00:27,430
Step number one is we are going to check if the user already exists and you are going to check by email.

6
00:00:27,760 --> 00:00:34,690
So let's go ahead and then structure the payload from the red dot body.

7
00:00:34,870 --> 00:00:40,920
So we want the foo name, email, password, and then the other properties.

8
00:00:40,930 --> 00:00:45,880
So let me show you the properties that we need for user registration.

9
00:00:46,150 --> 00:00:48,460
Let me put it side by side here.

10
00:00:48,760 --> 00:00:49,480
All right.

11
00:00:49,480 --> 00:00:53,500
So we need the full name, which is this one.

12
00:00:53,500 --> 00:01:01,030
And then the email also and the password also.

13
00:01:01,270 --> 00:01:06,580
And then the profile image and then the cover image post and comment.

14
00:01:06,610 --> 00:01:09,370
These are optional fields.

15
00:01:09,370 --> 00:01:13,240
And this one, we are going to add them dynamically.

16
00:01:13,270 --> 00:01:14,110
That is the post.

17
00:01:14,110 --> 00:01:15,430
And then the comment.

18
00:01:15,460 --> 00:01:18,300
So now this what we need for now.

19
00:01:18,310 --> 00:01:20,650
So let's collapse this one.

20
00:01:20,920 --> 00:01:24,970
So next step is that let's check if user exists.

21
00:01:24,970 --> 00:01:30,880
So we need to bring in the user model and to have the auto import.

22
00:01:30,910 --> 00:01:34,940
Make sure you save the file that you want to import.

23
00:01:34,960 --> 00:01:44,230
And now back to the file where I want to use that one and I'll make use of user and I have the auto

24
00:01:44,230 --> 00:01:45,040
import.

25
00:01:45,220 --> 00:01:54,430
So now here we go, make use of cost and then user found and is equal to a weight.

26
00:01:54,790 --> 00:01:59,110
And then the user model we are going to use find one.

27
00:01:59,110 --> 00:02:03,940
And in here we want to find the user by email.

28
00:02:04,360 --> 00:02:12,100
And if the user is being found, then it means that this email is being taken by a different user.

29
00:02:12,130 --> 00:02:15,310
So let's go ahead and throw an error here.

30
00:02:16,270 --> 00:02:20,510
We will have a section for custom error handling.

31
00:02:20,530 --> 00:02:26,690
For the meantime, let's throw this one as an error, but indeed it is not.

32
00:02:26,710 --> 00:02:31,450
So let's go ahead and then create the function execution.

33
00:02:31,540 --> 00:02:43,780
And if user found in here, we are going to return this red dot JSON and we are going to provide the

34
00:02:43,780 --> 00:02:44,710
status.

35
00:02:46,750 --> 00:02:49,360
And you say fail.

36
00:02:50,920 --> 00:02:58,100
Data, rather, is going to be as user already exists.

37
00:02:58,270 --> 00:03:01,810
So let's return by bringing in return here.

38
00:03:02,260 --> 00:03:11,920
So below, if it means that this email is not being taken so we can go ahead and then hash the user

39
00:03:11,920 --> 00:03:15,810
password, but we will come back to it later on.

40
00:03:15,820 --> 00:03:22,870
So let's keep this one and let's go ahead and then create the user or register the user.

41
00:03:23,320 --> 00:03:24,340
So here we go.

42
00:03:24,340 --> 00:03:30,280
Const user is equal to a weight and then the user model.

43
00:03:33,290 --> 00:03:34,760
Dot great.

44
00:03:34,790 --> 00:03:37,970
Then you pass in the properties that we want to create.

45
00:03:38,000 --> 00:03:49,940
You want the full name, the email, the password for now, and let's send the data as data and provide

46
00:03:49,940 --> 00:03:51,230
the user.

47
00:03:52,280 --> 00:03:53,090
Oh, right.

48
00:03:53,090 --> 00:03:55,270
So moment of truth.

49
00:03:55,280 --> 00:04:01,670
So let's get back to the tender client and for users, let's pass in the payload.

50
00:04:01,670 --> 00:04:04,580
But we face some well thing here.

51
00:04:04,580 --> 00:04:06,530
But for the meantime, let's add up.

52
00:04:06,530 --> 00:04:10,580
So click on body and let's provide the JSON data.

53
00:04:10,610 --> 00:04:17,180
So the full name, I want to name this one as Emmanuel.

54
00:04:18,320 --> 00:04:29,120
And then the email for this, I would say email at gmail.com and then the password.

55
00:04:31,000 --> 00:04:37,330
I'll use one, two, three, four, five and hit send and I got some error.

56
00:04:37,360 --> 00:04:46,060
Let me check the console and you can see that it cannot structure the property from the request button.

57
00:04:46,090 --> 00:04:56,080
This means that by default Express doesn't pass the incoming payload unless we configure it manually.

58
00:04:56,710 --> 00:05:01,290
I would suggest that this feature might be implemented by default.

59
00:05:01,300 --> 00:05:07,060
That is, to be able to pass the incoming payload, but here is not the case.

60
00:05:07,060 --> 00:05:12,440
So let's add that one quickly here and express make it pretty easy as well.

61
00:05:12,460 --> 00:05:19,900
So before the live version of EXPRESS, unless we install a third party package called Buddy Pass,

62
00:05:20,230 --> 00:05:28,180
but with Express that the current version we have access to that one on the app dot use and then you

63
00:05:28,180 --> 00:05:32,760
bring in express dot JSON and that is all.

64
00:05:32,770 --> 00:05:37,570
So let me comment this one and say pass incoming data.

65
00:05:38,500 --> 00:05:41,290
So with this one, let's register again.

66
00:05:42,770 --> 00:05:49,190
And now you can see that the user is being registered with the start of success and then the data being

67
00:05:49,190 --> 00:05:54,370
sent to us and post empty comments and other properties.

68
00:05:54,380 --> 00:06:01,700
If you check our database, let's right click and refresh and you can see we have a new database called

69
00:06:01,700 --> 00:06:02,750
full Stack block.

70
00:06:02,750 --> 00:06:11,570
And when I check inside, we can see that I have one collection called Users and inside I have one command.

71
00:06:11,570 --> 00:06:16,790
And when I click on that, this is a user that has been registered.

72
00:06:17,090 --> 00:06:17,810
All right.

73
00:06:17,810 --> 00:06:24,790
But the problem here is that we don't need to say the plane password unless we hash it.

74
00:06:24,800 --> 00:06:29,330
So let's see how we are going to hash the user password in the next video.

