0
1
00:00:00,420 --> 00:00:07,530
So in the last lesson, we learned about using the Firebase Authentication Module to register our very
1

2
00:00:07,530 --> 00:00:09,130
first user,
2

3
00:00:09,270 --> 00:00:12,610
this 1@2.com person.
3

4
00:00:12,620 --> 00:00:16,580
Now, we're going to look at the login routes.
4

5
00:00:16,740 --> 00:00:22,830
So in this case, it's this screen right here and we're going to be doing pretty much the same thing
5

6
00:00:22,830 --> 00:00:24,990
as what we did in the register screen.
6

7
00:00:25,020 --> 00:00:30,510
We're going to take the email and password that they've typed in, and in the moment, when they press on
7

8
00:00:30,510 --> 00:00:37,260
the login button, we're going to take their details through to Firebase. And we're going to be using
8

9
00:00:37,350 --> 00:00:44,760
a login method to check that email and password against what we've got stored in our fFrebase Authentication
9

10
00:00:44,760 --> 00:00:45,960
database.
10

11
00:00:45,960 --> 00:00:52,270
And if that was successful, then we're again going to take them straight to the Chart View Controller.
11

12
00:00:52,290 --> 00:00:58,560
So this is the case where after the user registers for the first time, the next time, they use our service
12

13
00:00:58,590 --> 00:01:02,250
they're always going to be accessing it through the login route.
13

14
00:01:02,850 --> 00:01:05,110
So let's go ahead and tackle this.
14

15
00:01:05,430 --> 00:01:12,930
If we head back to the documentation and we go to authentication, iOS password authentication, we can
15

16
00:01:12,990 --> 00:01:18,990
skip all the parts that we've already done and head over to the sign in user with an email address
16

17
00:01:19,080 --> 00:01:20,840
and password section.
17

18
00:01:20,910 --> 00:01:23,690
We've already done step one and step two.
18

19
00:01:23,760 --> 00:01:26,900
We now have to move on to step three.
19

20
00:01:26,970 --> 00:01:31,470
So the method is called signInWithEmail and password.
20

21
00:01:31,470 --> 00:01:34,650
And, again, it takes two arguments or inputs.
21

22
00:01:34,650 --> 00:01:40,360
One is the e-mail that they've registered with and another is the password that they've registered with.
22

23
00:01:40,530 --> 00:01:46,920
And once this closure completes, we end up with an authentication result and an error
23

24
00:01:46,920 --> 00:01:47,810
if there was one.
24

25
00:01:48,380 --> 00:01:57,210
So the user's account data will live in this authResult object that we get back and we can use it inside
25

26
00:01:57,210 --> 00:02:01,790
this closure block or we can check to see if there were any errors.
26

27
00:02:01,830 --> 00:02:07,050
Again, I'm gonna go ahead and copy this code, making sure that you're copying the Swift code and not the
27

28
00:02:07,050 --> 00:02:13,220
Objective-C code which will look very strange anyways. And heading back to our app,
28

29
00:02:13,230 --> 00:02:18,660
we're going to go to the LoginViewController.swift and we're going to put that code inside the
29

30
00:02:18,810 --> 00:02:21,270
loginPressed IBAction.
30

31
00:02:21,570 --> 00:02:26,870
So that, of course, relates to this particular button when it gets pressed.
31

32
00:02:27,180 --> 00:02:32,560
And what we want to happen is to trigger this sign in with email and password method.
32

33
00:02:32,640 --> 00:02:40,080
So, again, we need to tell this file that we want to use the Firebase module, so that we can use the Auth object
33

34
00:02:40,170 --> 00:02:47,730
and we also have to supply an email and password. And I want to quickly pause here and pose a
34

35
00:02:47,730 --> 00:02:48,960
challenge to you.
35

36
00:02:49,230 --> 00:02:54,570
See if you can add the code to retrieve the tags that the user typed into the email and the password fields,
36

37
00:02:54,570 --> 00:03:01,620
then you should only call the sign and method if neither the email nor the password text is equal
37

38
00:03:01,620 --> 00:03:02,730
to nil.
38

39
00:03:02,760 --> 00:03:06,060
I'll give you a few seconds to pause the video before I show you the solution.
39

40
00:03:10,200 --> 00:03:10,680
Ready?
40

41
00:03:10,680 --> 00:03:12,270
Here's the solution.
41

42
00:03:12,270 --> 00:03:18,270
I'm going to do the same thing that we did before to use optional binding to get out the email text
42

43
00:03:18,390 --> 00:03:19,440
and password text.
43

44
00:03:20,130 --> 00:03:27,780
So I'm going to write "if let email = emailTextField.text, and then add a comma,
44

45
00:03:27,780 --> 00:03:34,770
and let password = passwordTextField.text, and then we're going to wrap all of this authentication
45

46
00:03:34,770 --> 00:03:38,750
code inside a set of curly brackets like so.
46

47
00:03:39,030 --> 00:03:42,740
And now we're ready to tackle this block.
47

48
00:03:43,050 --> 00:03:44,850
And that's the solution to the challenge.
48

49
00:03:45,000 --> 00:03:49,740
The code we wrote is very similar to what we did in the RegisterViewController.
49

50
00:03:49,740 --> 00:03:55,500
Now, you might have noticed that this particular signIn method actually looks a little bit different
50

51
00:03:55,830 --> 00:04:00,900
from the create user method that we got from Firebase earlier on.
51

52
00:04:00,900 --> 00:04:08,850
And the extra bits are this part, the "weak self," and also this "guard" statement, and this is just a little
52

53
00:04:08,850 --> 00:04:15,960
bit of code that Firebase has actually left in here which is actually slightly outdated. The latest syntax
53

54
00:04:15,960 --> 00:04:17,830
would look something like this.
54

55
00:04:17,940 --> 00:04:24,990
And what it does is it avoids a retained cycle in the cases where we're actually referring to the view
55

56
00:04:24,990 --> 00:04:27,120
controller within our closure,
56

57
00:04:27,510 --> 00:04:32,980
and we're going to be destroying this view controller before this closure completes.
57

58
00:04:32,970 --> 00:04:40,110
So if you're interested in getting more details on this and why it is that we don't actually need this
58

59
00:04:40,110 --> 00:04:47,700
part or this part in our code, then I've linked to a number of resources for you to check out. And I have
59

60
00:04:47,700 --> 00:04:52,390
to warn you it's quite complex and the rabbit hole goes quite deep,
60

61
00:04:52,560 --> 00:04:56,400
but I found some resources that explain this topic quite well.
61

62
00:04:56,400 --> 00:05:01,180
So if you're interested in the nitty-gritty details, then be sure to check it out.
62

63
00:05:01,260 --> 00:05:01,500
All right.
63

64
00:05:01,500 --> 00:05:08,460
So coming back to our Authentication signIn method, we want to do the same thing that we did before
64

65
00:05:08,490 --> 00:05:11,620
which is to check if there was an error.
65

66
00:05:11,640 --> 00:05:17,010
So if let e = error, then in this case, I'm going to simply just print the error,
66

67
00:05:17,400 --> 00:05:23,010
but, of course, we could also handle this error by presenting it to the user, in which case we would have
67

68
00:05:23,010 --> 00:05:28,530
to take a look at the reference for "FIRAuthErrors" to see all of the different error codes, create a
68

69
00:05:28,530 --> 00:05:34,560
switch statement that checks for which one we're gettingm and then turn all of those error codes into
69

70
00:05:34,560 --> 00:05:39,690
a message that's useful for the user, and then present it in some sort of alerts.
70

71
00:05:39,690 --> 00:05:44,310
Now in my case, I'm just going to print it out for the developer to see.
71

72
00:05:44,490 --> 00:05:51,570
But if we know that the error is equal to nil, then we can go ahead and add an "else" statement to proceed
72

73
00:05:51,630 --> 00:05:52,980
with the login process.
73

74
00:05:53,460 --> 00:05:58,890
So in this case, what we're gonna do is we're going to keep the login screen in our navigation stack.
74

75
00:05:58,950 --> 00:06:02,130
So at this point, they've got the welcome screen at the bottom,
75

76
00:06:02,160 --> 00:06:08,010
the login screen next, and then on top of that, we're going to present the next screen which is the
76

77
00:06:08,040 --> 00:06:14,130
ChatViewController. And I want to pose this to you as another challenge. See if you can write the code that
77

78
00:06:14,130 --> 00:06:20,270
navigates the ChatViewController when signIn has been successful. I'll give you a few seconds to pose
78

79
00:06:20,270 --> 00:06:24,740
a video before I show you the solution.
79

80
00:06:24,850 --> 00:06:25,360
Ready?
80

81
00:06:25,360 --> 00:06:26,770
Here's the solution.
81

82
00:06:26,770 --> 00:06:32,230
We're going to navigate to the ChatViewController in the same way as we did in the RegisterViewController
82

83
00:06:32,230 --> 00:06:37,260
which is to perform a segue with a particular identifier.
83

84
00:06:37,600 --> 00:06:42,370
And in our case, that segue is this one which we named LoginToChat.
84

85
00:06:42,430 --> 00:06:48,970
So I'm just going to copy it from here to minimize the possibility of typos. And then down here in this
85

86
00:06:49,000 --> 00:06:54,730
"else" statement, I'm going to say performSegue withIdentifier and sender is "self."
86

87
00:06:54,730 --> 00:06:57,940
This is the starting point of our segue.
87

88
00:06:57,940 --> 00:07:04,600
And the final thing we have to do is to add the "self" in front of our performSegue method call, and
88

89
00:07:04,600 --> 00:07:06,180
that's the solution to the challenge.
89

90
00:07:06,190 --> 00:07:12,180
We're now ready to run our app and test it out, so instead of going to the register page, I'm going to
90

91
00:07:12,180 --> 00:07:19,440
go into the login page. And here I'm going to use the credentials that I used previously to register
91

92
00:07:19,830 --> 00:07:23,930
which is a user with email 1@2.com and their password was
92

93
00:07:23,930 --> 00:07:25,450
1, 2, 3, 4, 5, 6.
93

94
00:07:25,500 --> 00:07:31,970
Because six is the minimum number of characters I have to supply Firebase with in order for it to accept
94

95
00:07:31,970 --> 00:07:33,660
it as a password.
95

96
00:07:33,660 --> 00:07:40,240
So, now my email is going to be 1@2.com and my password: 1, 2, 3, 4, 5, 6,
96

97
00:07:40,410 --> 00:07:42,660
and then I'm going to click Log In.
97

98
00:07:42,660 --> 00:07:46,700
And as you can see, the login process was successful.
98

99
00:07:46,770 --> 00:07:53,580
I didn't get any error messages and it performed to that segue taking me to the Chat View Controller right
99

100
00:07:53,580 --> 00:07:55,150
here.
100

101
00:07:55,290 --> 00:07:59,420
So that's how we log in our users. In the next lesson,
101

102
00:07:59,460 --> 00:08:06,300
we're going to be covering the logout route so that we'll be able to log in users, register users, and
102

103
00:08:06,390 --> 00:08:09,480
also log them out back to the welcome screen.
103

104
00:08:09,540 --> 00:08:12,480
So for all of that and more, I'll see you on the next lesson.
