1
00:00:00,480 --> 00:00:03,750
In this video, we are going to create a murderer.

2
00:00:03,750 --> 00:00:08,970
And that murderer is going to check if indeed a user is logged in.

3
00:00:09,090 --> 00:00:15,450
If a user is logged in, we're going to have a property on the session as user of.

4
00:00:15,450 --> 00:00:17,840
So let's see how we are going to implement that.

5
00:00:17,850 --> 00:00:23,940
Inside the middleware, we are going to create one file and this one I'm going to name it as protected.

6
00:00:25,630 --> 00:00:26,920
Dot JS.

7
00:00:27,280 --> 00:00:30,130
Or you can make it as is locked in.

8
00:00:30,370 --> 00:00:32,740
All right, so let's see how we're going to create that one.

9
00:00:32,740 --> 00:00:37,720
So I'm going to create a function call and I'll call this one as protected.

10
00:00:38,970 --> 00:00:46,470
And is equal to every function, and I'm going to pass in the req response and then the next.

11
00:00:49,550 --> 00:00:53,150
So we are going to check if a user is log in.

12
00:00:53,150 --> 00:01:03,410
So here, check if the user is logged in and if a user is log in, we have access of the user on the

13
00:01:03,410 --> 00:01:04,790
record session.

14
00:01:04,790 --> 00:01:10,460
So here we go if we have a property on the rec dot session.

15
00:01:12,660 --> 00:01:14,100
Dart user of.

16
00:01:16,410 --> 00:01:21,150
Then it means that the user is locked in and I'm going to call next.

17
00:01:22,650 --> 00:01:29,360
Ere you are going to throw an error by saying that not allowed or permission denied.

18
00:01:29,370 --> 00:01:32,310
So let's require the app error.

19
00:01:33,750 --> 00:01:34,260
All right.

20
00:01:34,260 --> 00:01:35,040
It wouldn't work.

21
00:01:35,040 --> 00:01:38,400
So let me make sure that I received the file again.

22
00:01:38,400 --> 00:01:41,160
And here I'm going to make use of up error.

23
00:01:41,190 --> 00:01:49,350
Now I have the auto import, so I'm going to call next and I'll pass in the error.

24
00:01:49,350 --> 00:01:55,230
And the message I want to send is not authorized.

25
00:01:55,560 --> 00:01:57,810
Log in again.

26
00:01:58,260 --> 00:01:59,010
All right.

27
00:01:59,010 --> 00:02:03,960
So now I'm going to pass this one as a mirror to our route.

28
00:02:03,960 --> 00:02:08,910
So let me export this one as protected.

29
00:02:10,449 --> 00:02:16,930
So any route I want to check if a user is logged in, I'm going to pass this one as a middle word.

30
00:02:17,470 --> 00:02:20,800
So let me get back to the user route, which is this one.

31
00:02:20,800 --> 00:02:30,130
For the meantime, let's protect the profile, so let's require it here as context protected.

32
00:02:32,870 --> 00:02:36,680
Forced lash made worse and then protected.

33
00:02:37,010 --> 00:02:40,040
So let's protect the profile.

34
00:02:40,040 --> 00:02:45,530
So here I'm going to pass this as a second argument to this as protected.

35
00:02:45,920 --> 00:02:51,410
So now let's make a request to the profile endpoint and.

36
00:02:52,410 --> 00:02:55,920
Here is a profile end point, which is this one.

37
00:02:56,040 --> 00:02:59,880
So I'm going to send the request and go back.

38
00:02:59,880 --> 00:03:03,590
The response has not allowed me not authorize.

39
00:03:03,600 --> 00:03:05,100
Log in again.

40
00:03:05,280 --> 00:03:09,810
So let me go ahead and log in and let's see the effect.

41
00:03:10,020 --> 00:03:10,680
All right.

42
00:03:10,680 --> 00:03:14,610
So let me get back here and log in.

43
00:03:14,760 --> 00:03:15,390
Fine.

44
00:03:15,390 --> 00:03:16,280
I have log in.

45
00:03:16,290 --> 00:03:21,120
I have the user on the session and let me click on this one sent.

46
00:03:21,120 --> 00:03:26,430
And now you can see that you go back the response from the controller of the profile.

47
00:03:26,430 --> 00:03:30,000
For the meantime, we are sending the static text here.

48
00:03:30,210 --> 00:03:32,100
But let me show you one thing here.

49
00:03:32,370 --> 00:03:40,650
As soon as I restart the server, let me clear the terminal and then restart the server and let me hit

50
00:03:40,650 --> 00:03:42,870
the same endpoint for the profile.

51
00:03:43,880 --> 00:03:50,300
And you can see we go back not authorized, meaning that I'm not logged in even though I have logged

52
00:03:50,300 --> 00:03:50,750
in.

53
00:03:51,080 --> 00:03:58,490
The problem is that as soon as our server gets restarted, it loses the session, meaning that the session

54
00:03:58,490 --> 00:04:03,370
that has been generated, it is saving into the memory of our application.

55
00:04:03,380 --> 00:04:08,310
That's why it got lost upon every restarting of the server.

56
00:04:08,330 --> 00:04:10,700
So how are we going to process that?

57
00:04:10,820 --> 00:04:17,750
So to avoid this, we are going to save the session inside our database than saving into the memory

58
00:04:17,750 --> 00:04:19,029
of our application.

59
00:04:19,040 --> 00:04:22,640
So let's see how we are going to achieve that one in the next video.

