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