1
00:00:05,110 --> 00:00:08,780
Welcome back to class everyone Johnny be here with slopes does come.

2
00:00:08,800 --> 00:00:11,780
And in this lesson we are going to loggin users.

3
00:00:11,780 --> 00:00:17,060
All right so just to recap the last lesson we put in all of the code to create a user.

4
00:00:17,110 --> 00:00:24,760
We added code to change add a change request and update the display name for our user as well as create

5
00:00:24,820 --> 00:00:31,090
a new collection with a user document with the user id as the document ID for each document.

6
00:00:31,090 --> 00:00:33,780
Now let's go ahead and work in the log and see.

7
00:00:33,790 --> 00:00:34,020
All right.

8
00:00:34,040 --> 00:00:36,220
This one's This one's quite a bit easier.

9
00:00:36,220 --> 00:00:36,690
OK.

10
00:00:36,950 --> 00:00:39,880
I've got to do here is under logon button tab.

11
00:00:39,940 --> 00:00:44,400
Well first we've got to import firebase say import firebase.

12
00:00:44,560 --> 00:00:52,930
Well first let's get our e-mail and passwords from the textfield we say guard that e-mail equal or e-mail

13
00:00:53,110 --> 00:01:04,650
text that text and I'm going to chain the next one so that password equal password text that text or

14
00:01:04,650 --> 00:01:06,280
else we will return.

15
00:01:06,530 --> 00:01:06,850
OK.

16
00:01:06,970 --> 00:01:14,410
So now we have our e-mail and our password so to log in we got to do is say off which is the base for

17
00:01:14,500 --> 00:01:20,770
the base class for our authentication and then we're going say off to get the instance and we're going

18
00:01:20,770 --> 00:01:22,800
to say sign in.

19
00:01:22,970 --> 00:01:23,640
OK.

20
00:01:24,010 --> 00:01:26,300
And of course we want the with e-mail.

21
00:01:26,500 --> 00:01:26,820
OK.

22
00:01:26,890 --> 00:01:32,240
So the e-mail that we're sending in with is e-mail and the password is password.

23
00:01:32,300 --> 00:01:33,610
We're on our completion.

24
00:01:33,610 --> 00:01:40,140
So when this press return and in our response we get access to our reference or our user and our error

25
00:01:40,430 --> 00:01:45,770
code and in our code all we're going to do is we're going to check for the air Rissa if left error is

26
00:01:45,800 --> 00:01:47,180
equal to error.

27
00:01:47,680 --> 00:01:54,420
And if there is an error we'll just debug print that out and it will say error.

28
00:01:54,670 --> 00:02:01,620
Signing in and we'll string interpolate hower error here.

29
00:02:02,440 --> 00:02:05,550
Else we were done.

30
00:02:05,590 --> 00:02:05,920
That's it.

31
00:02:05,950 --> 00:02:13,140
All we've got to do is say self that this Miss animated is true and completion is no.

32
00:02:13,480 --> 00:02:20,340
So we pressed the log in and we attempt a log in and if we are unsuccessful we'll print out the error.

33
00:02:20,380 --> 00:02:24,970
If we are successful then we're done here with the log and in C and we just dismiss back to the main

34
00:02:24,970 --> 00:02:25,670
visi.

35
00:02:26,050 --> 00:02:32,230
And here we created a and b action for our create user tab and just realizing that we actually don't

36
00:02:32,230 --> 00:02:37,990
need this flow we want to do actually or what we can do we could use the action there but it's easier

37
00:02:37,990 --> 00:02:45,280
to just create a direct segue by holding control and dragging from our create user button to our create

38
00:02:45,280 --> 00:02:48,190
user Visi and press show.

39
00:02:48,650 --> 00:02:54,420
OK so now we can just click on the Create use a button and automatically segue back here for us.

40
00:02:54,430 --> 00:02:54,690
All right.

41
00:02:54,700 --> 00:02:57,820
So we have our log in the C code.

42
00:02:57,880 --> 00:03:02,980
So let's pretend that we have used that that we have logged in we have access to the current user through

43
00:03:03,040 --> 00:03:04,430
out the app and now.

44
00:03:04,590 --> 00:03:07,120
And we also get some other cool stuff for free.

45
00:03:07,150 --> 00:03:10,620
The other instance persists the user state for us.

46
00:03:10,660 --> 00:03:13,590
So if we close the app and come back in we're still logged in.

47
00:03:13,600 --> 00:03:17,140
We don't have to handle that ourselves with like user defaults or anything like that.

48
00:03:17,380 --> 00:03:24,120
And when we log out the current user head back to nil and we're ready to sign back in again.

49
00:03:24,460 --> 00:03:28,010
All right so we're able to log any user.

50
00:03:28,070 --> 00:03:32,030
How do we keep track of the logged in state.

51
00:03:32,110 --> 00:03:35,680
And so this is where we're going to get back into our main visi.

52
00:03:35,920 --> 00:03:41,020
And I know we don't have a way to actually get to this screen yet but that's going to be here in just

53
00:03:41,020 --> 00:03:41,800
a second.

54
00:03:42,010 --> 00:03:45,190
All right we're back here in our main This is where we're going to keep track of whether or not we're

55
00:03:45,190 --> 00:03:45,930
logged in.

56
00:03:46,180 --> 00:03:53,680
And for that we create an off state did to change Lisner handle and get basically this handle is responsible

57
00:03:53,890 --> 00:03:59,920
for listening for changes in the logged in status it is notified when a user is resuming a signed in

58
00:03:59,920 --> 00:04:06,160
session or when they sign out or when they sign in for a signed outstate or when their access token

59
00:04:06,160 --> 00:04:07,030
is refreshed.

60
00:04:07,140 --> 00:04:07,430
OK.

61
00:04:07,450 --> 00:04:10,170
So a little bit about that refreshed token.

62
00:04:10,180 --> 00:04:16,150
This occurs under the following circumstances when the token expires when the user changes password

63
00:04:16,270 --> 00:04:19,250
or when the user read authenticates.

64
00:04:19,540 --> 00:04:24,940
So we are going to create our handle right here under variables.

65
00:04:25,030 --> 00:04:34,140
I'm going to say private var handle and this is an off state did change Lisner handle.

66
00:04:34,210 --> 00:04:35,060
OK.

67
00:04:35,610 --> 00:04:41,290
And that's going to be an optional and we're going to implement that in our view will appear and I actually

68
00:04:41,290 --> 00:04:42,000
want this.

69
00:04:42,010 --> 00:04:45,190
I'm going to cut this and add it up here under load.

70
00:04:45,400 --> 00:04:50,760
Just kind of keep things together and then I'm going to grab our view will disappear from here.

71
00:04:50,770 --> 00:04:54,620
Cut it and move that appear under that view will appear.

72
00:04:54,640 --> 00:04:55,010
There you go.

73
00:04:55,180 --> 00:04:56,850
I like that better.

74
00:04:56,890 --> 00:04:57,320
OK.

75
00:04:57,400 --> 00:05:03,700
So what do we want to have happen when we open the app if we are not logged in then we need to transition

76
00:05:03,940 --> 00:05:06,350
and segue to the log and see.

77
00:05:06,370 --> 00:05:10,270
However if we are logged in then we just proceed as normal.

78
00:05:10,270 --> 00:05:14,010
We will set our listener and everything will be the same as it was.

79
00:05:14,260 --> 00:05:16,790
So here we're going to do it.

80
00:05:17,020 --> 00:05:27,580
So here we're going to add our listeners so we're going to say handle is equal to off dot off dot and

81
00:05:27,800 --> 00:05:30,400
state to change the listener.

82
00:05:30,520 --> 00:05:32,310
And we got to push and Hendler here.

83
00:05:32,470 --> 00:05:35,960
We have access to the off instance and the user.

84
00:05:35,960 --> 00:05:40,870
And so here in the code what we can do is we can listen and we can do a check on the user and caesareans

85
00:05:40,940 --> 00:05:43,620
if user is nil.

86
00:05:43,920 --> 00:05:46,630
So if the user has no that means that we are not logged in.

87
00:05:47,040 --> 00:05:56,200
OK else if we are logged in there and we're just going to set the listener and the app will perform

88
00:05:56,230 --> 00:05:58,480
as it has in the past.

89
00:05:58,480 --> 00:05:58,740
All right.

90
00:05:58,740 --> 00:06:05,140
So but if we are not logged in I need the self here self if we are not logged in.

91
00:06:05,140 --> 00:06:09,470
Then we need to instantiate the log in VC and presented.

92
00:06:09,550 --> 00:06:09,950
OK.

93
00:06:10,120 --> 00:06:14,290
So the way to do that is we're just going to reference it from the storyboard and then present it.

94
00:06:14,300 --> 00:06:25,990
So when I say left storyboard will you buy this Daury board and angry to instantiate it and say name

95
00:06:26,110 --> 00:06:29,250
the name of it is just main.

96
00:06:29,290 --> 00:06:37,510
The bundle is now and then we're going to instantiate the log in VCs who want to say that laga and VC

97
00:06:37,720 --> 00:06:43,540
equal storyboard dot the stain sheet view controller with identifier.

98
00:06:43,750 --> 00:06:47,890
So here we need an identifier and I don't think we actually set that in storyboards so I'm going to

99
00:06:47,890 --> 00:06:53,750
come over to mean that storyboard looked at our log in VC come up to our attributes and Specter actually

100
00:06:53,850 --> 00:06:56,050
are things we think yeah.

101
00:06:56,200 --> 00:07:04,430
So here understory board ID we are going to say log in VC and get a copy that come back in the main

102
00:07:04,440 --> 00:07:10,200
You got swift and set the identifier here as log in visi.

103
00:07:10,900 --> 00:07:19,570
And lastly it's as simple as things self dot present view controller and the view controller that we

104
00:07:19,570 --> 00:07:24,910
want to present is Lague and visi animated is true and completion is now.

105
00:07:25,360 --> 00:07:31,270
All right so now when the controller when we launch the app we do a check if the users know we are not

106
00:07:31,270 --> 00:07:36,280
logged in then we instantiate and present the log in to sea.

107
00:07:36,370 --> 00:07:38,830
Otherwise we just set the listener and proceed as normal.

108
00:07:38,830 --> 00:07:40,180
So let's test this up.

109
00:07:40,300 --> 00:07:42,740
I'm going to press play here.

110
00:07:43,270 --> 00:07:53,080
So I forgot to change this here so now that we are doing a check for a log and used before we set the

111
00:07:53,080 --> 00:07:53,720
listener.

112
00:07:53,860 --> 00:07:58,850
Now it's not guaranteed that we are setting the listener and in the set listener function.

113
00:07:58,930 --> 00:08:04,870
This is where we actually instantiate our thoughts to a listener which up at the top.

114
00:08:04,870 --> 00:08:11,160
Here we set as a as implicitly on the wrapped value.

115
00:08:11,440 --> 00:08:14,270
So here and view will disappear.

116
00:08:14,440 --> 00:08:25,540
We need to do and if not check so I say if thoughts a listener is not equal to nil then we will remove

117
00:08:25,540 --> 00:08:26,030
it.

118
00:08:29,250 --> 00:08:30,510
That we try that again.

119
00:08:30,690 --> 00:08:31,490
All right.

120
00:08:31,750 --> 00:08:33,410
So here we go.

121
00:08:33,550 --> 00:08:34,530
So we are not logged in.

122
00:08:34,540 --> 00:08:39,760
So it immediately instantiated and presented this view controller very nice.

123
00:08:39,920 --> 00:08:46,190
And so we can go to create user mode we should be able to open up something went wrong.

124
00:08:46,720 --> 00:08:51,590
And I think it was because we deleted that IB action but did not remove it in the story board.

125
00:08:51,700 --> 00:08:58,120
So I'm back up to story board here under it create user we need to see here if I right click on our

126
00:08:58,630 --> 00:08:59,700
controller.

127
00:09:00,000 --> 00:09:02,180
Yep and then we have this morning right here.

128
00:09:02,230 --> 00:09:03,890
That was bad Johnny.

129
00:09:04,570 --> 00:09:10,460
So when you delete from your IB our actions or outlet's from code you need to also remove them in storyboard.

130
00:09:10,660 --> 00:09:14,220
So we're just going to click on that exit there and that should fix it.

131
00:09:14,240 --> 00:09:18,060
So when I run that again I'm not logged in.

132
00:09:18,070 --> 00:09:19,600
We present the log in to the sea.

133
00:09:19,630 --> 00:09:23,320
We don't have a user created yet so we need to go to create user.

134
00:09:23,350 --> 00:09:24,700
And there we go.

135
00:09:24,700 --> 00:09:25,900
All right so let's test this up.

136
00:09:25,900 --> 00:09:28,090
Going to put in e-mail I'm going to say J.

137
00:09:28,130 --> 00:09:28,900
J.

138
00:09:29,030 --> 00:09:35,730
Dot com password 1 2 3 4 5 6 and the user name is going to be Johnny B.

139
00:09:35,740 --> 00:09:36,880
Goode.

140
00:09:37,150 --> 00:09:40,510
I am going to press create user and let's see if this works.

141
00:09:42,010 --> 00:09:42,510
All right.

142
00:09:42,520 --> 00:09:43,630
So it looks like it works.

143
00:09:43,630 --> 00:09:46,510
Let's say we have created a user no errors down here.

144
00:09:46,510 --> 00:09:50,730
Let's check out our fire store authentication here.

145
00:09:50,860 --> 00:09:54,390
So under users we should now see a user.

146
00:09:54,420 --> 00:09:56,670
There we go Jeju dot com.

147
00:09:56,740 --> 00:09:58,240
Very nice.

148
00:09:58,240 --> 00:10:05,050
And in our database we should also have a new collection of users with a single document with a date

149
00:10:05,050 --> 00:10:06,370
created and user name.

150
00:10:06,370 --> 00:10:08,680
All right so that is pretty cool.

151
00:10:08,680 --> 00:10:13,280
All right so now that we have created a user let's log that user in here.

152
00:10:13,300 --> 00:10:19,030
So under e-mail we're going to enter that one that we just created J.J dot com and password is 1 2 3

153
00:10:19,030 --> 00:10:20,220
4 5 6.

154
00:10:20,530 --> 00:10:26,080
And I want to press logon and if it works then we should dismiss to our main Visi and then we set our

155
00:10:26,080 --> 00:10:26,620
listener.

156
00:10:26,620 --> 00:10:30,320
And so everything is now working perfectly.

157
00:10:30,500 --> 00:10:30,900
Right.

158
00:10:30,910 --> 00:10:32,640
Awesome.

159
00:10:32,650 --> 00:10:33,910
So now we can log in.

160
00:10:33,910 --> 00:10:35,230
How about signing out.

161
00:10:35,240 --> 00:10:36,940
Just go ahead and do that now.

162
00:10:36,970 --> 00:10:44,320
So that's going to be a pretty quick let's jump into the story board and here in our navigation bar

163
00:10:44,320 --> 00:10:50,620
here in the main Visi we we're going to do a search for a bar button and I'm just going to drag in a

164
00:10:50,650 --> 00:10:55,020
Barberton item over here and we're not going to do anything too fancy here.

165
00:10:55,030 --> 00:11:00,840
I'm just going to change the title to log out and that'll be that.

166
00:11:01,030 --> 00:11:06,010
And then we're going to download one of our assistant editor for the main VC by holding option and clicking

167
00:11:06,010 --> 00:11:14,040
on Main Visi and we're going to create an action for the for the sign out here.

168
00:11:14,050 --> 00:11:20,920
It's going it's like the barber Nighttime control and drag and changes to action and the name is going

169
00:11:20,920 --> 00:11:29,110
to be log out tapped it connect and that looks good.

170
00:11:29,110 --> 00:11:35,260
So I'm going to change back to our clothes our assistant editor and come back into our I mean does swift

171
00:11:35,950 --> 00:11:38,440
and let's find that out right here.

172
00:11:38,890 --> 00:11:46,780
So here what we do is we are going to attempt a log out and with firebase the sign out can throw a an

173
00:11:46,780 --> 00:11:47,940
error an exception.

174
00:11:47,980 --> 00:11:50,390
So we need to have a do try catch block.

175
00:11:50,560 --> 00:12:01,520
So when we say let firebase off equal off top off and then we're going to say do.

176
00:12:02,390 --> 00:12:07,880
And here we are going to try firebase off dot.

177
00:12:08,060 --> 00:12:09,710
Sign out.

178
00:12:10,430 --> 00:12:12,980
And if there is a problem we're going to catch it.

179
00:12:13,070 --> 00:12:24,980
I say catch let sign out error as and this error and then we'll just debug print the Ericsson's debug

180
00:12:25,340 --> 00:12:26,260
print.

181
00:12:26,720 --> 00:12:28,800
I'm going to say error.

182
00:12:28,820 --> 00:12:30,690
Finding out.

183
00:12:31,160 --> 00:12:36,340
And then we'll do our string intercalation and the sign out error.

184
00:12:36,740 --> 00:12:36,950
All right.

185
00:12:36,950 --> 00:12:38,120
And that was good.

186
00:12:38,120 --> 00:12:38,950
Let's see if this works.

187
00:12:39,050 --> 00:12:39,850
Save it.

188
00:12:39,980 --> 00:12:46,600
And just want to point out that now that we are signing out our state change listener is going to detect

189
00:12:46,610 --> 00:12:51,740
that and it's going to run this logic again and it's going to say if user is nil which it will be now

190
00:12:51,740 --> 00:12:57,440
that we signed out then it's going to relaunch the log and VC again get someone to run this.

191
00:12:57,440 --> 00:13:02,520
And if you remember in the last lesson I mentioned that with firebase authentication we get for free.

192
00:13:02,540 --> 00:13:04,480
The user state persistence.

193
00:13:04,580 --> 00:13:09,110
So we don't have to manually keep track of whether a user is logged in or not which is super nice and

194
00:13:09,110 --> 00:13:13,490
so because of that you'll see that we are automatically logged in right now.

195
00:13:13,490 --> 00:13:19,760
All right so if I click on a log out then we should immediately launch the log in visi.

196
00:13:19,770 --> 00:13:23,320
And because this was called and we ran the logic.

197
00:13:23,480 --> 00:13:28,640
And yet so now we are back to our log in view.

198
00:13:28,640 --> 00:13:29,700
All right so there we go.

199
00:13:29,780 --> 00:13:36,770
We are able to create users were able to log in those users were able to log back out were able to access

200
00:13:36,770 --> 00:13:45,300
the user anywhere in the app assuming we import firebase of course via the auth Singleton and Yesso.

201
00:13:45,320 --> 00:13:46,160
Pretty great stuff.

202
00:13:46,160 --> 00:13:47,940
Hope you guys are enjoying this so far.

203
00:13:48,080 --> 00:13:53,650
And the next lesson we're going to add a new feature to our app being able to add comments to our posts.

204
00:13:53,660 --> 00:13:54,730
I hope we're excited for that.

205
00:13:54,740 --> 00:13:56,460
And I will see you in the next one.
