1
00:00:08,780 --> 00:00:09,950
In this video.

2
00:00:09,950 --> 00:00:12,710
Let's go ahead to register a user.

3
00:00:12,860 --> 00:00:18,950
So let's get into the controller of the users and here we go.

4
00:00:19,160 --> 00:00:21,710
So let's start with the registration.

5
00:00:21,800 --> 00:00:26,330
Before we get into that, let's look at the steps that we are going to follow.

6
00:00:26,600 --> 00:00:32,650
Step number one is you want to check if the user is not registered.

7
00:00:32,659 --> 00:00:39,410
So here, check if email exists that first step.

8
00:00:39,620 --> 00:00:48,080
The second step is that you want to check if a user is trying to submit an empty data verrastro mongoose

9
00:00:48,080 --> 00:00:49,490
who through an error.

10
00:00:49,490 --> 00:00:55,410
But we can also check for that inside the controller before getting to the model.

11
00:00:55,430 --> 00:00:56,690
So here we go.

12
00:00:56,720 --> 00:00:59,360
Check if failed.

13
00:01:04,209 --> 00:01:04,840
And.

14
00:01:07,240 --> 00:01:12,610
And lastly is we want to hash the user password.

15
00:01:13,570 --> 00:01:16,540
So these are the three steps that we are going to follow.

16
00:01:16,690 --> 00:01:20,170
So first is let's bring in the user model.

17
00:01:20,180 --> 00:01:23,380
So let's see, the auto import is working fine.

18
00:01:23,650 --> 00:01:26,350
If it doesn't work, let me show you some tricks.

19
00:01:26,380 --> 00:01:27,640
Go to the file.

20
00:01:27,670 --> 00:01:29,090
That is the model here.

21
00:01:29,110 --> 00:01:30,400
This is what we want.

22
00:01:30,550 --> 00:01:31,480
We save it.

23
00:01:31,480 --> 00:01:35,330
And now back to the file where you want to have the auto import.

24
00:01:35,350 --> 00:01:40,680
And by typing user, you can see that it is working like a magic.

25
00:01:40,690 --> 00:01:42,520
So here we go.

26
00:01:42,550 --> 00:01:50,080
So first of all, let's structure the properties that we want to create from the request body as cons

27
00:01:50,080 --> 00:01:53,350
equal to wreck dirt body.

28
00:01:53,950 --> 00:02:03,400
And inside here we need the foo name, the password, and then the email.

29
00:02:03,640 --> 00:02:07,930
So here let's check if user is found by email.

30
00:02:07,960 --> 00:02:19,060
So the const user found is equal to await the user model and you're going to use find one.

31
00:02:19,210 --> 00:02:26,110
And if find one we can find by any property for this one you want to find by email.

32
00:02:26,170 --> 00:02:30,810
So here, let's go ahead and through an error if a user is found.

33
00:02:30,820 --> 00:02:38,080
But because we have not tackled the error handling, we are going to send some message to the user by

34
00:02:38,080 --> 00:02:39,280
using rest.

35
00:02:39,580 --> 00:02:41,650
Jason So here we go.

36
00:02:41,740 --> 00:02:49,520
If user found, then we can say return from this function and then let's use rest.

37
00:02:49,750 --> 00:02:58,390
Jason And then we are going to say the message by saying that user already.

38
00:03:01,570 --> 00:03:02,440
Exists.

39
00:03:02,560 --> 00:03:03,760
So let's save it.

40
00:03:03,760 --> 00:03:07,080
And now let's work with the empty field.

41
00:03:07,090 --> 00:03:21,010
Well, so we are going to check if there is no email or there is no password or there is no full name

42
00:03:21,280 --> 00:03:24,760
and then return rest docs.

43
00:03:24,760 --> 00:03:27,850
Jason and then message.

44
00:03:27,850 --> 00:03:31,510
You are going to say that, please.

45
00:03:35,840 --> 00:03:39,200
Provide all fuel.

46
00:03:39,770 --> 00:03:47,420
Well, we can still use a third party package like Express Validator to do all these ones.

47
00:03:47,450 --> 00:03:49,580
But I just want to bring this one here.

48
00:03:49,580 --> 00:03:52,010
So it's is not going to be an optional, though.

49
00:03:52,130 --> 00:03:57,950
So after that, at this point, we can go ahead and then register the user.

50
00:03:57,980 --> 00:04:02,900
But you don't want to save the PIN password into the database.

51
00:04:02,900 --> 00:04:08,680
Because of that, we are going to use a third party package or be equipped chase.

52
00:04:08,690 --> 00:04:13,370
So let's get to the terminal and now let's install that package.

53
00:04:13,400 --> 00:04:17,570
Let's create a terminal on an MP and I.

54
00:04:26,520 --> 00:04:28,110
Ain't Jazz finished?

55
00:04:28,260 --> 00:04:30,180
Let's restart our server.

56
00:04:30,660 --> 00:04:32,430
Now the server is running.

57
00:04:32,460 --> 00:04:35,850
The next step is let's require the package.

58
00:04:35,970 --> 00:04:42,840
So if you have been taking my other policies, you can see that I always handle the hash or the password

59
00:04:42,840 --> 00:04:44,140
inside the module.

60
00:04:44,160 --> 00:04:48,030
But for this time around, I just want to do it inside the controller.

61
00:04:48,060 --> 00:04:49,180
So here we go.

62
00:04:49,200 --> 00:04:59,160
Can't be encrypt is equal to required the secret package and that is it be encrypt chips.

63
00:04:59,550 --> 00:05:00,510
That is it.

64
00:05:00,540 --> 00:05:03,600
So how are we going to hash the password?

65
00:05:03,600 --> 00:05:04,590
So here.

66
00:05:04,770 --> 00:05:15,870
First of all, we are going to create a salt from the package as a wait and decrypt dot gen sort and

67
00:05:15,870 --> 00:05:17,940
by convention we use ten.

68
00:05:18,570 --> 00:05:21,060
After that, we are going to hash the password.

69
00:05:21,060 --> 00:05:31,230
So here let's assign the hash password to this variable as hashed password, which is equal to our weight.

70
00:05:31,230 --> 00:05:37,210
And we bring in the big rate package on that we call something hash.

71
00:05:37,230 --> 00:05:43,890
So here I'm going to pass in the plane password and then the salt and that is it.

72
00:05:43,920 --> 00:05:48,530
So after that, we can go ahead and then create the user.

73
00:05:48,540 --> 00:05:50,640
So here we go.

74
00:05:50,820 --> 00:05:55,860
So let's comment here and say that creates user perfect.

75
00:05:55,860 --> 00:06:05,520
So const user is equal to our weight and then the user does not create.

76
00:06:05,970 --> 00:06:09,120
Then you pass in the properties that you want to create.

77
00:06:09,480 --> 00:06:16,290
The first one going to be the full name, the email, and for the password we are going to save the

78
00:06:16,290 --> 00:06:19,530
plane password as hash password.

79
00:06:19,710 --> 00:06:25,280
And if everything goes on, well, we are going to send the response to the user.

80
00:06:25,290 --> 00:06:35,400
So here, let's refactor this one a little bit and let's provide status and start and say success.

81
00:06:35,400 --> 00:06:37,790
You may need this one in the front end.

82
00:06:37,800 --> 00:06:40,210
So I will show you when we get there.

83
00:06:40,230 --> 00:06:49,950
And after that, let's also sign the foo name as user dot foo name and the id too.

84
00:06:49,980 --> 00:06:53,820
You may need this one also in the front end as user.

85
00:06:53,970 --> 00:06:55,000
That is it.

86
00:06:55,020 --> 00:07:00,040
So for the error handling, don't worry, we are going to do that one in this video.

87
00:07:00,060 --> 00:07:01,520
So moment of truth.

88
00:07:01,530 --> 00:07:06,890
But before we continue, we need to tell x player to pass the incoming data.

89
00:07:06,900 --> 00:07:09,570
So by default, exploit doesn't allow that.

90
00:07:09,570 --> 00:07:11,580
So let's go ahead and then do that one.

91
00:07:26,920 --> 00:07:29,460
So let's go ahead and then register a user.

92
00:07:29,470 --> 00:07:31,650
So this is the end point.

93
00:07:31,660 --> 00:07:34,690
So here I'm going to pass in a body.

94
00:07:51,130 --> 00:07:52,690
So let's hit send.

95
00:07:53,620 --> 00:08:01,630
And there we go we go starters 200 which is OC and we have the response great.

96
00:08:01,630 --> 00:08:07,060
But we are not sending the emails, so let's go ahead and send the email also.

97
00:08:07,060 --> 00:08:14,290
So here email is going to be the user dot e mail.

98
00:08:14,860 --> 00:08:15,550
Great.

99
00:08:15,550 --> 00:08:20,920
So let's go ahead and then register with the same email and let's see the message.

100
00:08:21,940 --> 00:08:24,130
And this is the message.

101
00:08:24,130 --> 00:08:27,790
You can see that we have titles, 200 million.

102
00:08:27,790 --> 00:08:28,510
Okay?

103
00:08:28,540 --> 00:08:31,510
That is why I say that this is not an error.

104
00:08:31,510 --> 00:08:35,890
So you're going to find a proper way to handle this kind of situations.

105
00:08:36,340 --> 00:08:43,809
So to check if a user is being saved, let's use the extension that is the MongoDB for visual STD code

106
00:08:43,960 --> 00:08:48,070
and let's click on the connection string and click on refresh.

107
00:08:50,900 --> 00:08:56,120
And this time around we have our database name as income expenses app.

108
00:08:56,210 --> 00:09:03,860
If we open it, you can see we have one collection called Uses and this what I was telling you that

109
00:09:03,860 --> 00:09:11,420
when you compile it is going to utilize this one and convert each one to lowercase as we see here.

110
00:09:11,540 --> 00:09:18,210
So if you open that, we have one document and inside the document this is what we have.

111
00:09:18,230 --> 00:09:24,240
If you open it, you can see this is a data being saved inside MongoDB.

112
00:09:24,260 --> 00:09:29,330
And for the password, we are saving this random string of characters.

113
00:09:29,330 --> 00:09:30,800
That is perfect.

114
00:09:31,100 --> 00:09:37,430
So in the next video, let's go ahead and implement the logic for logging in.

115
00:09:37,460 --> 00:09:40,010
Let's get into that in the next video.

