1
00:00:00,050 --> 00:00:00,380
All right.

2
00:00:00,380 --> 00:00:05,780
And up next, let's set up, get current user root and effectively the functionality is going to be

3
00:00:05,780 --> 00:00:06,530
following.

4
00:00:06,530 --> 00:00:09,410
We're going to use user model.

5
00:00:09,440 --> 00:00:11,120
We'll use find one.

6
00:00:11,120 --> 00:00:14,670
And based on the ID, we're going to grab the user.

7
00:00:14,690 --> 00:00:20,090
Now, of course, you can also search right away based on the ID, but I just want to showcase that

8
00:00:20,090 --> 00:00:24,860
of course we have multiple options, so let's try it out.

9
00:00:24,890 --> 00:00:29,960
There's going to be one gotcha and we'll work on it once we have this functionality in place.

10
00:00:29,960 --> 00:00:33,920
So back in the controllers, pretty straightforward.

11
00:00:33,920 --> 00:00:42,230
In the user controller, we just want to look for a user whose ID matches to the one we have in the

12
00:00:42,230 --> 00:00:43,850
req dot user user ID.

13
00:00:44,510 --> 00:00:46,760
So I'm going to call this user.

14
00:00:47,060 --> 00:00:48,500
We're going to go with Await.

15
00:00:48,530 --> 00:00:51,140
We already have the user model.

16
00:00:51,940 --> 00:00:54,700
Then let's go with find one.

17
00:00:54,700 --> 00:01:01,180
Like I said, we can also use find by ID, but I just want to showcase that we have multiple options.

18
00:01:01,180 --> 00:01:04,510
And basically in here I'll say, get me the user.

19
00:01:05,330 --> 00:01:05,990
Whose ID.

20
00:01:06,170 --> 00:01:13,370
And remember, when Mongo creates the ID, the property is following its underscore ID and set it equal

21
00:01:13,370 --> 00:01:15,500
to user user ID.

22
00:01:15,650 --> 00:01:17,450
Now, how do I know that it's there?

23
00:01:17,480 --> 00:01:24,680
Well, because remember, we placed authenticate middleware in front of it and now let's send back user.

24
00:01:26,300 --> 00:01:28,400
And for now, let's set it equal to a user.

25
00:01:28,400 --> 00:01:30,170
And this is where you'll see the gotcha.

26
00:01:30,200 --> 00:01:34,340
So now let's navigate back to the user routes.

27
00:01:34,640 --> 00:01:36,350
We're looking for current user.

28
00:01:36,380 --> 00:01:37,250
Let's send it.

29
00:01:37,250 --> 00:01:41,690
And if everything is correct, I should get info about the job.

30
00:01:42,020 --> 00:01:49,250
Now, if I'm going to go to a login and if I'll change it around and if I'll say, Peter, what do you

31
00:01:49,250 --> 00:01:51,380
know in the get user?

32
00:01:51,620 --> 00:01:54,050
Of course I'll get the Peter value.

33
00:01:54,080 --> 00:02:00,110
Now there's a tiny typo over here as far as the name, but we'll actually fix that once we start working

34
00:02:00,110 --> 00:02:01,580
with update user.

35
00:02:01,580 --> 00:02:03,830
So pretty much this will always.

36
00:02:04,650 --> 00:02:06,770
Return, which is the current user.

37
00:02:06,780 --> 00:02:13,830
Again, something very important because we're not storing that user value anywhere in the front end

38
00:02:14,010 --> 00:02:16,050
as far as when we log in.

39
00:02:16,050 --> 00:02:21,960
So that is just stored in a cookie and then JWT, hopefully that is clear.

40
00:02:21,960 --> 00:02:25,560
And now let's also fix the gotcha.

41
00:02:25,590 --> 00:02:28,290
Notice how we're sending back the password.

42
00:02:28,470 --> 00:02:34,380
And even though, yes, it's hashed, we still don't want to send back the password.

43
00:02:34,860 --> 00:02:39,090
And effectively we have multiple ways how we can fix this.

44
00:02:39,240 --> 00:02:42,120
And I'll show you a instance.

45
00:02:42,120 --> 00:02:43,590
Method approach.

46
00:02:43,590 --> 00:02:45,020
Now what am I talking about?

47
00:02:45,030 --> 00:02:48,690
Well, I'm going to go to models, more specifically user model.

48
00:02:48,690 --> 00:02:53,460
And then right after my schema I can go here with user schema.

49
00:02:53,460 --> 00:02:58,740
So the same one we created then methods and then I need to come up with a name.

50
00:02:58,740 --> 00:03:04,120
So effectively when I get back, my user, for example, in here.

51
00:03:04,120 --> 00:03:09,940
But just please keep in mind, of course it works in any controller where you're getting back that instance.

52
00:03:09,940 --> 00:03:13,150
On that instance, I'm creating a method.

53
00:03:13,180 --> 00:03:13,540
Now.

54
00:03:13,540 --> 00:03:19,240
There are also static methods which are available on the entire model, but this is not it.

55
00:03:19,240 --> 00:03:23,170
So again, we're talking about the instance method and we need to come up with a name.

56
00:03:23,170 --> 00:03:25,750
So in my case I'm going to go to Json.

57
00:03:25,780 --> 00:03:29,710
Now you do want to go with good old function keyword.

58
00:03:31,010 --> 00:03:33,800
Because inside of the function we'll use this.

59
00:03:33,800 --> 00:03:37,340
So if you don't use this, then technically it doesn't matter.

60
00:03:37,340 --> 00:03:39,620
You can go with arrow function syntax as well.

61
00:03:39,620 --> 00:03:46,520
But if you're using this, which is going to point back to the instance, then yes, I strongly suggest

62
00:03:46,520 --> 00:03:48,740
using the good old function keyword.

63
00:03:48,740 --> 00:03:51,050
Otherwise the functionality is not going to work.

64
00:03:51,050 --> 00:03:52,670
So what do we want to do over here?

65
00:03:52,670 --> 00:03:57,620
Well, first I'm just going to go with let an object and I'll set it equal to this.

66
00:03:57,620 --> 00:04:03,170
So again, I'm talking about the instance, the user instance that we're getting back, and we're just

67
00:04:03,170 --> 00:04:05,180
going to go with two objects.

68
00:04:05,180 --> 00:04:10,160
So we will transform our user to the good old JavaScript object.

69
00:04:10,190 --> 00:04:17,329
Then we'll use the delete keyword and I simply want to remove the password property.

70
00:04:17,329 --> 00:04:22,550
And at the very end of the day I just want to go with return object.

71
00:04:22,550 --> 00:04:27,430
And essentially once I run it, I'll get back the user without the password.

72
00:04:27,440 --> 00:04:36,800
So let's navigate to user controller and essentially right after we grab the user based on data, let's

73
00:04:36,800 --> 00:04:38,750
come up with a variable.

74
00:04:38,750 --> 00:04:40,550
In my case, I'm going to go with user.

75
00:04:41,250 --> 00:04:44,910
Without password and we'll set it equal to user.

76
00:04:44,910 --> 00:04:52,200
And since we just created that instance method we want to go with to Json and we want to invoke it.

77
00:04:52,200 --> 00:04:59,160
And now instead of sending back the user, we'll set it equal to user without password.

78
00:04:59,160 --> 00:05:06,510
And if everything is correct when it comes to a current user, I'm going to get back the user without

79
00:05:06,510 --> 00:05:08,370
the password property.

