1
00:00:04,930 --> 00:00:08,550
Welcome back to class everyone Johnny B here with slope stop calm.

2
00:00:08,590 --> 00:00:13,330
And in this lesson we are going to work on implementing firebase authentication.

3
00:00:13,330 --> 00:00:17,380
All right so the last lesson we created our nice lookin luggin views here.

4
00:00:17,380 --> 00:00:20,180
We got one for logging in and one for creating that user.

5
00:00:20,350 --> 00:00:23,980
So in this lesson we are going to work on hooking those up and getting them actually working and being

6
00:00:23,980 --> 00:00:25,500
able to logon users.

7
00:00:25,780 --> 00:00:32,290
So the first thing we're going to need to do is add the firebase authentication SDK to our project which

8
00:00:32,290 --> 00:00:34,850
we're going to do via cocoa pods.

9
00:00:34,870 --> 00:00:41,890
So if you're under our Pod's target here I can click on our pod file here and we can just add this right

10
00:00:41,890 --> 00:00:45,120
here is from the seed pod single quote.

11
00:00:45,380 --> 00:00:50,780
Firebase slash off get to see that.

12
00:00:51,160 --> 00:00:57,130
And then just going to open up a terminal and I've already I'm already in our CD project here.

13
00:00:57,190 --> 00:01:04,210
So all you've got to do is save pod install and that will update and install the new fire based off

14
00:01:04,270 --> 00:01:05,690
SDK.

15
00:01:05,690 --> 00:01:07,960
All right so we got that install just jump back in here.

16
00:01:07,960 --> 00:01:13,760
What's this error here that we may need to close this and reopen it.

17
00:01:13,960 --> 00:01:16,130
So that's fine is going to re open it.

18
00:01:20,920 --> 00:01:23,880
We're going to build that just to make sure everything's building fine.

19
00:01:23,890 --> 00:01:24,490
All right.

20
00:01:24,560 --> 00:01:25,830
We've got a successful build.

21
00:01:25,970 --> 00:01:28,600
So now we have firebase off inside of our project.

22
00:01:28,610 --> 00:01:31,840
Now this isn't something specific to fire store.

23
00:01:31,850 --> 00:01:38,010
This is a separate SDK that you can use in the fire store database or the real time database.

24
00:01:38,030 --> 00:01:42,140
But just wanted to show you how to use this very important to be able to offer users it.

25
00:01:42,200 --> 00:01:42,400
All right.

26
00:01:42,410 --> 00:01:47,350
So next up we need to go to our hot fire store database.

27
00:01:47,590 --> 00:01:47,900
All right.

28
00:01:47,910 --> 00:01:54,050
So get up in here in a console or in the browser window here and you may notice that we now have an

29
00:01:54,080 --> 00:01:57,590
Android app in our project.

30
00:01:57,590 --> 00:02:03,980
All right so you're able to have different platforms all using the same database.

31
00:02:03,980 --> 00:02:13,580
So I just want to show you that we have our android version of the app run it over here next to our

32
00:02:14,510 --> 00:02:22,310
version and you can kind of see that they have the same sets of data and I guess is a pretty cool thing.

33
00:02:22,310 --> 00:02:28,340
Both of those up and running at the same time offer the same database and if you want to learn how to

34
00:02:28,670 --> 00:02:34,940
make this app in the Android platform then go ahead and check out that course after you finish up this

35
00:02:34,970 --> 00:02:35,770
one.

36
00:02:36,210 --> 00:02:39,170
So I just want to show you that real quick and all right.

37
00:02:39,170 --> 00:02:42,840
So we need to activate the authentication.

38
00:02:42,890 --> 00:02:43,520
So that's right.

39
00:02:43,550 --> 00:02:46,500
Over here above our database we're going to come into it.

40
00:02:46,850 --> 00:02:52,770
And here we're going to go to the seinen method all right and firebase.

41
00:02:52,970 --> 00:03:00,740
The SDK provides API is to integrate with a whole bunch of different types of providers.

42
00:03:00,740 --> 00:03:05,640
We have email phone Google Facebook Twitter Khattab and anonymous.

43
00:03:05,870 --> 00:03:11,780
And so you can use any of these two off hand logging users for this course.

44
00:03:11,780 --> 00:03:18,620
We're going to focus on the e-mail and password as that is one of the most commonly used types of authentication.

45
00:03:18,710 --> 00:03:24,200
So just go ahead and click right here and check enable and to save.

46
00:03:24,200 --> 00:03:28,170
All right so now we have e-mail and password enabled on our project.

47
00:03:28,400 --> 00:03:31,040
So now let's jump back into the X code.

48
00:03:31,070 --> 00:03:31,370
All right.

49
00:03:31,370 --> 00:03:36,170
So to sign the user and we need to first have an existing user right.

50
00:03:36,170 --> 00:03:40,280
So let's we're going to first start in the create user of these.

51
00:03:40,400 --> 00:03:45,900
So that's over here and our controller folder and create user Visi Swift.

52
00:03:46,260 --> 00:03:52,750
You can just talk a little bit real quick about kind of the process and flow of logging a user in.

53
00:03:52,760 --> 00:03:53,000
All right.

54
00:03:53,000 --> 00:03:58,120
So to sign a user into the app you first get the authentication credentials from the user.

55
00:03:58,220 --> 00:04:01,950
And in our case this will be the email and the password.

56
00:04:02,000 --> 00:04:10,070
If we were using Twitter or Facebook or Google Plus we would get an off token from that provider.

57
00:04:10,370 --> 00:04:16,160
And then you pass these credentials to the firebase authentication STK the firebase back in services

58
00:04:16,430 --> 00:04:21,150
then verify those credentials and return a response to the client.

59
00:04:21,340 --> 00:04:27,800
And then after a successful sign and we have access to the user's basic profile information and also

60
00:04:27,800 --> 00:04:32,600
we can control the users access to our database with security rules.

61
00:04:32,600 --> 00:04:36,410
All right so let's get to it to create a user.

62
00:04:36,410 --> 00:04:38,110
It's pretty simple.

63
00:04:38,310 --> 00:04:40,410
Well first we need to import firebase here.

64
00:04:40,660 --> 00:04:42,770
Say import

65
00:04:45,590 --> 00:04:50,530
firebase and then we're going to say I'm off.

66
00:04:50,780 --> 00:04:57,320
OK so off the base class for firebase authentication it manages the authentication for firebase apps.

67
00:04:57,400 --> 00:05:03,670
So we'll say off dot and we access the instance of it and then we say to create user.

68
00:05:03,670 --> 00:05:04,140
OK.

69
00:05:04,430 --> 00:05:05,690
And this is the one we want here.

70
00:05:05,720 --> 00:05:07,910
We create a user with an e-mail.

71
00:05:08,160 --> 00:05:16,490
OK I'm just going to put a empty string there for now and then our completion going to press Return

72
00:05:16,490 --> 00:05:17,560
to get our completion.

73
00:05:17,570 --> 00:05:22,640
And inside of our response we get an optional user and an optional error.

74
00:05:22,700 --> 00:05:25,820
Kips want to rename those user and error.

75
00:05:26,230 --> 00:05:31,850
All right and let's handle the first one to say if left ever is equal to error.

76
00:05:32,270 --> 00:05:35,560
So if there is an error then we're just going to debug printed.

77
00:05:35,600 --> 00:05:37,310
So when I say Debug.

78
00:05:37,580 --> 00:05:48,680
Print and we're going to say error creating user and then do a string interpellation and we will print

79
00:05:48,680 --> 00:05:51,990
out the error dot.

80
00:05:52,150 --> 00:05:53,810
That should be error right there.

81
00:05:53,810 --> 00:05:54,770
There we go.

82
00:05:55,780 --> 00:05:58,210
Griddata localized description.

83
00:05:59,300 --> 00:06:04,490
Get snow if there wasn't an error and we were successful we created a user rights and also talk a little

84
00:06:04,490 --> 00:06:11,670
bit about the user object the user instance the user has a fixed set of basic properties.

85
00:06:11,780 --> 00:06:14,800
It has a unique id called the ID.

86
00:06:15,080 --> 00:06:20,470
And in the case of logging in with e-mail the primary e-mail with which it was logged in.

87
00:06:20,720 --> 00:06:21,770
And that's pretty much it.

88
00:06:21,770 --> 00:06:25,370
There's not a whole lot of other information that you can access or even add.

89
00:06:25,370 --> 00:06:27,520
There are a couple of other things that we can add.

90
00:06:27,580 --> 00:06:34,580
I'm using a transaction that we're going to look into here in a second we can add a display name and

91
00:06:34,580 --> 00:06:36,140
a photo you are l.

92
00:06:36,200 --> 00:06:36,410
All right.

93
00:06:36,410 --> 00:06:37,460
But that's pretty much it.

94
00:06:37,760 --> 00:06:45,500
If you want to add other things like say age employment occupation I or whatever other things you might

95
00:06:45,710 --> 00:06:48,330
need to store for a user.

96
00:06:48,410 --> 00:06:56,810
You would have to create a new document in a collection and you would want to put put as the ID for

97
00:06:56,810 --> 00:07:00,420
that document the ID for that user.

98
00:07:00,500 --> 00:07:00,770
OK.

99
00:07:00,800 --> 00:07:03,480
So we're going to cover both of those instances right here.

100
00:07:04,050 --> 00:07:05,260
All right so we've created a user.

101
00:07:05,270 --> 00:07:11,890
Let's go ahead and add a display name so that we can have a user name to use throughout our app.

102
00:07:11,950 --> 00:07:12,380
OK.

103
00:07:12,560 --> 00:07:18,500
So I want to say what we got is we got to create a change request because when we see that change request

104
00:07:18,890 --> 00:07:24,810
equal I'm going to say user dot create profile change request and.

105
00:07:25,070 --> 00:07:29,700
Says This creates an object which may be used to change the user profile data.

106
00:07:29,850 --> 00:07:36,080
And then we are going to say change request to display name and you'll see here that the other option

107
00:07:36,110 --> 00:07:38,140
is photo you URL.

108
00:07:38,220 --> 00:07:47,800
OK so a change request that display display name and that is going to be equal to the user name.

109
00:07:47,840 --> 00:07:50,310
But we need to actually define that don't we.

110
00:07:50,510 --> 00:07:56,720
So here let's go ahead and create our variable's for the e-mail password and username that we get from

111
00:07:56,720 --> 00:07:58,100
our i'd be outlet's up here.

112
00:07:58,220 --> 00:08:07,880
So I'm just going to say God let email people he mail text that text and then you can actually chain

113
00:08:08,380 --> 00:08:08,910
guards.

114
00:08:08,920 --> 00:08:17,370
So I'm going say comma and I'm going to say that password equal password text that text when I miss

115
00:08:17,390 --> 00:08:27,270
a comma and the user name equal username text dot text Ninnis else return OK.

116
00:08:27,380 --> 00:08:31,250
So if any of these fail then it's just going to return right out.

117
00:08:31,250 --> 00:08:31,870
All right.

118
00:08:31,970 --> 00:08:34,810
So let's see here.

119
00:08:34,820 --> 00:08:37,490
All right so we have created a change request.

120
00:08:37,490 --> 00:08:43,370
We are saying we're going to be updating the display name and then all we have to do is say Change Request

121
00:08:43,470 --> 00:08:49,820
tot commit changes and we get a completion Hendler and press enter and when you get an optional Harris

122
00:08:49,840 --> 00:08:55,820
when to say error in the code and here we're just going to debug print this as well so we'll see if

123
00:08:55,910 --> 00:09:01,200
that error is equal to error and then Debug.

124
00:09:01,200 --> 00:09:06,020
Print and I'm just going to print that right out.

125
00:09:06,180 --> 00:09:07,840
Localize description.

126
00:09:08,120 --> 00:09:14,280
OK so now we have created a user and we have added a display name with this code right here.

127
00:09:14,480 --> 00:09:19,930
And before I forget let's go ahead and add these parameters to our create user functions we are creating

128
00:09:19,940 --> 00:09:26,150
a user with email and the e-mail is this e-mail that we know very well that we created here which comes

129
00:09:26,150 --> 00:09:30,200
from the e-mail textfield and Pittsford here.

130
00:09:30,360 --> 00:09:36,360
Let's see here which is password password text just password.

131
00:09:36,410 --> 00:09:37,900
There we go.

132
00:09:37,910 --> 00:09:44,630
All right so this is how we can add a display ending but I said if you want to add additional information

133
00:09:44,720 --> 00:09:52,150
then we need to be able to create a new document in a new collection that that holds those are.

134
00:09:52,160 --> 00:09:59,450
So we're going to do is we are going to say fire store dot fire store and then we're going to need a

135
00:10:00,080 --> 00:10:07,880
reference to a collection and this collection doesn't exist yet but it is going to be called users.

136
00:10:08,040 --> 00:10:08,930
OK.

137
00:10:09,440 --> 00:10:14,090
And because we don't like to have just magic strings laying around we're going to copy this and we're

138
00:10:14,090 --> 00:10:23,930
going to go to our Constans folder where you at utilities Constans file and I'm going to create a new

139
00:10:24,410 --> 00:10:27,940
ref here going to say Commons or a user's ref actually.

140
00:10:28,370 --> 00:10:38,420
Users are ref is equal to users getting a say that jump back in to create users and then replace this

141
00:10:38,420 --> 00:10:41,200
with users ref.

142
00:10:41,460 --> 00:10:42,000
OK.

143
00:10:42,140 --> 00:10:44,870
So we are referencing a new collection that we haven't created yet.

144
00:10:45,140 --> 00:10:52,130
And then instead of using add a document here like we have done in the past which generates an auto

145
00:10:52,130 --> 00:11:01,790
generated document ID what we want to do is we want to set the document ID equal to the user's ID.

146
00:11:01,790 --> 00:11:06,680
That way in the future when we want to reference this document we can just grab it by the user's ID.

147
00:11:06,680 --> 00:11:07,160
All right.

148
00:11:07,370 --> 00:11:10,440
So the way to do that is we actually say document.

149
00:11:11,090 --> 00:11:20,030
And then we explicitly set the the document name the document ID in the document ID is the user ID.

150
00:11:20,150 --> 00:11:27,710
Now the user ID is optional so I'm going to do a guard str. here in this regard that user id equal user

151
00:11:28,520 --> 00:11:32,120
that you ID else.

152
00:11:33,330 --> 00:11:34,270
Return.

153
00:11:34,840 --> 00:11:43,030
So now we have the user id and we were and said that as the document Id say user ID and then we say

154
00:11:43,030 --> 00:11:49,560
Dot set data and we want the one with the completion handler right here.

155
00:11:50,300 --> 00:11:56,200
All right so a return on the data then tab over to the completion handler and return on that and say

156
00:11:56,530 --> 00:12:00,350
error right there and return on the code and enter.

157
00:12:00,700 --> 00:12:00,970
All right.

158
00:12:00,970 --> 00:12:06,520
So I'm just going to put in a couple of fields here you can add anything that you would like like the

159
00:12:06,520 --> 00:12:11,140
age occupation hobbies whatever whatever you want.

160
00:12:11,140 --> 00:12:16,310
I'm going to put in fields for the username and the time the date created OK.

161
00:12:16,420 --> 00:12:17,680
Basically a time stamp.

162
00:12:17,950 --> 00:12:22,360
So I'm going to write here I'm going to do enter to make this a little bit more readable and then enter

163
00:12:22,360 --> 00:12:23,380
again.

164
00:12:23,380 --> 00:12:30,570
And so here for the field name I'm going to say user name which is a constant that we already have.

165
00:12:30,880 --> 00:12:39,140
And then here I'm going to put in the user name which we created up above here.

166
00:12:40,060 --> 00:12:43,530
And then the second field I'm going to I'm going to create a new constant.

167
00:12:43,720 --> 00:12:49,920
And just because I'm one is referring to a user time stamp doesn't really quite sound right.

168
00:12:50,020 --> 00:12:58,790
So I'm going to say let the date created equal date created.

169
00:12:58,830 --> 00:13:03,150
Get that on a copy constant and jump back into our accrete user visi.

170
00:13:03,160 --> 00:13:10,010
And here we are at a pace that and the value is field value dot servers timestamp.

171
00:13:10,300 --> 00:13:16,570
And then in our completion we're just going to see if left error is equal to error if there isn't error

172
00:13:16,650 --> 00:13:18,910
that we are going to debug.

173
00:13:18,910 --> 00:13:21,440
Print that errors when we say error.

174
00:13:21,760 --> 00:13:28,630
Localized description if there is not an error then we are going to actually just dismiss this view

175
00:13:28,630 --> 00:13:32,250
controller and go back to the log in your control and then we'll log in there.

176
00:13:32,260 --> 00:13:40,660
So I would say self-taught dismiss animated is true and completion is now looks good.

177
00:13:40,810 --> 00:13:47,960
And then if we can't sell it then we're going to do this self-taught dismiss animated true completion.

178
00:13:47,980 --> 00:13:52,120
No actually here I don't think we need the self.

179
00:13:52,310 --> 00:13:58,150
Are you going to save this and we will quickly run through what we have covered so far.

180
00:13:58,390 --> 00:14:04,320
So we have activated the e-mail password authentication in our firebase account.

181
00:14:04,420 --> 00:14:11,860
We have added our authentication create user function here we pass in an email and a password.

182
00:14:12,250 --> 00:14:18,040
If there is not an error then the first thing we're going to do is going to go ahead and add a property

183
00:14:18,040 --> 00:14:22,420
to our user We're going to add a display name so that we can reference a username throughout the rest

184
00:14:22,420 --> 00:14:29,590
of the app and then assuming that there is additional information that we want to store for each user

185
00:14:29,590 --> 00:14:31,470
when they are created.

186
00:14:31,490 --> 00:14:39,400
We are adding a document where the document ID is set to the user's ID so that it will be easily accessible

187
00:14:39,820 --> 00:14:42,670
later on if we need it and then we set the data.

188
00:14:42,820 --> 00:14:44,890
And when you set data.

189
00:14:45,010 --> 00:14:49,650
If the document does not exist it will create the document.

190
00:14:50,140 --> 00:14:57,220
And then if everything goes according to plan we just dismiss the create user Visi and go back to the

191
00:14:57,220 --> 00:15:00,920
log in VC after successfully creating a user.

192
00:15:01,210 --> 00:15:01,450
All right.

193
00:15:01,490 --> 00:15:07,720
So the next lesson we were going to cover logging in after we have created a user and will go on from

194
00:15:07,720 --> 00:15:07,930
there.

195
00:15:07,920 --> 00:15:08,190
Right.

196
00:15:08,250 --> 00:15:08,790
So even.
