@@ -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
166229openURL : (NSURL *)url
167230options : (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