All right.
And once we have the validation in place now, let's also take care of the admin role.
And first, I just want to mention that, of course, you can always, always go back to the database
and update that value manually.
So of course at the moment I have only one user, but in general you can find the specific user.
I notice I can simply go here and I can say admin.
And the moment I do that now of course this user has all of the admin privileges.
So that's definitely one way how to do that.
But we will set up this functionality programmatically and essentially my approach is going to be following.
If it's the first account.
So if it's the first user, then automatically this user is going to be an admin and the rest of them,
they're going to be users.
So let's see how we can do that.
First we want to go to controller.
We're still looking for the register and what's really awesome on the model, we have this count documents
method which essentially does what it says.
It returns how many documents we have and it's a synchronous.
So that's why we want to go with Await and effectively I'm just going to say, hey listen, if that
value is equal to zero, that means it's the first account, it's the first user.
And if that's the case, the role is going to be an admin one.
So first let's come up with some kind of variable.
In my case I'm going to go with is first.
Account or user however you want to.
Approach this.
Then we go with model.
Then, like I said, count documents.
It's something that we can invoke.
It's a synchronous and I'm going to set it equal to zero.
So if it is equal to zero, of course that's the first account.
And then remember, all of the values are where they are in the body.
And I'm passing this req.body directly in the user dot create.
Correct.
And what that means, I can simply add the property on the req.body I can say, you know what, there's
going to be a role property it's going to be equal to is first account and we'll have the ternary operator
and I'll say if the first account is true, then the role is going to be an admin.
If not, it's always, always going to be a user one.
And check it out the moment I save.
First of all, let's navigate back to register.
And yes, you know what?
Let me navigate over here.
Let's remove everything.
Let's go with users drop.
Then let's send it.
And now you'll see that the role is admin.
But if I go with Peter and I'll right away showcase that.
Even if I go here with the role.
And admin.
Since I have the logic in place, it's always going to be a user.
Unless of course I go to a database manually, so check it out.
Even though I was trying to force the admin one.
Nope.
Since it's not the first account automatically the role is equal to a user and with this in place we
can move on to hashing passwords functionality.