1
00:00:06,840 --> 00:00:10,980
In this video, let's implement the log in functionality.

2
00:00:10,980 --> 00:00:18,720
So inside the user's controller and here we go, we are done with the registration, so let's collapse

3
00:00:18,720 --> 00:00:22,140
it and let's work with the logging functionality.

4
00:00:22,320 --> 00:00:25,620
So for the log in, let's look at the steps two.

5
00:00:25,650 --> 00:00:30,450
Step number one is you are going to check if the email exists.

6
00:00:30,450 --> 00:00:36,360
So here we go, check if email exists.

7
00:00:36,360 --> 00:00:41,820
And after checking we want to compare if the password is correct.

8
00:00:41,820 --> 00:00:44,280
So check for.

9
00:00:47,020 --> 00:00:48,190
Password.

10
00:00:50,560 --> 00:00:51,610
Validity.

11
00:00:52,570 --> 00:01:04,720
So here we go less the structure, the body object as rig the body and what we need is the email and

12
00:01:04,720 --> 00:01:06,460
then the password.

13
00:01:06,460 --> 00:01:08,590
So here we go.

14
00:01:09,040 --> 00:01:20,080
And when I be the same format as we did for the registration, so const user found is equal to await

15
00:01:20,080 --> 00:01:27,520
the user model to find one and you want to find by the email field.

16
00:01:27,910 --> 00:01:34,600
So if user found, it means that there is a user with this particular account.

17
00:01:34,630 --> 00:01:37,150
So here we are going to invert.

18
00:01:37,150 --> 00:01:44,560
Meaning if there is no user found, it means that this user is new or this user has to account with

19
00:01:44,560 --> 00:01:48,640
us so we can go ahead and then return from this function.

20
00:01:48,640 --> 00:01:56,350
And then you say That red dot, Jason, again, you find a proper way to handle error.

21
00:01:56,410 --> 00:01:59,890
So let's say the message is equal to this.

22
00:02:00,100 --> 00:02:03,130
For this one, don't try to be more descriptive.

23
00:02:03,130 --> 00:02:06,580
For example, if the email is not found, don't say that.

24
00:02:06,580 --> 00:02:09,850
Well, email is not found in that way.

25
00:02:09,850 --> 00:02:14,020
We are giving a hint if a hacker is trying to use your account.

26
00:02:14,380 --> 00:02:20,650
So provide a message that will not give more details about the response coming back if the password

27
00:02:20,650 --> 00:02:22,420
or email is incorrect.

28
00:02:22,600 --> 00:02:28,570
Most of times I like to use this invalid log in credentials.

29
00:02:29,470 --> 00:02:30,220
Perfect.

30
00:02:30,220 --> 00:02:33,640
So here after that, we're going to check for the password.

31
00:02:33,640 --> 00:02:41,080
Remember, we are saving random string of characters of the password so we cannot remember those password.

32
00:02:41,080 --> 00:02:45,280
So we are going to use the decrypt package to get that one done.

33
00:02:45,280 --> 00:02:47,410
So let's see how we're going to implement that.

34
00:02:47,500 --> 00:02:57,640
So the const is password match, meaning found match is equal to our weight.

35
00:02:58,090 --> 00:03:03,490
Then you bring in the big package and the method is called per.

36
00:03:03,610 --> 00:03:05,770
The name is even descriptive.

37
00:03:05,800 --> 00:03:13,570
We pass in the plane password and then the sip password in our DB and that one we can get it on the

38
00:03:13,570 --> 00:03:18,970
user found third password and this what is inside our database.

39
00:03:19,120 --> 00:03:24,640
So for this it's going to return true or false for this function or method.

40
00:03:24,670 --> 00:03:31,420
If it is true, it means that the password is correct and if it is false, it means that the password

41
00:03:31,420 --> 00:03:32,590
is incorrect.

42
00:03:32,740 --> 00:03:34,840
So here, let's check for that.

43
00:03:34,840 --> 00:03:42,700
Meaning, if there is no password match, then we can go ahead and then send the same message so we

44
00:03:42,700 --> 00:03:46,660
can copy this one and then paste it here.

45
00:03:46,750 --> 00:03:52,660
Let's have some spacing between these ones as that and as that.

46
00:03:52,660 --> 00:03:53,320
Perfect.

47
00:03:53,500 --> 00:03:56,950
So at this point, everything is correct.

48
00:03:56,950 --> 00:04:01,420
So now we can go ahead and then send the user details.

49
00:04:01,420 --> 00:04:08,440
So here rest that saying let's send more meaningful messages by providing starters.

50
00:04:12,590 --> 00:04:15,710
And equal to success.

51
00:04:15,770 --> 00:04:23,360
And then let's say the full name also you going to be as user found dot full name.

52
00:04:25,100 --> 00:04:31,580
And then let's also say the ID also is going to be user found dot.

53
00:04:32,530 --> 00:04:34,300
And as crow eyed.

54
00:04:34,330 --> 00:04:36,740
So moment of truth.

55
00:04:36,760 --> 00:04:39,160
Let's go ahead and then look in.

56
00:04:39,160 --> 00:04:41,290
So back to the ten lines.

57
00:04:41,320 --> 00:04:44,060
This is the end point for logging in.

58
00:04:44,080 --> 00:04:46,030
So here we go.

59
00:04:46,060 --> 00:04:48,610
Let's provide the payload.

60
00:04:48,700 --> 00:04:51,910
And the first one going to be the email.

61
00:04:52,060 --> 00:04:55,060
And for the email, we use John.

62
00:04:57,550 --> 00:05:00,610
John at gmail.com.

63
00:05:01,120 --> 00:05:07,870
And then the password is equal to one, two, three, four, five.

64
00:05:07,900 --> 00:05:08,950
Let's hit send.

65
00:05:09,820 --> 00:05:11,630
And there we go.

66
00:05:11,650 --> 00:05:14,710
But if I change the email, let's see.

67
00:05:14,920 --> 00:05:18,370
And you can see we have invalid login credentials.

68
00:05:18,850 --> 00:05:22,120
If the image is correct, but the password is incorrect.

69
00:05:22,240 --> 00:05:24,400
We got the same response.

70
00:05:24,400 --> 00:05:27,610
Meaning that everything is working fine.

71
00:05:27,760 --> 00:05:33,070
So guys, that's how we implement the login function and it's the API.

72
00:05:33,460 --> 00:05:40,330
What you have done is what you call the authentication, meaning allowing a user to log into our system.

73
00:05:40,420 --> 00:05:47,740
But remember, HTTP servers are stateless, meaning that as soon as we look in our application does

74
00:05:47,740 --> 00:05:50,710
not know or doesn't remember the log in user.

75
00:05:50,830 --> 00:05:58,390
That's why we need to find a way to make HTTP servers more stateful by introducing what is called token

76
00:05:58,390 --> 00:05:59,950
based authentication.

77
00:05:59,980 --> 00:06:06,630
So in the next video, you have a look at how we can generate a token for the log in user.

78
00:06:06,640 --> 00:06:13,660
So upon every request, the user will send that token to the roots with our we know that, okay, this

79
00:06:13,660 --> 00:06:15,560
particular user is logged in.

80
00:06:15,580 --> 00:06:18,130
Let's get into that in the next video.

