1
00:00:00,240 --> 00:00:06,180
Wonderful, and once we're done with a registered route now, let's worry about the logging road.

2
00:00:06,600 --> 00:00:08,670
And here are steps that I want you to take.

3
00:00:09,270 --> 00:00:10,860
So we already have the controller.

4
00:00:11,460 --> 00:00:13,380
We just need to worry about the functionality.

5
00:00:13,860 --> 00:00:20,310
First, we want to structure, email and password and check whether both of them exist.

6
00:00:20,740 --> 00:00:25,800
If one of them is missing, we return 400, then we'll try to find the user.

7
00:00:26,310 --> 00:00:29,670
Remember, on the user model, we have find one.

8
00:00:30,090 --> 00:00:33,810
Essentially, for all the models we have, but in this case, we're using user.

9
00:00:34,110 --> 00:00:35,430
So we'll use find one.

10
00:00:35,850 --> 00:00:42,650
We'll check whether there's such user with such e-mail if no return for 401.

11
00:00:43,110 --> 00:00:44,820
When we want to check the password.

12
00:00:45,210 --> 00:00:48,900
And remember that one is already in the user model.

13
00:00:49,050 --> 00:00:52,260
So we already added the method on the instance.

14
00:00:52,830 --> 00:01:00,630
And if everything is correct, then we attach cookie and then we send back the response the same as

15
00:01:00,630 --> 00:01:01,320
where the register.

16
00:01:01,530 --> 00:01:07,020
So let's try it out and then go back to the off controller.

17
00:01:07,350 --> 00:01:12,570
And like I said first, I wanted to structure the email and password that is coming in.

18
00:01:12,900 --> 00:01:15,510
And both of them are available in that body.

19
00:01:15,810 --> 00:01:16,410
So let's go.

20
00:01:16,420 --> 00:01:18,210
Here comes email.

21
00:01:18,890 --> 00:01:20,090
Comma password.

22
00:01:21,000 --> 00:01:23,190
Let's set it equal to that body.

23
00:01:23,550 --> 00:01:27,840
And once I have both of them, then I want to check one of them is missing.

24
00:01:27,900 --> 00:01:30,270
Then I'll send back bad request.

25
00:01:30,480 --> 00:01:31,410
So 400.

26
00:01:31,590 --> 00:01:34,350
So let's say here, if there is no email.

27
00:01:35,290 --> 00:01:41,950
Email or there is no password, then we have a bad request, so password.

28
00:01:42,520 --> 00:01:49,570
If either of them are missing, let's throw a new custom or not, and then we're looking for bad request

29
00:01:49,570 --> 00:01:51,970
and then say, please provide.

30
00:01:53,140 --> 00:01:56,050
Email and password, email and password.

31
00:01:56,710 --> 00:01:58,990
All right, we're done with our first check.

32
00:01:59,320 --> 00:02:02,180
Then we want to get the user correct.

33
00:02:02,230 --> 00:02:02,920
So let's go.

34
00:02:03,100 --> 00:02:05,750
Com's User is equal to a weight.

35
00:02:06,400 --> 00:02:07,210
It's a cinch.

36
00:02:07,810 --> 00:02:10,030
And then we'll look for user model.

37
00:02:10,360 --> 00:02:15,280
And the method we're looking for is find one in here or pass in the email.

38
00:02:15,950 --> 00:02:20,200
And now we want to check whether the user exists correct since we're logging in.

39
00:02:20,710 --> 00:02:25,330
So in order to create the user, the user first need to register.

40
00:02:25,780 --> 00:02:30,250
And if that's the case, then there should be a user of inside budget.

41
00:02:30,250 --> 00:02:31,810
So I get a little bit more realistic.

42
00:02:32,260 --> 00:02:35,860
And let's say if there is no user, then we want to be vague.

43
00:02:36,370 --> 00:02:39,370
We want to say, Hey, invalid credentials, something like that.

44
00:02:39,370 --> 00:02:41,190
So throw you now.

45
00:02:41,200 --> 00:02:45,610
In this case, we're throwing what we're throwing on authenticated error.

46
00:02:46,030 --> 00:02:48,010
So remember, that was 401.

47
00:02:48,330 --> 00:02:50,530
So unauthenticated error.

48
00:02:50,980 --> 00:02:57,550
And as far as message, I just say vague, invalid credentials.

49
00:02:57,700 --> 00:02:59,070
OK, awesome.

50
00:02:59,650 --> 00:03:02,860
Then we want to check whether the password is correct.

51
00:03:03,610 --> 00:03:11,650
And if you remember in the model user model, we already created the instant method compare password.

52
00:03:12,130 --> 00:03:15,010
And here we need to pass in the password that's coming in.

53
00:03:15,790 --> 00:03:21,250
I understand that candidate password and then we'll use B script to compare if it is a match.

54
00:03:21,490 --> 00:03:21,870
Awesome.

55
00:03:22,240 --> 00:03:24,910
If not, then we'll send back another error.

56
00:03:25,480 --> 00:03:28,320
So let's try here, counselor, and I'm going to go.

57
00:03:28,330 --> 00:03:29,770
It is password.

58
00:03:31,140 --> 00:03:32,250
Correct.

59
00:03:32,610 --> 00:03:36,990
And that is equal to your weight than we look for a user in this case.

60
00:03:37,290 --> 00:03:44,190
So I'm not looking for a model, for instance, already on there, I have a method by the name of compare

61
00:03:44,760 --> 00:03:45,490
password.

62
00:03:46,350 --> 00:03:51,540
Hopefully, that's how I named it in the model of images that we'll check because I'm an expert on messing

63
00:03:51,660 --> 00:03:51,840
up.

64
00:03:52,110 --> 00:03:54,720
So compare password and password.

65
00:03:54,730 --> 00:03:55,950
Yep, everything's correct.

66
00:03:56,310 --> 00:03:57,980
And I want to pass in the password.

67
00:03:59,000 --> 00:04:07,990
And by the way, I'll get back the response now if it is false, so I'm going to go with if is passport

68
00:04:08,000 --> 00:04:13,790
is incorrect, then we'll throw the same error or say invalid credentials.

69
00:04:14,180 --> 00:04:20,720
Now if everything is correct, but I know that user exists is there are already registered.

70
00:04:21,110 --> 00:04:27,200
So now I just want to create a token user and then attach my Kokesh to the response.

71
00:04:27,410 --> 00:04:32,930
More specifically, one cookie for now and then will send back the same response that we have over here.

72
00:04:33,260 --> 00:04:40,040
So essentially, what we can do is just take these three lines of code and copy and paste just so we

73
00:04:40,040 --> 00:04:41,240
can speed this up a little bit.

74
00:04:41,630 --> 00:04:42,830
So let's copy and paste.

75
00:04:43,220 --> 00:04:46,010
And now let's head to a post man on the street.

76
00:04:46,700 --> 00:04:50,360
So as far as the register, I definitely registered John.

77
00:04:50,810 --> 00:04:56,870
So let me go to log in years there, and this is going to be the case again where I copy and paste just

78
00:04:56,870 --> 00:04:58,490
so you don't have to watch me type.

79
00:04:58,910 --> 00:05:07,310
So log in user body run, raw JSON, copy and paste, and I'm not looking for a name.

80
00:05:07,640 --> 00:05:09,740
So we're just looking for email and password.

81
00:05:09,920 --> 00:05:15,890
And if everything is correct in response, I should get back to cookie and I do, and I can also see

82
00:05:15,890 --> 00:05:17,240
that John is adamant.

83
00:05:17,730 --> 00:05:24,080
And if I can go to my testing around the dummy route, the one where we're logging the sign cookies

84
00:05:24,080 --> 00:05:29,240
and if I send a should see back in the console that there is a token.

85
00:05:29,570 --> 00:05:34,810
And if your result exactly the same, you have successfully set up a log in wrote.

