Skip to content

Commit 51076f3

Browse files
Handle login better on iOS
1 parent 37f0739 commit 51076f3

3 files changed

Lines changed: 77 additions & 37 deletions

File tree

plugin.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@
8989
<header-file src="src/ios/FirebaseUIAuthPlugin.h"/>
9090
<source-file src="src/ios/FirebaseUIAuthPlugin.m"/>
9191

92-
<framework src="FirebaseUI/Auth" type="podspec" spec="~> 5.0.0"/>
93-
<framework src="FirebaseUI/Google" type="podspec" spec="~> 5.0.0"/>
94-
<framework src="FirebaseUI/Facebook" type="podspec" spec="~> 5.0.0"/>
92+
<framework src="FirebaseUI/Auth" type="podspec" spec=""/>
93+
<framework src="FirebaseUI/Google" type="podspec" spec=""/>
94+
<framework src="FirebaseUI/Facebook" type="podspec" spec=""/>
9595
</platform>
9696

9797
<platform name="android">

src/ios/FirebaseUIAuthPlugin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
- (void)initialise:(CDVInvokedUrlCommand *)command;
99
- (void)signIn:(CDVInvokedUrlCommand *)command;
10+
- (void)signInAnonymously:(CDVInvokedUrlCommand *)command;
1011
- (void)signOut:(CDVInvokedUrlCommand *)command;
1112
- (void)getToken:(CDVInvokedUrlCommand *)command;
1213
- (void)deleteUser:(CDVInvokedUrlCommand *)command;

src/ios/FirebaseUIAuthPlugin.m

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ - (void)initialise:(CDVInvokedUrlCommand *)command {
3535
if ([anonymous isEqualToNumber:[NSNumber numberWithBool:YES]]) {
3636
self.anonymous = true;
3737
}
38-
39-
// [[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth * _Nonnull auth, FIRUser * _Nullable user) {
40-
// if (user != nil) {
41-
// [self raiseEventForUser:user];
42-
// } else {
43-
// [self raiseEvent:@"signoutsuccess" withData:nil];
44-
// }
45-
// }];
46-
47-
// [self signInAnonymous];
4838
}
4939
@catch (NSException *exception) {
5040
NSLog(@"Initialise error %@", [exception reason]);
@@ -162,6 +152,79 @@ - (void)signIn:(CDVInvokedUrlCommand *)command {
162152
}
163153
}
164154

155+
- (void)authUI:(FUIAuth *)authUI didSignInWithAuthDataResult:(nullable FIRAuthDataResult *)authDataResult
156+
error:(nullable NSError *)error {
157+
158+
NSDictionary *data = nil;
159+
160+
if (error == nil) {
161+
[self raiseEventForUser:authDataResult.user];
162+
} else {
163+
164+
if (error.code == FUIAuthErrorCodeUserCancelledSignIn) {
165+
166+
[self signInAnonymous];
167+
[self raiseEvent:@"signinaborted" withData:data];
168+
169+
return;
170+
}
171+
172+
if (error.code == FUIAuthErrorCodeMergeConflict) {
173+
FIRAuthCredential *authCredential = [error.userInfo valueForKey:FUIAuthCredentialKey];
174+
175+
if (authCredential != nil) {
176+
[[FIRAuth auth] signInAndRetrieveDataWithCredential:authCredential completion:^(FIRAuthDataResult * _Nullable authResult, NSError * _Nullable error) {
177+
if ([authResult user] != nil) {
178+
[self raiseEventForUser:[authResult user]];
179+
} else if (error != nil) {
180+
NSDictionary *data = nil;
181+
182+
if (error.localizedFailureReason != nil && error.localizedDescription != nil) {
183+
data = @{
184+
@"code" : [error localizedFailureReason],
185+
@"message" : [error localizedDescription]
186+
};
187+
} else {
188+
189+
data = @{
190+
@"code" : @-1,
191+
@"message" : @"Unknown failure reason"
192+
};
193+
}
194+
195+
[self raiseEvent:@"signinfailure" withData:data];
196+
}
197+
}];
198+
}
199+
} else {
200+
if (error.localizedFailureReason != nil && error.localizedDescription != nil) {
201+
data = @{
202+
@"code" : [error localizedFailureReason],
203+
@"message" : [error localizedDescription]
204+
};
205+
} else {
206+
207+
data = @{
208+
@"code" : @1,
209+
@"message" : @"Unknown failure reason"
210+
};
211+
}
212+
213+
[self raiseEvent:@"signinfailure" withData:data];
214+
}
215+
}
216+
}
217+
218+
- (void)signInAnonymously:(CDVInvokedUrlCommand *)command {
219+
@try {
220+
[self signInAnonymous];
221+
}
222+
@catch (NSException *exception) {
223+
NSLog(@"SignOut error %@", [exception reason]);
224+
@throw exception;
225+
}
226+
}
227+
165228
- (BOOL)application:(UIApplication *)app
166229
openURL:(NSURL *)url
167230
options:(NSDictionary *)options {
@@ -251,30 +314,6 @@ - (void)reloadUser:(CDVInvokedUrlCommand *)command {
251314
}
252315
}
253316

254-
- (void)authUI:(nonnull FUIAuth *)authUI didSignInWithUser:(nullable FIRUser *)user error:(nullable NSError *)error {
255-
if (error == nil) {
256-
[self raiseEventForUser:user];
257-
} else {
258-
259-
NSDictionary *data = nil;
260-
261-
if (error.localizedFailureReason != nil && error.localizedDescription != nil) {
262-
data = @{
263-
@"code" : [error localizedFailureReason],
264-
@"message" : [error localizedDescription]
265-
};
266-
} else {
267-
268-
data = @{
269-
@"code" : @1,
270-
@"message" : @"Unknown failure reason"
271-
};
272-
}
273-
274-
[self raiseEvent:@"signinfailure" withData:data];
275-
}
276-
}
277-
278317
- (void)raiseEventForUser:(FIRUser *)user {
279318

280319
NSDictionary *result;

0 commit comments

Comments
 (0)