1
00:00:00,240 --> 00:00:06,290
We are continuing to implement what is called authentication and authorization.

2
00:00:06,300 --> 00:00:13,440
When we say authentication is a process of allowing a user to log in into an application where they

3
00:00:13,440 --> 00:00:17,940
write, log in details, and that is what we have done so far.

4
00:00:18,090 --> 00:00:21,450
A user can log in with email and a password.

5
00:00:21,450 --> 00:00:23,670
So what about authorization?

6
00:00:23,700 --> 00:00:31,110
Or authorization simply means that allowing a user to take any action after logging in and.

7
00:00:31,110 --> 00:00:38,910
And for us, HTTP servers are stateless, meaning that as soon as a user log in our server does not

8
00:00:38,910 --> 00:00:41,390
remember the log in user.

9
00:00:41,400 --> 00:00:45,000
So how can we make HTTP servers stateful?

10
00:00:45,030 --> 00:00:48,400
And we can achieve this one in a couple of ways.

11
00:00:48,420 --> 00:00:56,340
One, we can use what is called cookie based authentication or session based authentication or token

12
00:00:56,340 --> 00:00:59,790
based authentication using JWT.

13
00:01:00,000 --> 00:01:06,960
All these forms of authentication, we have covered them in details and the authentication and authorization

14
00:01:06,960 --> 00:01:08,850
in this particular course.

15
00:01:08,850 --> 00:01:11,610
So we are going to make use of session.

16
00:01:11,610 --> 00:01:13,170
So here is a flow.

17
00:01:13,200 --> 00:01:20,700
As soon as a user log in, our server is going to generate a session or a token input in that way and

18
00:01:20,700 --> 00:01:22,650
then assign to the user.

19
00:01:22,680 --> 00:01:30,210
So upon every request a user will take, our server is going to check if that user is having the right

20
00:01:30,210 --> 00:01:30,900
session.

21
00:01:30,900 --> 00:01:34,950
If the user have the right session or token, then put it that way.

22
00:01:34,980 --> 00:01:38,850
Then our server will allow the user to take any action.

23
00:01:38,850 --> 00:01:41,070
So how are you going to make use of it?

24
00:01:41,130 --> 00:01:45,870
So we are going to install a package called expert session.

25
00:01:46,050 --> 00:01:51,330
Let me shut down the server and let me clear the terminal and make use of NPM.

26
00:01:51,330 --> 00:01:56,490
I express dash session and hit enter.

27
00:01:59,290 --> 00:01:59,950
Perfect.

28
00:01:59,950 --> 00:02:03,820
So let me clear the terminal and restart the server.

29
00:02:04,330 --> 00:02:09,460
We are going to configure it inside the server case file.

30
00:02:09,490 --> 00:02:17,170
So I'm going to require the package as session is equal to require.

31
00:02:19,590 --> 00:02:21,300
Express session.

32
00:02:22,200 --> 00:02:22,800
So.

33
00:02:22,800 --> 00:02:24,360
And the murderer's here.

34
00:02:24,390 --> 00:02:28,650
I'm going to add what is called session configuration.

35
00:02:31,440 --> 00:02:33,000
And here you go.

36
00:02:33,270 --> 00:02:34,890
We make use of app.

37
00:02:36,510 --> 00:02:44,220
Dirty use and I'm going to make use of session and as a function core with some configuration.

38
00:02:44,220 --> 00:02:51,570
And as that's the first property, I'm going to be the secret and I'm going to use any random text here

39
00:02:51,840 --> 00:02:55,910
and I can put this one inside environment variable.

40
00:02:55,920 --> 00:02:57,990
So let me go ahead and do that.

41
00:02:57,990 --> 00:03:04,350
So inside the DMV, I'm going to add a variable called Search searching key.

42
00:03:06,960 --> 00:03:10,440
And I'm going to use my key.

43
00:03:10,890 --> 00:03:13,590
You can use a nice string of characters here.

44
00:03:14,010 --> 00:03:18,320
So inside the session, I'm going to make use of this environment variable.

45
00:03:18,330 --> 00:03:27,090
So I'm going to remove this one and make use of process dot and v dot the variable name as searching

46
00:03:27,090 --> 00:03:27,690
key.

47
00:03:28,020 --> 00:03:32,820
The next property is going to be as we save and put it to force.

48
00:03:33,060 --> 00:03:40,620
So this are just a configuration and you can find all this one inside the package itself and save or

49
00:03:40,650 --> 00:03:42,720
initialize also to true.

50
00:03:43,900 --> 00:03:45,320
And that is it.

51
00:03:45,340 --> 00:03:52,520
So with this one, we have accessed what is called record session in any of our routes.

52
00:03:52,540 --> 00:03:59,320
So let's go ahead and then console, look, record session inside the user's controller.

53
00:03:59,320 --> 00:04:07,270
So let's go back to the log in here and here I'm going to console log what is called rec dot session.

54
00:04:09,040 --> 00:04:10,390
And let's see.

55
00:04:10,510 --> 00:04:16,630
Let's get back to the tender clients and then click on log in and let me check the terminal.

56
00:04:18,329 --> 00:04:26,870
And you can see we have a property code section and inside we have cookie and then some properties inside

57
00:04:26,880 --> 00:04:27,330
that.

58
00:04:27,390 --> 00:04:33,390
So with this one, we can add the log in user into the session.

59
00:04:33,390 --> 00:04:38,820
So with this we can save the logging user into session.

60
00:04:38,820 --> 00:04:42,420
So let's see how we can add properties to the session.

61
00:04:42,420 --> 00:04:45,390
So let's go back to the controller.

62
00:04:45,390 --> 00:04:53,460
That's for the user and I'm going to add a property called log in user and it's equal to four.

63
00:04:53,460 --> 00:04:56,010
Now let me say IMA.

64
00:04:57,730 --> 00:05:00,040
So let's make the request and let's see.

65
00:05:00,070 --> 00:05:07,870
Let me check the terminal and you can see that we got what is called Imma inside the console.

66
00:05:07,900 --> 00:05:12,850
So with this one we can add the locking user into that.

67
00:05:13,450 --> 00:05:18,760
So after logging in, we want to save the logging user into session.

68
00:05:19,180 --> 00:05:26,440
At this point the user is log in so you can see that save the user into session.

69
00:05:27,520 --> 00:05:28,540
Rick.

70
00:05:29,210 --> 00:05:38,090
Dot session and I'm going to add a property called user found or user of.

71
00:05:39,960 --> 00:05:48,600
Ane is equal to the user found here after finding the user by email, which is this.

72
00:05:48,900 --> 00:05:55,710
So now anywhere in our route or controller we have access to the log in user.

73
00:05:55,740 --> 00:06:04,480
So after that, let's go ahead and console that, log the rec session and going to see the log in user.

74
00:06:04,500 --> 00:06:05,880
So here we go.

75
00:06:06,030 --> 00:06:11,340
So let's provide the data to log in email.

76
00:06:13,160 --> 00:06:20,960
Is going to be John at gmail.com and then password.

77
00:06:22,790 --> 00:06:24,380
One, two, three, four, five.

78
00:06:24,380 --> 00:06:25,430
And hit send.

79
00:06:26,210 --> 00:06:34,670
Now let's check the terminal and you can see we have a property called user off and this contains the

80
00:06:34,670 --> 00:06:36,080
log in user.

81
00:06:36,110 --> 00:06:40,030
And for this we can send the data that you want to save.

82
00:06:40,040 --> 00:06:46,280
You can see that way for me, I want to save all the ID of the user and that is what I want.

83
00:06:46,280 --> 00:06:49,340
I want to save the ID of the user.

84
00:06:49,340 --> 00:06:50,870
So here we go.

85
00:06:50,900 --> 00:06:57,590
Inside here I'm going to make this of user found dot underscore ID.

86
00:06:57,650 --> 00:07:01,790
So with this one, let's look in and check our terminal.

87
00:07:02,800 --> 00:07:08,380
And you can see we have the user of the ID, all right.

88
00:07:08,380 --> 00:07:14,470
Now that we can save the user into session, what we are going to do is that we are going to create

89
00:07:14,470 --> 00:07:20,380
a murderer and that murderer is going to check if indeed a user is logged in.

90
00:07:20,500 --> 00:07:24,730
If a user is logged in, you're going to have this property called user off.

91
00:07:24,730 --> 00:07:28,420
So let's see how we can do about that one in the next video.

