1
00:00:07,790 --> 00:00:12,230
Hey hey everybody what's going on this is Caleb with Devon Lopes dotcom.

2
00:00:12,260 --> 00:00:17,570
And in this video we're going to set up our authentication service which includes two very critical

3
00:00:17,570 --> 00:00:21,170
functions register user and log in user.

4
00:00:21,230 --> 00:00:27,530
These both are going to be what allow us to actually authenticate users add users to our app and also

5
00:00:27,530 --> 00:00:30,920
it's going to allow us to check to see whether they're logged in or not which is going to be really

6
00:00:30,920 --> 00:00:36,440
important for just monitoring just the authentication status of our app.

7
00:00:36,440 --> 00:00:36,730
All right.

8
00:00:36,740 --> 00:00:42,470
So go ahead and pull open that X code project and we're going to go ahead and create a new file in our

9
00:00:42,470 --> 00:00:46,280
services group called Auth. service.

10
00:00:46,280 --> 00:00:49,370
Right click on services click new file.

11
00:00:49,370 --> 00:00:55,610
And what we're going to do is create a new Swift file doesn't need to subclass anything but we're going

12
00:00:55,610 --> 00:00:58,300
to go ahead and call this auth service.

13
00:00:58,520 --> 00:01:04,040
And this is of course just like the data service going to be a singleton class where we're going to

14
00:01:04,040 --> 00:01:10,070
instantiate an instance of this class inside itself which gives us access to it throughout the entire

15
00:01:10,070 --> 00:01:12,230
life cycle of our app from any class.

16
00:01:12,230 --> 00:01:14,250
It's very cool and very helpful.

17
00:01:14,480 --> 00:01:19,940
But we're also being careful not to abuse the singleton pattern because it's very easy to do that and

18
00:01:19,940 --> 00:01:23,850
we don't want to do that because we want to use this how we need it and that's all.

19
00:01:24,020 --> 00:01:29,390
So let's go ahead and let's begin actually by importing firebase because we're going to need that to

20
00:01:29,390 --> 00:01:31,160
authenticate users.

21
00:01:31,160 --> 00:01:32,450
And I don't want to forget.

22
00:01:32,460 --> 00:01:42,320
So let's go ahead create the class and call it author service then we're going to create a static constant

23
00:01:42,320 --> 00:01:43,160
called instance.

24
00:01:43,160 --> 00:01:49,880
And the reason we're creating a static instance is because it allows the static variable to be alive

25
00:01:49,940 --> 00:01:54,830
for the entire life cycle of the app so as long as the app is running you have access to that instance

26
00:01:54,920 --> 00:01:56,060
of this class.

27
00:01:56,060 --> 00:02:03,950
So go ahead and type static let instance equals auth service put parentheses at the end to instantiate

28
00:02:03,950 --> 00:02:04,520
it.

29
00:02:04,640 --> 00:02:11,530
And we're going to create two functions like I said one is phunk register user.

30
00:02:11,540 --> 00:02:19,370
The other is phunk log in user code and are apps going to be pretty interesting because if a user has

31
00:02:19,370 --> 00:02:24,200
not yet registered they're going to be able to put in their email and password and click sign in if

32
00:02:24,200 --> 00:02:29,930
they're not registered it will register them then log them in if they are registered it will just log

33
00:02:29,930 --> 00:02:30,220
them in.

34
00:02:30,230 --> 00:02:30,940
It's pretty cool.

35
00:02:30,950 --> 00:02:35,870
Kind of adds to the user experience it makes it very simple to get into our app and start using it.

36
00:02:35,870 --> 00:02:42,110
Now there are some important things that we actually need to know and actually need to do in order to

37
00:02:42,110 --> 00:02:44,510
register a user successfully.

38
00:02:44,510 --> 00:02:45,530
Now what we need.

39
00:02:45,590 --> 00:02:46,570
We need their e-mail.

40
00:02:46,580 --> 00:02:47,710
We need their password.

41
00:02:47,720 --> 00:02:50,910
And we also need to know when registration is done.

42
00:02:51,110 --> 00:02:57,720
Because like I said when it's done we should be able to call our log in function to log them in automatically.

43
00:02:57,770 --> 00:03:00,390
So go ahead and we're going to type with e-mail.

44
00:03:00,500 --> 00:03:06,090
So register user with e-mail or use an internal parameter here called e-mail.

45
00:03:06,140 --> 00:03:12,140
Next I'm going to say and password internal parameter called password that's just something I like to

46
00:03:12,140 --> 00:03:17,990
do because if I was to just say and password I would have to use that and password name in here.

47
00:03:17,990 --> 00:03:22,530
But really I'm just talking about the password not an and password or with e-mail.

48
00:03:22,580 --> 00:03:27,920
So internal parameters are kind of cool because they let you set the actual name of what your what kind

49
00:03:27,920 --> 00:03:29,650
of information you're dealing with.

50
00:03:29,660 --> 00:03:32,340
So registry user with email and password.

51
00:03:32,630 --> 00:03:38,710
And we're going to go ahead and also create a completion handler using an escaping closure.

52
00:03:38,750 --> 00:03:47,120
So go ahead and type user creation complete and that's going to be escaping Like I said so that values

53
00:03:47,120 --> 00:03:54,300
can be taken out like in this case we want to be able to send an error to an air handler just in case

54
00:03:54,320 --> 00:04:00,770
there's a problem and we're going to create a function here and we're going to return an empty function

55
00:04:00,790 --> 00:04:03,330
K that means we're returning nothing because we don't need to.

56
00:04:03,350 --> 00:04:05,340
But we do need to pass values out.

57
00:04:05,360 --> 00:04:11,400
So in order to do that we're going to do underscore status is of type bool meaning.

58
00:04:11,750 --> 00:04:18,440
Is it complete or is it not complete then we're going to go ahead and we're going to return or pass

59
00:04:18,470 --> 00:04:20,200
an error if there is one.

60
00:04:20,300 --> 00:04:21,740
But sometimes there's not.

61
00:04:21,770 --> 00:04:27,470
So we're going to set error to actually be optional meaning we can pass nil here and it's OK it's not

62
00:04:27,470 --> 00:04:30,360
going to crash our app because it can except nil.

63
00:04:30,590 --> 00:04:33,610
So very cool once or user creation is complete.

64
00:04:33,620 --> 00:04:34,310
We're going to pass.

65
00:04:34,310 --> 00:04:35,090
True.

66
00:04:35,450 --> 00:04:40,610
If there's an error and it's not we're going to pass false if we get an error from our account creation

67
00:04:40,610 --> 00:04:46,910
like maybe it notices it's not a real email there's no sign there's no dot com dot org we'll send an

68
00:04:46,940 --> 00:04:48,980
error through and we'll print that.

69
00:04:49,100 --> 00:04:49,810
OK.

70
00:04:50,030 --> 00:04:56,660
So that's our function for registry user logon user is actually going to be basically identical because

71
00:04:56,660 --> 00:05:02,320
to log in a user We need an e-mail we need a password and we need to know when it's done.

72
00:05:02,330 --> 00:05:03,730
So I'm going to actually copy this.

73
00:05:03,740 --> 00:05:09,570
I'm being a bad coder but I'm going to paste it in and instead of saying user creation complete Let's

74
00:05:09,570 --> 00:05:11,790
just say log in complete.

75
00:05:12,510 --> 00:05:13,220
OK.

76
00:05:13,530 --> 00:05:14,070
Very cool.

77
00:05:14,070 --> 00:05:16,260
And we'll come back to log in user in a second.

78
00:05:16,260 --> 00:05:20,990
But for now let's go into registry user and let's think about what we need to do.

79
00:05:21,150 --> 00:05:26,850
We need to pass in the e-mail and password and we need to use firebase in order to actually register

80
00:05:26,850 --> 00:05:29,080
a user into our database.

81
00:05:29,160 --> 00:05:33,450
So to do that we're going to use some of the nice built in functions from firebase.

82
00:05:33,930 --> 00:05:42,780
So go ahead and type off dot off and there's a function in here called create user with e-mail password

83
00:05:43,170 --> 00:05:45,400
and it has a completion handler which is pretty cool.

84
00:05:45,510 --> 00:05:51,240
So let's go ahead and let's give it the e-mail that we passed and from our function give it the password

85
00:05:51,570 --> 00:05:53,480
that we passed in from our password.

86
00:05:53,640 --> 00:05:58,050
And for the result callback press enter and you'll see there are two things.

87
00:05:58,050 --> 00:06:02,760
Given user and error go ahead and name them user and error.

88
00:06:02,760 --> 00:06:05,000
And those are the things that are passed back.

89
00:06:05,010 --> 00:06:10,010
So as you can see they're optional meaning sometimes there might not be a user if there's not then it'll

90
00:06:10,020 --> 00:06:12,510
probably throw an error saying this user doesn't exist.

91
00:06:12,690 --> 00:06:18,220
So let's just call it user and error as constants to hold those values.

92
00:06:18,240 --> 00:06:24,420
Now what we need to do is if a user is returned we're going to create a constant to hold all of the

93
00:06:24,420 --> 00:06:26,450
information for that user.

94
00:06:26,520 --> 00:06:31,050
If there is not a user We're going to basically handle any errors that come back.

95
00:06:31,050 --> 00:06:36,360
So we're going to use guard less so that we can safely create a user if there is one returned.

96
00:06:36,360 --> 00:06:39,310
But if there's not then we're going to handle the error.

97
00:06:39,310 --> 00:06:46,520
So go ahead and type guard let user equals user WIPs user.

98
00:06:46,560 --> 00:06:50,510
That means if a user is returned we will do that else.

99
00:06:50,880 --> 00:06:56,660
And I'm actually going to go ahead and inside the brackets here I'm going to handle the error.

100
00:06:56,670 --> 00:07:00,390
So go ahead and type user creation complete.

101
00:07:00,660 --> 00:07:03,200
And this is where we can pass in false.

102
00:07:03,210 --> 00:07:07,710
The user was not created and the error is going to be the error.

103
00:07:07,720 --> 00:07:09,740
We're going to pass the error back to our handlers.

104
00:07:09,750 --> 00:07:11,260
We can use it later on.

105
00:07:11,490 --> 00:07:13,170
That's pretty cool.

106
00:07:13,170 --> 00:07:16,220
Now what we're going to do is we're going to just leave it at that.

107
00:07:16,230 --> 00:07:18,560
And that is what we will do if there is an error.

108
00:07:18,750 --> 00:07:23,760
We're going to say false the user creation was not complete and there was an error.

109
00:07:24,000 --> 00:07:25,960
But what if there was no error.

110
00:07:26,040 --> 00:07:28,980
We get through we have a value in user.

111
00:07:28,980 --> 00:07:35,560
We should create an account for that user so if you remember in our data service let's call data service

112
00:07:35,900 --> 00:07:41,030
instance dot create DB user We need a unique ID.

113
00:07:41,220 --> 00:07:44,580
We need a unique ID and a user dictionary.

114
00:07:44,730 --> 00:07:47,020
That's where all the data gets passed to firebase.

115
00:07:47,040 --> 00:07:52,500
So in order to actually pass that we need to create a user data dictionary.

116
00:07:52,500 --> 00:07:54,560
So let's go ahead and do that by typing.

117
00:07:54,600 --> 00:07:56,430
Let user data.

118
00:07:56,820 --> 00:07:59,540
And that's going to be equal to a dictionary.

119
00:07:59,910 --> 00:08:06,310
And for the first key value pair We're going to go ahead and just call provider now.

120
00:08:06,450 --> 00:08:10,770
If you dive deeply into firebase you will see there are different providers.

121
00:08:10,770 --> 00:08:12,020
One is Facebook.

122
00:08:12,060 --> 00:08:15,050
One is Google one is firebase one is e-mail.

123
00:08:15,120 --> 00:08:21,900
Now we're going to be creating an email user so our firebase provider is just going to be pulled from

124
00:08:21,900 --> 00:08:25,110
user K assuming the user comes through.

125
00:08:25,200 --> 00:08:31,500
And if you put a period there you can see that there is a provider I.D. and that's the information that

126
00:08:31,500 --> 00:08:34,710
comes through to us a provider I.D. can be firebase.

127
00:08:34,760 --> 00:08:38,400
Email Facebook Google et cetera.

128
00:08:38,400 --> 00:08:42,030
Next we're going to go ahead and we're going to send up the e-mail from the user.

129
00:08:42,270 --> 00:08:49,940
So let's give it a key of e-mail and the value is going to be user e-mail.

130
00:08:50,300 --> 00:08:51,140
OK.

131
00:08:51,300 --> 00:08:54,440
If there's a user We're going to get its e-mail and that's awesome.

132
00:08:54,440 --> 00:08:56,960
We now have a dictionary of user data.

133
00:08:57,120 --> 00:08:58,440
Very very cool.

134
00:08:58,530 --> 00:09:04,680
Now to create the database user we need to pass a unique ID but you know what the cool thing is it already

135
00:09:04,680 --> 00:09:05,700
has one.

136
00:09:05,700 --> 00:09:11,880
Let's go ahead and type user you I.D. the user already has a unique ID that it's given.

137
00:09:11,880 --> 00:09:12,830
So that's really easy.

138
00:09:12,830 --> 00:09:13,910
We don't have to generate it.

139
00:09:13,910 --> 00:09:17,340
We don't have to write a function that can generate a random number that's different than all the other

140
00:09:17,340 --> 00:09:18,120
numbers.

141
00:09:18,120 --> 00:09:24,770
We can just pull the ID from firebase so so helpful for user data of course we just created that dictionary

142
00:09:24,780 --> 00:09:31,050
so let's pass it user data and you know what when we call this if everything is here that means that

143
00:09:31,050 --> 00:09:35,520
our user will actually be sent up to firebase will have a user in our database.

144
00:09:35,520 --> 00:09:36,820
Very very cool.

145
00:09:37,050 --> 00:09:43,620
So what we do need to do actually sorry is we need to return.

146
00:09:44,280 --> 00:09:45,560
And that's part of guard led.

147
00:09:45,550 --> 00:09:46,570
I forgot about that.

148
00:09:46,740 --> 00:09:49,150
If if there is a problem.

149
00:09:49,200 --> 00:09:49,770
OK.

150
00:09:49,950 --> 00:09:55,530
We will pass false to our completion handler will handle the error and then we're going to go ahead

151
00:09:55,560 --> 00:09:57,010
and return out of it.

152
00:09:57,080 --> 00:09:57,350
OK.

153
00:09:57,360 --> 00:09:59,150
But if it works then we'll have a value.

154
00:09:59,160 --> 00:10:05,550
We can create this dictionary create the database user then at the end we can go ahead and call user

155
00:10:05,550 --> 00:10:12,800
completion or use your creation complete and we can pass in true and we can pass in nil because there

156
00:10:12,800 --> 00:10:14,060
will be no errors.

157
00:10:14,060 --> 00:10:14,840
Very very cool.

158
00:10:14,840 --> 00:10:20,450
So that's how we're going to register a user We're going to pass up the data to firebase If an email

159
00:10:20,450 --> 00:10:22,070
and password come in.

160
00:10:22,070 --> 00:10:24,210
And if there is a problem right.

161
00:10:24,230 --> 00:10:29,780
If maybe it's not a real e-mail or maybe if the password is not enough characters which will set that

162
00:10:29,780 --> 00:10:32,380
up later it will pass us an error.

163
00:10:32,720 --> 00:10:33,070
All right.

164
00:10:33,080 --> 00:10:34,130
Very cool.

165
00:10:34,130 --> 00:10:39,410
Next up let's go ahead and let's log in the user and we're going to do it pretty much the same way just

166
00:10:39,410 --> 00:10:43,570
with a different function from firebase So go ahead and call off.

167
00:10:43,760 --> 00:10:44,610
Off.

168
00:10:44,690 --> 00:10:49,810
Log in user sign in with e-mail.

169
00:10:49,900 --> 00:10:52,060
You'll see it down there it's like the fifth option.

170
00:10:53,510 --> 00:10:54,950
Very cool.

171
00:10:54,950 --> 00:11:00,160
And for the e-mail of course we'll pass an email for the password will pass in the password.

172
00:11:00,220 --> 00:11:07,250
OK remember email and password from our function and the completion press Enter just like above if we

173
00:11:07,970 --> 00:11:09,330
attempt to sign in.

174
00:11:09,410 --> 00:11:12,750
We should get a user back if we have a problem.

175
00:11:13,130 --> 00:11:16,340
If we have a problem we'll get an error back.

176
00:11:16,340 --> 00:11:17,500
Simple as that.

177
00:11:17,720 --> 00:11:21,020
And what we're going to do is we're going to handle what happens if we get a user.

178
00:11:21,020 --> 00:11:22,510
So go ahead and type garb let.

179
00:11:22,520 --> 00:11:25,870
Just like above user equals user.

180
00:11:26,450 --> 00:11:27,830
Else if we don't.

181
00:11:27,850 --> 00:11:28,120
Right.

182
00:11:28,130 --> 00:11:35,240
If there if we don't get a successful user back go ahead and call log in completion or log in complete

183
00:11:35,660 --> 00:11:41,240
is false we didn't log in and we had an error and we're going to pass it that error that we get from

184
00:11:41,300 --> 00:11:42,610
our closure here.

185
00:11:42,620 --> 00:11:42,910
OK.

186
00:11:42,950 --> 00:11:44,350
That's pretty cool.

187
00:11:44,360 --> 00:11:46,810
Now of course at the end we need to return.

188
00:11:46,820 --> 00:11:51,560
We just need to get out of this function if we don't successfully get a user.

189
00:11:51,560 --> 00:11:59,000
So if we do get a value back for user all we need to do is just type log incomplete and go ahead and

190
00:11:59,450 --> 00:12:04,880
give it a true for its boolean and for the error we can just say nil because there was no problem.

191
00:12:04,880 --> 00:12:11,660
Very very cool and sign in will happen on its own when it's done you'll know that it has been successfully

192
00:12:11,660 --> 00:12:12,770
authenticated.

193
00:12:12,770 --> 00:12:19,280
We have successfully logged in and this is it guys we have now set up the functions to register a user

194
00:12:19,670 --> 00:12:21,190
and to log in a user.

195
00:12:21,260 --> 00:12:22,530
Pretty pretty cool.

196
00:12:22,640 --> 00:12:28,910
In the next video we're going to actually set up so that our log in Visi is presented from our app delegate

197
00:12:29,180 --> 00:12:31,330
that will be monitoring our authentication status.

198
00:12:31,340 --> 00:12:36,070
And as soon as we log out it's going to present that log in B.C. automatically for us.

199
00:12:36,140 --> 00:12:41,820
We're going to also set up how we can present off AVC from the log in VC.

200
00:12:42,140 --> 00:12:46,160
And we're also going to set up Visi so that we can log in with our e-mail.

201
00:12:46,190 --> 00:12:47,210
That's super super cool.

202
00:12:47,210 --> 00:12:52,310
We're going to actually allow it to authenticate us and log into firebase So we'll see in the next video.

203
00:12:52,310 --> 00:12:53,270
Awesome job with this one.

204
00:12:53,270 --> 00:12:53,870
Let's head over there.
