1
00:00:00,210 --> 00:00:08,510
Now that we have created our global era and land, let's see how we can make use of it inside our controllers.

2
00:00:08,520 --> 00:00:13,230
But for the meantime, let's see the problem that we are facing at the moment.

3
00:00:13,260 --> 00:00:20,700
Let's get back to the users controller and for this, if we register a user without full name, email

4
00:00:20,700 --> 00:00:28,260
or password, we are expecting a message that these fields are required and we want that message from

5
00:00:28,260 --> 00:00:31,340
the mongoose schema as we implemented.

6
00:00:31,350 --> 00:00:34,230
So let's see if we're going to get that message.

7
00:00:34,230 --> 00:00:40,470
So let's get back to the tender client and for the user registration, I'm going to register a user

8
00:00:40,470 --> 00:00:43,950
without payload or without full name or email.

9
00:00:43,950 --> 00:00:50,400
And let's see the response when I hit send, you can see that we got status code 200 million, everything

10
00:00:50,400 --> 00:00:54,120
is okay, and then you got empty response.

11
00:00:54,120 --> 00:00:56,040
So why is it so?

12
00:00:56,220 --> 00:01:03,690
The problem is that inside the user controller and that is inside the cache block, there is no message

13
00:01:03,690 --> 00:01:04,769
on the error.

14
00:01:04,769 --> 00:01:09,270
So what we got back from this one is going to be an empty object.

15
00:01:09,270 --> 00:01:11,160
So what is a problem?

16
00:01:11,160 --> 00:01:19,530
The problem is because of the big package for this operation, it takes some time before we can get

17
00:01:19,530 --> 00:01:21,750
back the hash password.

18
00:01:21,750 --> 00:01:28,680
So before getting back the response, the code inside the card blog has been called already.

19
00:01:28,680 --> 00:01:31,920
That's why we got the empty response.

20
00:01:31,920 --> 00:01:40,800
So let's try to comment this one out here and let's send the raw password for the meantime and let's

21
00:01:40,800 --> 00:01:44,340
save it and let's make the request to register and let's see.

22
00:01:44,850 --> 00:01:52,890
And here you can see that we go back the validation error from our mongoose and we go back these messages.

23
00:01:52,890 --> 00:01:55,380
So how are we going to prevent that?

24
00:01:55,380 --> 00:02:02,960
We can implement this logic inside the module, but we can also check if the full name or email field

25
00:02:03,000 --> 00:02:03,810
are empty.

26
00:02:03,810 --> 00:02:07,470
Then we can throw an error so we can check it here.

27
00:02:07,470 --> 00:02:14,070
So here we're going to be check if failed is empty.

28
00:02:15,930 --> 00:02:18,690
And going to be as if.

29
00:02:19,640 --> 00:02:29,390
There is no full name or there is no email or there is no password.

30
00:02:30,840 --> 00:02:39,200
Then you go ahead and return from this function by throwing an arrow, by passing our up arrow to next.

31
00:02:39,210 --> 00:02:42,590
And here we can pass in the message.

32
00:02:42,600 --> 00:02:48,660
So here we can say that all fails required.

33
00:02:49,290 --> 00:02:55,410
Let's bring back the script operation and let's send the hash password.

34
00:02:55,560 --> 00:02:56,790
So.

35
00:02:58,660 --> 00:03:01,660
So with this one, let's make the request again.

36
00:03:02,260 --> 00:03:06,910
And you can see we got the message that all files are required.

37
00:03:07,480 --> 00:03:07,960
All right.

38
00:03:07,960 --> 00:03:11,590
Let's also check inside the log in controller.

39
00:03:11,920 --> 00:03:19,210
But before I continue, I want to trust my database because I have a record which plane?

40
00:03:19,210 --> 00:03:23,200
Password to be able to delete your database.

41
00:03:23,200 --> 00:03:28,600
Right click and click on drop database and type the name of the database.

42
00:03:28,600 --> 00:03:31,960
Mine is full stack that block.

43
00:03:34,490 --> 00:03:35,720
And hit enter.

44
00:03:36,200 --> 00:03:42,100
And you can see we got a message that this operation is not allowed by this user.

45
00:03:42,110 --> 00:03:46,530
So unless we give a user this right to be able to delete.

46
00:03:46,640 --> 00:03:51,500
So back to our dashboard, click on database access.

47
00:03:52,670 --> 00:03:55,070
And then click on edit.

48
00:03:55,400 --> 00:03:58,880
And down here we have the built in room.

49
00:03:59,240 --> 00:04:06,120
Click on this dropdown and here select at last admin and click on update.

50
00:04:06,140 --> 00:04:08,660
So now we can delete the database.

51
00:04:08,660 --> 00:04:18,200
So right click and click on Drop database and the name of the DB has full stack dash block and hit enter.

52
00:04:18,589 --> 00:04:21,500
And now you can see that database has been dropped.

53
00:04:21,500 --> 00:04:29,810
So now let's go ahead and register a user with write details and let's see the flow again.

54
00:04:29,960 --> 00:04:33,530
So here I'm going to provide the full name.

55
00:04:39,010 --> 00:04:41,170
And I'm going to make use of John.

56
00:04:42,350 --> 00:04:48,050
And then the email John asks gmail.com.

57
00:04:49,490 --> 00:04:51,590
And then the password.

58
00:04:53,280 --> 00:04:54,870
One, two, three, four, five.

59
00:04:54,870 --> 00:04:56,010
And hit send.

60
00:04:56,930 --> 00:04:59,480
And you can see we go back to the user.

61
00:04:59,600 --> 00:05:02,990
So let's go ahead and log in with this details.

62
00:05:02,990 --> 00:05:09,770
So inside the log in, I have the email as drawn and the password as one, two, three, four, five.

63
00:05:09,800 --> 00:05:16,130
Let me hit send and you go back the response meaning that everything is okay.

64
00:05:16,220 --> 00:05:25,070
But if I change the email and let's see you go back the response as invalid login credentials where

65
00:05:25,070 --> 00:05:29,540
you go start is called 200, meaning everything is okay, but it is not.

66
00:05:29,540 --> 00:05:36,110
So let's send the right message and this is where we are sending the message.

67
00:05:36,110 --> 00:05:43,640
So instead of using Jason to send a message, we are going to connect the past message into our app

68
00:05:43,700 --> 00:05:44,450
error handler.

69
00:05:44,450 --> 00:05:47,240
So here I'm going to pass next here.

70
00:05:48,200 --> 00:05:56,050
So here I'm going to remove the rest of Jason and I'm going to call next and I'm going to pass the app

71
00:05:56,060 --> 00:05:56,630
error.

72
00:05:56,630 --> 00:06:02,870
And then the message I want to send, I'm going to be as invalid log in credentials.

73
00:06:05,450 --> 00:06:13,570
So with this one, if I log in again, you can see we go by the status code 500 and then you go back

74
00:06:13,580 --> 00:06:15,620
the message that we want.

75
00:06:15,710 --> 00:06:22,250
So inside the log into that, it's down here and I want to send the exact message.

76
00:06:22,250 --> 00:06:28,610
So I'm going to copy this one and move this one from here and send the same message.

77
00:06:29,960 --> 00:06:35,690
But in case a user refuses to provide whether email or password, let's also send the message.

78
00:06:35,690 --> 00:06:44,000
So here we are going to check if there is no email or there is no password field.

79
00:06:44,360 --> 00:06:55,670
We will say return and then call it next, then pass in the error and we say that email and password

80
00:06:55,670 --> 00:06:56,450
field.

81
00:07:00,230 --> 00:07:01,670
Are required.

82
00:07:03,310 --> 00:07:03,820
Great.

83
00:07:03,820 --> 00:07:12,190
So now let's look in with empty payload and let's hit send and we can see you go back the message as

84
00:07:12,190 --> 00:07:15,430
email and password failures are required.

85
00:07:15,430 --> 00:07:24,610
So let me add s to this one and start and inside the server file I made some typing mistake here, so

86
00:07:24,610 --> 00:07:29,080
let me remove the one v that is server is up and running.

87
00:07:29,110 --> 00:07:29,680
All right.

88
00:07:29,680 --> 00:07:36,370
So guy, this is how we can throw any error inside our application now that we have this one down the

89
00:07:36,370 --> 00:07:42,670
next video, let's go ahead and implement the authorization by making use of expert session.

