Skip to content

Commit 31ac4d0

Browse files
committed
Updates, including with FirestackModule
1 parent 4fadbb3 commit 31ac4d0

File tree

6 files changed

+248
-144
lines changed

6 files changed

+248
-144
lines changed

firestack.ios.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* @providesModule Firestack
33
* @flow
44
*/
5-
const FirebaseManager = require('firebase');
5+
const firebase = require('firebase');
66

77
const app = require('firebase/app');
88
const db = require('firebase/database');
99
const storage = require('firebase/storage');
1010

11-
import {NativeModules, NativeAppEventEmitter} from 'react-native';
11+
import {NativeModules, NativeAppEventEmitter, AsyncStorage} from 'react-native';
1212
const FirebaseHelper = NativeModules.Firestack;
1313

1414
import promisify from './lib/promisify'
@@ -24,6 +24,7 @@ export class Firestack {
2424

2525
this.configured = false;
2626
this._debug = options.debug || false;
27+
this.auth = null;
2728

2829
this.eventHandlers = {};
2930

@@ -34,6 +35,7 @@ export class Firestack {
3435
opts = opts || {};
3536
const firestackOptions = Object.assign({}, this.options, opts);
3637
this.appInstance = app.initializeApp(firestackOptions);
38+
3739
return promisify('configureWithOptions')(firestackOptions)
3840
.then((...args) => {
3941
this.configured = true;
@@ -43,6 +45,10 @@ export class Firestack {
4345

4446
// Auth
4547
listenForAuth(callback) {
48+
this.on('listenForAuth', auth => {
49+
this._updateUserTokenFromPromise(Promise.resolve(auth));
50+
});
51+
4652
const sub = this.on('listenForAuth', callback);
4753
FirebaseHelper.listenForAuth();
4854
return promisify(() => sub)(sub);
@@ -76,7 +82,7 @@ export class Firestack {
7682
* @return {Promise} A promise that is resolved upon completion
7783
*/
7884
signInWithEmail(email, password) {
79-
return promisify('signInWithEmail')(email, password);
85+
return this._updateUserTokenFromPromise(promisify('signInWithEmail')(email, password))
8086
}
8187

8288
/**
@@ -87,7 +93,7 @@ export class Firestack {
8793
* @return {Promise} A promise resolved upon completion
8894
*/
8995
signInWithProvider(provider, authToken, authSecret) {
90-
return promisify('signInWithProvider')(provider, authToken, authSecret);
96+
return this._updateUserTokenFromPromise(promisify('signInWithProvider')(provider, authToken, authSecret))
9197
}
9298

9399
/**
@@ -96,7 +102,7 @@ export class Firestack {
96102
* @return {Promise} A promise resolved upon completion
97103
*/
98104
signInWithCustomToken(customToken) {
99-
return promisify('signInWithCustomToken')(customToken);
105+
return this._updateUserTokenFromPromise(promisify('signInWithCustomToken')(customToken))
100106
}
101107

102108
/**
@@ -107,9 +113,12 @@ export class Firestack {
107113
* @return {Promise} A promise resolved upon completion
108114
*/
109115
reauthenticateWithCredentialForProvider(provider, token, secret) {
110-
return promisify('reauthenticateWithCredentialForProvider')(provider, token, secret);
116+
return this._updateUserTokenFromPromise(promisify('reauthenticateWithCredentialForProvider')(provider, token, secret))
111117
}
112118

119+
_updateUserTokenFromPromise(promise) {
120+
return promise;
121+
}
113122

114123
/**
115124
* Update the current user's email
@@ -213,7 +222,7 @@ export class Firestack {
213222
// database
214223
get database() {
215224
db.enableLogging(this._debug);
216-
return db()
225+
return this.appInstance.database();
217226
}
218227

219228
/**
@@ -239,6 +248,13 @@ export class Firestack {
239248
return this.remoteConfig;
240249
}
241250

251+
/**
252+
* app instance
253+
**/
254+
get app() {
255+
return this.appInstance;
256+
}
257+
242258
/**
243259
* Redux store
244260
**/

ios/Firestack/Firestack.m

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ @implementation Firestack
1212

1313
@synthesize bridge = _bridge;
1414

15+
typedef void (^UserWithTokenResponse)(NSDictionary *, NSError *);
16+
1517
RCT_EXPORT_MODULE(Firestack);
1618

1719
RCT_EXPORT_METHOD(configureWithOptions:(NSDictionary *) opts
@@ -98,6 +100,28 @@ @implementation Firestack
98100
callback:callback];
99101
}
100102

103+
104+
RCT_EXPORT_METHOD(signInWithCustomToken:
105+
(NSString *)customToken
106+
callback:(RCTResponseSenderBlock) callback)
107+
{
108+
[[FIRAuth auth]
109+
signInWithCustomToken:customToken
110+
completion:^(FIRUser *user, NSError *error) {
111+
112+
if (user != nil) {
113+
NSDictionary *userProps = [self userPropsFromFIRUser:user];
114+
callback(@[[NSNull null], userProps]);
115+
} else {
116+
NSDictionary *err =
117+
[self handleFirebaseError:@"signinError"
118+
error:error
119+
withUser:user];
120+
callback(@[err]);
121+
}
122+
}];
123+
}
124+
101125
RCT_EXPORT_METHOD(signInWithProvider:
102126
(NSString *)provider
103127
token:(NSString *)authToken
@@ -114,8 +138,6 @@ @implementation Firestack
114138
return callback(@[err]);
115139
}
116140

117-
NSLog(@"signinWithCredential: %@", credential);
118-
119141
@try {
120142
[[FIRAuth auth] signInWithCredential:credential
121143
completion:^(FIRUser *user, NSError *error) {
@@ -164,13 +186,21 @@ @implementation Firestack
164186

165187
if (user != nil) {
166188
// User is signed in.
167-
NSDictionary *userProps = [self userPropsFromFIRUser:user];
168-
// callback(@[[NSNull null], userProps]);
169-
[self sendJSEvent:@"listenForAuth" props: @{
170-
@"eventName": @"user",
171-
@"authenticated": @(true),
172-
@"user": userProps
173-
}];
189+
[self userPropsFromFIRUserWithToken:user
190+
andCallback:^(NSDictionary *userProps, NSError * error) {
191+
if (error != nil) {
192+
[self sendJSEvent:@"listenForAuth" props: @{
193+
@"eventName": @"userTokenError",
194+
@"msg": [error localizedFailureReason]
195+
}];
196+
} else {
197+
[self sendJSEvent:@"listenForAuth" props: @{
198+
@"eventName": @"user",
199+
@"authenticated": @(true),
200+
@"user": userProps
201+
}];
202+
}
203+
}];
174204
} else {
175205
// TODO: Update this with different error states
176206
NSDictionary *err = @{
@@ -242,7 +272,10 @@ @implementation Firestack
242272
completion:^(FIRUser *user, NSError *error) {
243273
if (user != nil) {
244274
NSDictionary *userProps = [self userPropsFromFIRUser:user];
245-
callback(@[[NSNull null], userProps]);
275+
276+
callback(@[[NSNull null], @{
277+
@"user": userProps
278+
}]);
246279
} else {
247280
NSDictionary *err =
248281
[self handleFirebaseError:@"signinError"
@@ -347,7 +380,8 @@ @implementation Firestack
347380
withUser:user];
348381
callback(@[err]);
349382
} else {
350-
callback(@[[NSNull null], @{@"token": token}]);
383+
NSDictionary *userProps = [self userPropsFromFIRUser:user];
384+
callback(@[[NSNull null], @{@"token": token, @"user": userProps}]);
351385
}
352386
}];
353387
}
@@ -359,12 +393,13 @@ @implementation Firestack
359393
[user getTokenWithCompletion:^(NSString *token , NSError *_Nullable error) {
360394
if (error) {
361395
NSDictionary *err =
362-
[self handleFirebaseError:@"deleteUserError"
396+
[self handleFirebaseError:@"getTokenWithCompletion"
363397
error:error
364398
withUser:user];
365399
callback(@[err]);
366400
} else {
367-
callback(@[[NSNull null], @{@"result": token}]);
401+
NSDictionary *userProps = [self userPropsFromFIRUser:user];
402+
callback(@[[NSNull null], @{@"token": token, @"user": userProps}]);
368403
}
369404
}];
370405
}
@@ -618,6 +653,8 @@ - (NSString *) getStorageUrl
618653
}];
619654
}
620655

656+
#pragma mark Database
657+
621658
#pragma mark Helpers
622659

623660
- (NSDictionary *) getConfig
@@ -667,7 +704,9 @@ - (NSDictionary *) userPropsFromFIRUser:(FIRUser *) user
667704
@"email": user.email ? user.email : @"",
668705
@"emailVerified": @(user.emailVerified),
669706
@"anonymous": @(user.anonymous),
670-
@"displayName": user.displayName ? user.displayName : @""
707+
@"displayName": user.displayName ? user.displayName : @"",
708+
@"refreshToken": user.refreshToken,
709+
@"providerID": user.providerID
671710
} mutableCopy];
672711

673712
if ([user valueForKey:@"photoURL"] != nil) {
@@ -678,6 +717,20 @@ - (NSDictionary *) userPropsFromFIRUser:(FIRUser *) user
678717
return userProps;
679718
}
680719

720+
- (void) userPropsFromFIRUserWithToken:(FIRUser *) user
721+
andCallback:(UserWithTokenResponse) callback
722+
{
723+
NSMutableDictionary *userProps = [[self userPropsFromFIRUser:user] mutableCopy];
724+
[user getTokenWithCompletion:^(NSString * _Nullable token, NSError * _Nullable error) {
725+
if (error != nil) {
726+
return callback(nil, error);
727+
}
728+
729+
[userProps setValue:token forKey:@"idToken"];
730+
callback(userProps, nil);
731+
}];
732+
}
733+
681734
- (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider
682735
token:(NSString *)authToken
683736
secret:(NSString *)authTokenSecret

ios/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,6 @@ SPEC CHECKSUMS:
7474
GoogleSymbolUtilities: 33117db1b5f290c6fbf259585e4885b4c84b98d7
7575
GoogleUtilities: 56c5ac05b7aa5dc417a1bb85221a9516e04d7032
7676

77-
PODFILE CHECKSUM: 649189c2f148047c708e89261de1a907f7e44cf8
77+
PODFILE CHECKSUM: 7c7fd1e44ea19ff5da075fa03510b062b8fd32c2
7878

7979
COCOAPODS: 1.0.1

ios/Pods/Manifest.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)