Skip to content

Commit 0303860

Browse files
committed
Fixed anonymously login on iOS
1 parent a2cd599 commit 0303860

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

ios/Firestack/FirestackAuth.m

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,36 @@ @implementation FirestackAuth
1919
RCT_EXPORT_METHOD(signInAnonymously:
2020
(RCTResponseSenderBlock) callBack)
2121
{
22-
22+
@try {
2323
[[FIRAuth auth] signInAnonymouslyWithCompletion
2424
:^(FIRUser *user, NSError *error) {
2525
if (!user) {
2626
NSDictionary *evt = @{
27-
@"eventName": @"SignInAnonymouslyError",
28-
@"msg": [error localizedFailureReason]
27+
@"eventName": AUTH_ANONYMOUS_ERROR_EVENT,
28+
@"msg": [error localizedDescription]
2929
};
3030

31-
[self sendJSEvent:AUTH_CHANGED_EVENT props: evt];
31+
32+
[self sendJSEvent:AUTH_CHANGED_EVENT
33+
props: evt];
34+
3235
callBack(@[evt]);
33-
return;
34-
}
35-
NSDictionary *userProps = [self userPropsFromFIRUser:user];
36-
callBack(@[[NSNull null], userProps]);
36+
} else {
37+
NSDictionary *userProps = [self userPropsFromFIRUser:user];
38+
callBack(@[[NSNull null], userProps]);
39+
}
3740
}];
41+
} @catch(NSException *ex) {
42+
NSDictionary *eventError = @{
43+
@"eventName": AUTH_ANONYMOUS_ERROR_EVENT,
44+
@"msg": ex.reason
45+
};
46+
47+
[self sendJSEvent:AUTH_ERROR_EVENT
48+
props:eventError];
49+
NSLog(@"An exception occurred: %@", ex);
50+
callBack(@[eventError]);
51+
}
3852
}
3953

4054
RCT_EXPORT_METHOD(signInWithCustomToken:
@@ -50,7 +64,7 @@ @implementation FirestackAuth
5064
callback(@[[NSNull null], userProps]);
5165
} else {
5266
NSDictionary *err =
53-
[FirestackErrors handleFirebaseError:@"signinError"
67+
[FirestackErrors handleFirebaseError:AUTH_ERROR_EVENT
5468
error:error
5569
withUser:user];
5670
callback(@[err]);
@@ -164,6 +178,11 @@ @implementation FirestackAuth
164178
}
165179
}
166180

181+
// Helper
182+
- (Boolean) listeningForAuth {
183+
return (self->authListenerHandle != nil);
184+
}
185+
167186
RCT_EXPORT_METHOD(getCurrentUser:(RCTResponseSenderBlock)callback)
168187
{
169188
FIRUser *user = [FIRAuth auth].currentUser;
@@ -470,15 +489,17 @@ - (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider
470489

471490
// Not sure how to get away from this... yet
472491
- (NSArray<NSString *> *)supportedEvents {
473-
return @[AUTH_CHANGED_EVENT];
492+
return @[AUTH_CHANGED_EVENT, AUTH_ANONYMOUS_ERROR_EVENT, AUTH_ERROR_EVENT];
474493
}
475494

476495
- (void) sendJSEvent:(NSString *)title
477496
props:(NSDictionary *)props
478497
{
479498
@try {
499+
if ([self listeningForAuth]) {
480500
[self sendEventWithName:title
481501
body:props];
502+
}
482503
}
483504
@catch (NSException *err) {
484505
NSLog(@"An error occurred in sendJSEvent: %@", [err debugDescription]);

ios/Firestack/FirestackEvents.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ static NSString *const kFirestackInitialized = @"FirestackInitializedEvent";
2222
static NSString *const INITIALIZED_EVENT = @"firestackInitialized";
2323

2424
static NSString *const AUTH_CHANGED_EVENT = @"listenForAuth";
25+
static NSString *const AUTH_ERROR_EVENT = @"authError";
26+
static NSString *const AUTH_ANONYMOUS_ERROR_EVENT = @"authAnonymousError";
2527
static NSString *const DEBUG_EVENT = @"debug";
2628

2729
// Database

lib/modules/authentication.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export class Authentication extends Base {
1313
this._addToFirestackInstance(
1414
'listenForAuth', 'unlistenForAuth',
1515
'createUserWithEmail', 'signInWithEmail',
16+
'signInAnonymously',
1617
'signInWithProvider', 'signInWithCustomToken',
1718
'reauthenticateWithCredentialForProvider',
1819
'updateUserEmail', 'updatePassword',
@@ -78,6 +79,14 @@ export class Authentication extends Base {
7879
return promisify('signInWithCustomToken', FirestackAuth)(customToken)
7980
}
8081

82+
/**
83+
* Sign a user in anonymously
84+
* @return {Promise} A promise resolved upon completion
85+
*/
86+
signInAnonymously() {
87+
return promisify('signInAnonymously', FirestackAuth)();
88+
}
89+
8190
/**
8291
* Reauthenticate a user with a third-party authentication provider
8392
* @param {string} provider The provider name

0 commit comments

Comments
 (0)