1 00:00:01,080 --> 00:00:05,390 It's a good practice to implement a slash me endpoint in 2 00:00:05,390 --> 00:00:06,770 any API. 3 00:00:06,770 --> 00:00:10,260 So, basically, an endpoint where a user can retrieve 4 00:00:10,260 --> 00:00:12,660 his own data, all right? 5 00:00:12,660 --> 00:00:15,423 And so, let's now, very quickly, implement that. 6 00:00:17,130 --> 00:00:20,240 And so basically it's gonna be something very similar to 7 00:00:20,240 --> 00:00:24,680 these updateme and deleteme endpoints that we 8 00:00:24,680 --> 00:00:26,543 already have, right? 9 00:00:27,600 --> 00:00:32,600 So, let's add export dot getme, all right? 10 00:00:34,130 --> 00:00:37,180 Now, we still actually want to use the getOne 11 00:00:37,180 --> 00:00:42,180 factory function, so basically, uh, 12 00:00:42,530 --> 00:00:44,360 this one here, right? 13 00:00:44,360 --> 00:00:46,400 Because otherwise it would be very, very 14 00:00:46,400 --> 00:00:48,690 similar code to this one. 15 00:00:48,690 --> 00:00:51,960 Now, the only problem with this is that getOne basically 16 00:00:51,960 --> 00:00:55,910 uses the ID coming from the parameter in order to get the 17 00:00:55,910 --> 00:00:57,380 requested document. 18 00:00:57,380 --> 00:01:00,950 But, what we want to do now is to basically get the document 19 00:01:00,950 --> 00:01:03,130 based on the current user ID, 20 00:01:03,130 --> 00:01:07,280 so the ID coming from the currently logged in user, okay? 21 00:01:07,280 --> 00:01:11,460 And so that way we don't have to pass in any ID as a URL 22 00:01:11,460 --> 00:01:13,460 parameter, right? 23 00:01:13,460 --> 00:01:17,690 So, how can we do that? Well, very simple. 24 00:01:17,690 --> 00:01:20,870 All we will do here is a very simple middleware, 25 00:01:20,870 --> 00:01:22,553 which is gonna go like this. 26 00:01:23,870 --> 00:01:28,243 So as always, request, response, next, 27 00:01:31,661 --> 00:01:36,100 and then all we're gonna do is to say request, 28 00:01:36,100 --> 00:01:39,570 dot params, dot ID, which remember, 29 00:01:39,570 --> 00:01:41,863 is what the getOne is going to use, 30 00:01:42,740 --> 00:01:47,740 and tell it equal to request dot user dot ID. 31 00:01:48,170 --> 00:01:49,003 And that's it! 32 00:01:51,910 --> 00:01:52,743 All right? 33 00:01:52,743 --> 00:01:55,020 And so we will then add this middleware here 34 00:01:55,020 --> 00:01:56,693 before calling getOne. 35 00:01:58,020 --> 00:02:02,600 So, let's implement that so user routes 36 00:02:02,600 --> 00:02:04,843 and I'm going to add it right here. 37 00:02:05,750 --> 00:02:10,683 So router dot get slash me, 38 00:02:12,260 --> 00:02:14,653 and now, of course, we need to be logged in. 39 00:02:15,640 --> 00:02:18,860 So protect, and this will then add the user to the 40 00:02:18,860 --> 00:02:22,180 current request, which you already know will then allow 41 00:02:22,180 --> 00:02:26,173 us to read the ID from that user, right? 42 00:02:27,130 --> 00:02:31,990 Then, the next step is to then basically put that user ID 43 00:02:31,990 --> 00:02:34,111 into the params dot ID. Okay? 44 00:02:34,111 --> 00:02:37,790 So basically faking that the ID is actually coming 45 00:02:37,790 --> 00:02:39,163 from the URL. 46 00:02:40,070 --> 00:02:43,793 So, user controller, uh, dot getme, 47 00:02:45,630 --> 00:02:49,600 and then finally user controller getuser. 48 00:02:53,800 --> 00:02:55,893 Okay? Make sense? 49 00:02:57,730 --> 00:03:00,670 Let's now very quickly test this out, 50 00:03:00,670 --> 00:03:03,803 and so it's gonna be a bit similar to this. 51 00:03:08,550 --> 00:03:09,863 But then, slash me. 52 00:03:10,810 --> 00:03:14,823 Of course we need authorization. Okay? 53 00:03:16,120 --> 00:03:21,120 And I'm also saving this in the users, 54 00:03:22,800 --> 00:03:27,800 and to get current user. 55 00:03:30,700 --> 00:03:34,510 Now, okay? Let's actually add it, these names 56 00:03:34,510 --> 00:03:38,310 because they look different so they have this data here 57 00:03:38,310 --> 00:03:39,143 for some reason. 58 00:03:43,540 --> 00:03:46,513 Okay, this one doesn't but still it's wrong here. 59 00:03:47,990 --> 00:03:50,770 All right? So let's now, just to make sure, 60 00:03:50,770 --> 00:03:53,780 log in as this admin. All right? 61 00:03:56,460 --> 00:03:59,870 And so now, get the data about this current user. 62 00:03:59,870 --> 00:04:02,593 So nothing in the body, nothing in the URL, 63 00:04:03,660 --> 00:04:07,150 but we still get the data about ourselves. 64 00:04:07,150 --> 00:04:11,080 So, name, email, role, and really all the data 65 00:04:11,080 --> 00:04:14,540 that there is about this user. Nice. 66 00:04:14,540 --> 00:04:18,870 So, again, you saw the great power of middleware. 67 00:04:18,870 --> 00:04:22,660 Where it was so easy to simply implement this getme 68 00:04:22,660 --> 00:04:25,850 middleware, so that then after that we can use our 69 00:04:25,850 --> 00:04:27,450 simple getuser. 70 00:04:27,450 --> 00:04:32,073 So, that one coming from the factory function, right? 71 00:04:33,140 --> 00:04:34,640 Okay, and that's it. 72 00:04:34,640 --> 00:04:37,533 That's all we had to do for this small task.