1
00:00:05,190 --> 00:00:08,860
Everyone welcome back Anjani be here with soap's dot com.

2
00:00:08,880 --> 00:00:14,910
And in this lesson we are going to continue working on google log in with firebase and the last lesson

3
00:00:15,000 --> 00:00:19,770
we got to the point where we actually logging in with Google and now we need to work on integrating

4
00:00:19,770 --> 00:00:21,440
that with firebase.

5
00:00:21,450 --> 00:00:27,570
So the way to start working with that is we need to go back to our firebase console and here under authentication

6
00:00:28,120 --> 00:00:34,710
we are going to go to our seinen method and here under Google when you click on it and we are going

7
00:00:34,710 --> 00:00:41,670
to say enable and let's give this a public facing names want to save firebase social aren't going to

8
00:00:41,670 --> 00:00:42,950
save save.

9
00:00:43,380 --> 00:00:45,310
So now Google is enabled.

10
00:00:45,630 --> 00:00:53,050
And so next thing we need to do is jump back into X code and here in our view control it us with file.

11
00:00:53,100 --> 00:01:00,990
Like I said we're going to have a method where we pass into a firebase method the authentication credential

12
00:01:01,170 --> 00:01:11,120
for that platform's sign it it's wrong to call this phunk firebase log in and we're going to pass into

13
00:01:11,160 --> 00:01:22,590
it a credential credential type off credential.

14
00:01:23,940 --> 00:01:36,930
And then we're going to say off dot off dot sign in with a credential and we're going to call this credential

15
00:01:37,600 --> 00:01:44,210
in completion we are going to give back a user and the optional error.

16
00:01:47,440 --> 00:01:55,690
And so we will say if that error is equal to error then we will just print that AUSMIN see Debug.

17
00:01:55,750 --> 00:02:02,440
Print and we'll just print out every dot localized description and then we'll return out of this function

18
00:02:03,160 --> 00:02:05,070
and that's all we need for now.

19
00:02:05,470 --> 00:02:07,270
But where do we call this function from.

20
00:02:07,480 --> 00:02:15,070
So once in our app delegate that we are signed in with Google down here we are going to say we are going

21
00:02:15,070 --> 00:02:19,940
to get our authentication credential from the user right here.

22
00:02:20,230 --> 00:02:24,550
So we're going to say Well first we're going to get a reference to our view controllers and say guard

23
00:02:24,670 --> 00:02:39,760
alert controller people and Id sign in that shared instance that I delegate as the controller and this

24
00:02:39,760 --> 00:02:42,960
view controller is actually our view control it out with file here.

25
00:02:43,240 --> 00:02:47,050
So here we set our UI delegate equal to self's Are you a delegate.

26
00:02:47,050 --> 00:02:48,700
Is this view controller.

27
00:02:48,820 --> 00:02:54,410
And so here in app delegate We are just grabbing reference to the delegate and casting it as a controller

28
00:02:55,330 --> 00:03:03,230
and then we are saying well first weve got to do else return and then we need to get our authentications

29
00:03:03,240 --> 00:03:16,490
when we say God for that authentication equal user datt authentication else return and then we need

30
00:03:16,490 --> 00:03:26,450
to get our credential so we can say let credential equal Google or provider that credential with the

31
00:03:26,450 --> 00:03:35,360
ID token which is the authentication that we just got that ID token and the access token is authentication

32
00:03:36,170 --> 00:03:38,050
access token.

33
00:03:38,120 --> 00:03:46,700
All right so now that we have our credential now we call controller dot firebase logon and we pass into

34
00:03:46,700 --> 00:03:52,660
it the credential that we just got from the Google provider.

35
00:03:52,670 --> 00:03:58,070
So now once we click on the Google sign in button and we are successful we are going to get a reference

36
00:03:58,070 --> 00:04:04,400
to our view controller get the authentication which gives us the ID token and the access token which

37
00:04:04,400 --> 00:04:09,470
gives us our credential and then we pass that credential into our firebase luggin function which then

38
00:04:09,470 --> 00:04:13,400
takes that credential and uses it to sign into firebase.

39
00:04:13,430 --> 00:04:16,930
And if there is not a user yet we will create a user.

40
00:04:17,140 --> 00:04:19,110
All right so its going to check this out.

41
00:04:19,110 --> 00:04:25,280
So when I run it in I'm going to have the Safari open here as well with our users.

42
00:04:25,520 --> 00:04:34,870
And once we log in hopefully we get a user populated here and so when we click on seinen And let's see

43
00:04:34,870 --> 00:04:38,060
if let's make sure we don't have any errors in X code.

44
00:04:38,470 --> 00:04:39,630
Doesn't look like it.

45
00:04:39,730 --> 00:04:41,080
So if I refresh your

46
00:04:43,900 --> 00:04:44,800
oh sure enough.

47
00:04:44,800 --> 00:04:45,550
Here we go.

48
00:04:45,670 --> 00:04:49,230
We have a new user with our identifier.

49
00:04:49,240 --> 00:04:55,630
Johnny Depp's that column and it shows the provider which is Google and the user id so that is pretty

50
00:04:55,630 --> 00:04:56,360
cool.

51
00:04:56,740 --> 00:04:58,690
All right so we are logged in.

52
00:04:58,690 --> 00:05:02,760
How do we log out and sort of click on this button doesn't do anything right now.

53
00:05:03,200 --> 00:05:06,160
All right so we got our Iby action for the log out tap.

54
00:05:06,190 --> 00:05:07,180
So how we log out.

55
00:05:07,180 --> 00:05:13,990
Is First we're going to get a reference to our fire base office and we say let firebase off equal off

56
00:05:14,320 --> 00:05:19,500
that off and then to log out using firebase.

57
00:05:19,500 --> 00:05:30,570
And that requires a do try catch block when to say do and then we're going to try a catch that sign

58
00:05:30,570 --> 00:05:45,610
out error as in this error and we going to try to firebase off that dot sign out and here if we catch

59
00:05:45,610 --> 00:05:47,060
an error we are going to just print that.

60
00:05:47,060 --> 00:05:48,560
I'm going to say debug.

61
00:05:48,670 --> 00:05:49,190
Print.

62
00:05:49,330 --> 00:05:52,630
And we're going to say error.

63
00:05:52,720 --> 00:05:54,350
Signing out.

64
00:05:54,520 --> 00:05:57,430
And then we'll print out the sign out error.

65
00:05:58,990 --> 00:06:06,280
And then firebase has a nice little handler that is a state change listener that can keep track of changes

66
00:06:06,280 --> 00:06:08,700
in the author at authentications state.

67
00:06:08,890 --> 00:06:10,430
So Harinder view did.

68
00:06:10,560 --> 00:06:18,700
Let's go ahead and add that to say you did appear and we're going to add a state changelessness I will

69
00:06:18,700 --> 00:06:23,580
say off that dot add state to changelessness.

70
00:06:23,590 --> 00:06:23,830
Right.

71
00:06:23,860 --> 00:06:30,920
And we got a listener here and we can say off and user.

72
00:06:31,200 --> 00:06:37,810
And so this is going to be called anytime that someone logs out someone logs in refresh tokens you know

73
00:06:37,810 --> 00:06:39,150
all that kind of stuff.

74
00:06:39,150 --> 00:06:41,670
Any time that the state changes.

75
00:06:41,670 --> 00:06:42,500
This is called.

76
00:06:42,630 --> 00:06:51,480
So we're going to say if the user is equal to nil meaning no one is logged in then we are going to get

77
00:06:51,480 --> 00:06:53,760
our user info label that we have here.

78
00:06:53,910 --> 00:07:03,270
I'm going to say user label that text is equal to say no user logged in else.

79
00:07:03,270 --> 00:07:08,490
If someone is logged in then we're going to just print out that user.

80
00:07:08,490 --> 00:07:13,130
We're just going to print out the user id for the person that is logged in and of itself here.

81
00:07:13,170 --> 00:07:19,440
Since we're in said closure and here I would say again self-taught user info labeled that text is equal

82
00:07:19,440 --> 00:07:35,390
to say welcome user and then print out the user ID and if something's gone wrong and that doesn't exist

83
00:07:35,390 --> 00:07:37,940
we'll just do an empty string.

84
00:07:37,950 --> 00:07:38,410
All right.

85
00:07:38,420 --> 00:07:40,810
So let's go ahead and test this out.

86
00:07:42,150 --> 00:07:42,570
Right.

87
00:07:42,570 --> 00:07:48,240
So we were already logged in firebase takes care of that for us once we have logged in.

88
00:07:48,240 --> 00:07:50,470
And you remain logged in until you log out.

89
00:07:50,650 --> 00:07:53,720
And so it says Welcome user and the ID.

90
00:07:53,800 --> 00:07:59,730
So let's try this log out button and click and we have logged out and now says no user logged in.

91
00:07:59,980 --> 00:08:07,920
However if I click on this again then nothing happens because we only logged out of the firebase instance

92
00:08:07,930 --> 00:08:08,950
these are separate things.

93
00:08:08,960 --> 00:08:13,300
Firebase and the Google are separate.

94
00:08:13,300 --> 00:08:17,740
So we also need to be able to log out of the social platform.

95
00:08:17,740 --> 00:08:23,830
So we're going to create a new function to handle that because we are going to need to be able to log

96
00:08:23,830 --> 00:08:28,050
out of Facebook Google and Twitter.

97
00:08:28,120 --> 00:08:35,800
So we're going to create a function here that will check on the users the firebase users information

98
00:08:36,070 --> 00:08:39,940
and see which one that is currently logged in under which is the provider.

99
00:08:39,940 --> 00:08:44,620
So if we look in our database again here it says provider here is Google.

100
00:08:44,800 --> 00:08:49,240
And if we had Facebook one then the provider would be Facebook and Twitter one private provider would

101
00:08:49,240 --> 00:08:50,170
be Twitter.

102
00:08:50,170 --> 00:08:56,020
So we need to know which is the provider so that we can know how to which which social platform to log

103
00:08:56,020 --> 00:08:56,700
out of.

104
00:08:56,950 --> 00:09:05,940
So we're going to say fonk log out social And first we're going to get information for the users who

105
00:09:05,940 --> 00:09:11,330
are going to say Gurd that user is equal to off dot dot.

106
00:09:11,650 --> 00:09:13,340
Current User.

107
00:09:13,360 --> 00:09:15,410
Else return.

108
00:09:15,400 --> 00:09:16,600
All right we've got our user.

109
00:09:16,880 --> 00:09:20,210
So what we need to look at is the provider data.

110
00:09:20,620 --> 00:09:25,020
And that is in an dictionary of data.

111
00:09:25,030 --> 00:09:34,510
So we need to say for info in user that provider data.

112
00:09:35,340 --> 00:09:39,530
And now we're going to do a switch statement on the provider Heidi.

113
00:09:39,600 --> 00:09:49,080
So when I say switch info that provider ID and we're going to have three cases that we take a look at

114
00:09:49,200 --> 00:09:52,780
the Twitter Facebook and Google.

115
00:09:52,900 --> 00:10:03,680
So case of Google or provider then we are going to log out.

116
00:10:04,110 --> 00:10:13,810
But right now I'm just going to print say print Google and get the next case is going to be Twitter

117
00:10:13,880 --> 00:10:15,580
off provider.

118
00:10:15,640 --> 00:10:19,950
We want to hear off provider ID and we want the same for Google off provider.

119
00:10:19,980 --> 00:10:22,280
I d There we go.

120
00:10:23,280 --> 00:10:24,530
And here we're going to print

121
00:10:27,300 --> 00:10:28,360
Twitter.

122
00:10:28,860 --> 00:10:37,530
And then lastly we have a case for Facebook off provider ID and for that we will also print out just

123
00:10:37,980 --> 00:10:41,820
Facebook and then needs to be exhaustless will have a default

124
00:10:44,960 --> 00:10:46,910
which we will just break.

125
00:10:47,750 --> 00:10:48,500
All right very good.

126
00:10:48,620 --> 00:10:54,320
So now we are handling our three cases Google off Twitter off and Facebook off.

127
00:10:54,380 --> 00:10:59,800
So these are this is whether or not we have logged into firebase using each of these.

128
00:10:59,840 --> 00:11:08,910
So if we have logged in with Google provider what we need to do is we need to say Gee Id signed and

129
00:11:09,080 --> 00:11:18,480
shared instance that sign out and then appear where we log out before we sign out with firebase who

130
00:11:18,500 --> 00:11:19,870
will say log out.

131
00:11:19,880 --> 00:11:21,420
Social Okay.

132
00:11:21,650 --> 00:11:26,660
And depending on whichever social platform we have logged in with it will automatically log us out of

133
00:11:26,660 --> 00:11:29,690
it through the use of this function here.

134
00:11:29,720 --> 00:11:31,890
So is run this and test it out.

135
00:11:32,150 --> 00:11:34,820
All right I'm going to log out we have logged out.

136
00:11:34,820 --> 00:11:41,090
Both should have logged out both the firebase and Google so here it is Google which means that we got

137
00:11:41,090 --> 00:11:44,100
into our site out block right here.

138
00:11:44,360 --> 00:11:51,170
And so now if I click on seinen we should again yes go through the Google sign and process just like

139
00:11:51,170 --> 00:11:52,260
Jollibee again.

140
00:11:52,580 --> 00:11:57,320
And now we are back to being logged in we display our user info.

141
00:11:57,470 --> 00:11:58,670
So that is working good.

142
00:11:58,730 --> 00:12:03,460
I click log out then we are back to no use logged in and we can log back in again.

143
00:12:03,800 --> 00:12:04,880
All right very good.

144
00:12:04,940 --> 00:12:09,000
So congratulations you are now logging in with Google.

145
00:12:09,230 --> 00:12:15,620
But you may be wondering do I have to use this ID sign in button.

146
00:12:15,710 --> 00:12:21,260
And the answer is No you can use any button that you want and just to show you this quote and create

147
00:12:21,650 --> 00:12:22,830
a button real quick.

148
00:12:22,880 --> 00:12:30,200
So here I'm going to throw in a new button and put it right here going to constrain it.

149
00:12:30,610 --> 00:12:40,700
Pin it 20 to the left 8 from the button and above it and 20 from the right change the height to 40 and

150
00:12:40,700 --> 00:12:49,790
add four constraints we're going to change the text to log in Google and we're going to change text

151
00:12:49,790 --> 00:12:57,830
color to white and change the background color to this or red color which if you want to use it it is

152
00:12:57,830 --> 00:13:01,810
hecks color a four three three five.

153
00:13:02,330 --> 00:13:08,090
And I say go ahead and create an action for this real quick so I can hold the option and click on our

154
00:13:08,270 --> 00:13:13,290
controller to bring up the assistant editor and right here under the Google sign in Taft.

155
00:13:13,340 --> 00:13:23,950
I'm going to control and drag and creating new IAB action and then call this one custom Google tapped

156
00:13:24,480 --> 00:13:35,500
I'm going to connect close our assistant editor go into our view controller and hear all we need to

157
00:13:35,500 --> 00:13:44,520
do is the same thing that we did for our GED sign in Boston Id sign in our shared instance that sign.

158
00:13:44,570 --> 00:13:51,700
So if we were on this now we see our two buttons our Google sign in button as well as just a regular

159
00:13:51,700 --> 00:13:58,240
old custom button and you can customize this button any any way how you want but if we click on it then

160
00:13:58,240 --> 00:13:59,600
it should do the same thing.

161
00:13:59,620 --> 00:14:05,490
So we go continue and we can log in with our Google account and boom.

162
00:14:05,530 --> 00:14:08,230
All right so now we are logged in again.

163
00:14:08,440 --> 00:14:08,620
All right.

164
00:14:08,620 --> 00:14:16,260
So if you want to use the built in ID sign in but you can or you can create your own custom button.

165
00:14:16,270 --> 00:14:17,730
All right so there you have it.

166
00:14:17,770 --> 00:14:26,020
You are officially logging in with Google and then also using firebase to be able to have firebase users

167
00:14:26,020 --> 00:14:28,100
using that Google logon.

168
00:14:28,120 --> 00:14:30,380
All right so congratulations that is how you do it.

169
00:14:30,460 --> 00:14:34,180
And in the next lesson we will start working on Facebook logon.

170
00:14:34,180 --> 00:14:34,570
All right.

171
00:14:34,600 --> 00:14:35,270
So I'll see you then.
