1
00:00:00,170 --> 00:00:00,840
All right.

2
00:00:00,890 --> 00:00:05,960
And once we have the validation in place now, let's also take care of the admin role.

3
00:00:05,990 --> 00:00:12,530
And first, I just want to mention that, of course, you can always, always go back to the database

4
00:00:12,710 --> 00:00:15,080
and update that value manually.

5
00:00:15,170 --> 00:00:20,930
So of course at the moment I have only one user, but in general you can find the specific user.

6
00:00:21,670 --> 00:00:26,080
I notice I can simply go here and I can say admin.

7
00:00:26,080 --> 00:00:33,880
And the moment I do that now of course this user has all of the admin privileges.

8
00:00:33,880 --> 00:00:36,670
So that's definitely one way how to do that.

9
00:00:36,670 --> 00:00:43,690
But we will set up this functionality programmatically and essentially my approach is going to be following.

10
00:00:44,250 --> 00:00:46,190
If it's the first account.

11
00:00:46,200 --> 00:00:54,420
So if it's the first user, then automatically this user is going to be an admin and the rest of them,

12
00:00:54,420 --> 00:00:56,340
they're going to be users.

13
00:00:56,340 --> 00:00:58,170
So let's see how we can do that.

14
00:00:58,170 --> 00:01:01,470
First we want to go to controller.

15
00:01:01,860 --> 00:01:08,370
We're still looking for the register and what's really awesome on the model, we have this count documents

16
00:01:08,370 --> 00:01:12,630
method which essentially does what it says.

17
00:01:12,630 --> 00:01:17,370
It returns how many documents we have and it's a synchronous.

18
00:01:17,370 --> 00:01:21,900
So that's why we want to go with Await and effectively I'm just going to say, hey listen, if that

19
00:01:21,900 --> 00:01:27,030
value is equal to zero, that means it's the first account, it's the first user.

20
00:01:27,030 --> 00:01:30,870
And if that's the case, the role is going to be an admin one.

21
00:01:30,870 --> 00:01:33,750
So first let's come up with some kind of variable.

22
00:01:33,780 --> 00:01:36,450
In my case I'm going to go with is first.

23
00:01:37,150 --> 00:01:39,850
Account or user however you want to.

24
00:01:40,820 --> 00:01:41,710
Approach this.

25
00:01:41,720 --> 00:01:43,070
Then we go with model.

26
00:01:43,070 --> 00:01:45,080
Then, like I said, count documents.

27
00:01:45,260 --> 00:01:47,480
It's something that we can invoke.

28
00:01:47,510 --> 00:01:51,050
It's a synchronous and I'm going to set it equal to zero.

29
00:01:51,050 --> 00:01:55,370
So if it is equal to zero, of course that's the first account.

30
00:01:55,370 --> 00:01:59,480
And then remember, all of the values are where they are in the body.

31
00:01:59,600 --> 00:02:04,550
And I'm passing this req.body directly in the user dot create.

32
00:02:04,580 --> 00:02:05,090
Correct.

33
00:02:05,090 --> 00:02:10,430
And what that means, I can simply add the property on the req.body I can say, you know what, there's

34
00:02:10,430 --> 00:02:17,570
going to be a role property it's going to be equal to is first account and we'll have the ternary operator

35
00:02:17,570 --> 00:02:22,430
and I'll say if the first account is true, then the role is going to be an admin.

36
00:02:22,430 --> 00:02:26,840
If not, it's always, always going to be a user one.

37
00:02:27,600 --> 00:02:29,480
And check it out the moment I save.

38
00:02:29,490 --> 00:02:32,310
First of all, let's navigate back to register.

39
00:02:32,310 --> 00:02:34,140
And yes, you know what?

40
00:02:34,140 --> 00:02:35,760
Let me navigate over here.

41
00:02:36,630 --> 00:02:37,980
Let's remove everything.

42
00:02:38,890 --> 00:02:41,350
Let's go with users drop.

43
00:02:42,070 --> 00:02:43,510
Then let's send it.

44
00:02:43,510 --> 00:02:46,330
And now you'll see that the role is admin.

45
00:02:46,330 --> 00:02:50,770
But if I go with Peter and I'll right away showcase that.

46
00:02:51,600 --> 00:02:54,540
Even if I go here with the role.

47
00:02:55,780 --> 00:02:57,010
And admin.

48
00:02:57,010 --> 00:03:01,660
Since I have the logic in place, it's always going to be a user.

49
00:03:01,990 --> 00:03:06,250
Unless of course I go to a database manually, so check it out.

50
00:03:06,250 --> 00:03:09,070
Even though I was trying to force the admin one.

51
00:03:09,160 --> 00:03:09,850
Nope.

52
00:03:10,210 --> 00:03:17,860
Since it's not the first account automatically the role is equal to a user and with this in place we

53
00:03:17,860 --> 00:03:21,130
can move on to hashing passwords functionality.

