Skip to content

Commit cd7d421

Browse files
committed
WIP: download test
2 parents 03b19fd + 0303860 commit cd7d421

File tree

13 files changed

+180
-100
lines changed

13 files changed

+180
-100
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,31 @@ Lastly, due to some dependencies requirements, Firestack supports iOS versions 8
200200

201201
### Android
202202

203-
There are several ways to setup Firebase on Android. The _easiest_ way is to pass the configuration settings in JavaScript. In that way, there is no setup for the native platform.
203+
There are several ways to setup Firebase on Android. The _easiest_ way is to pass the configuration settings in JavaScript. In that way, there is no setup for the native platform.
204204

205+
#### google-services.json setup
205206
If you prefer to include the default settings in the source of your app, download the `google-services.json` file provided by Firebase in the _Add Firebase to Android_ platform menu in your Firebase configuration console.
206207

208+
Next you'll have to add the google-services gradle plugin in order to parse it.
209+
210+
Add the google-services gradle plugin as a dependency in the *project* level build.gradle
211+
`android/build.gradle`
212+
```java
213+
buildscript {
214+
// ...
215+
dependencies {
216+
// ...
217+
classpath 'com.google.gms:google-services:3.0.0'
218+
}
219+
}
220+
```
221+
222+
In your app build.gradle file, add the gradle plugin at the VERY BOTTOM of the file (below all dependencies)
223+
`android/app/build.gradle`
224+
```java
225+
apply plugin: 'com.google.gms.google-services'
226+
```
227+
207228
## Usage
208229

209230
After creating a Firebase project and installing the library, we can use it in our project by importing the library in our JavaScript:

android/src/main/java/io/fullstack/firestack/FirestackAuth.java

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,22 +143,43 @@ public void signInWithProvider(final String provider, final String authToken, fi
143143
FirestackUtils.todoNote(TAG, "signInWithProvider", callback);
144144
}
145145

146+
@ReactMethod
147+
public void signInAnonymously(final Callback callback) {
148+
mAuth = FirebaseAuth.getInstance();
149+
150+
mAuth.signInAnonymously()
151+
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
152+
@Override
153+
public void onComplete(@NonNull Task<AuthResult> task) {
154+
Log.d(TAG, "signInAnonymously:onComplete:" + task.isSuccessful());
155+
156+
if (task.isSuccessful()) {
157+
user = task.getResult().getUser();
158+
userCallback(user, callback);
159+
}else{
160+
userErrorCallback(task, callback);
161+
}
162+
}
163+
});
164+
165+
}
166+
146167
@ReactMethod
147168
public void signInWithCustomToken(final String customToken, final Callback callback) {
148169
mAuth = FirebaseAuth.getInstance();
149170

150171
mAuth.signInWithCustomToken(customToken)
151172
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
152-
@Override
153-
public void onComplete(@NonNull Task<AuthResult> task) {
154-
Log.d(TAG, "signInWithCustomToken:onComplete:" + task.isSuccessful());
155-
if (task.isSuccessful()) {
156-
user = task.getResult().getUser();
157-
userCallback(user, callback);
158-
} else {
159-
userErrorCallback(task, callback);
173+
@Override
174+
public void onComplete(@NonNull Task<AuthResult> task) {
175+
Log.d(TAG, "signInWithCustomToken:onComplete:" + task.isSuccessful());
176+
if (task.isSuccessful()) {
177+
user = task.getResult().getUser();
178+
userCallback(user, callback);
179+
} else {
180+
userErrorCallback(task, callback);
181+
}
160182
}
161-
}
162183
});
163184
}
164185

android/src/main/java/io/fullstack/firestack/FirestackModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public String setKeyOrDefault(
6666
final String val = params.getString(key);
6767
Log.d(TAG, "Setting " + key + " from params to: " + val);
6868
return val;
69-
} else if (defaultValue != null && defaultValue != "") {
69+
} else if (defaultValue != null && !defaultValue.equals("")) {
7070
Log.d(TAG, "Setting " + key + " from params to: " + defaultValue);
7171
return defaultValue;
7272
} else {

android/src/main/java/io/fullstack/firestack/FirestackStorage.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,16 @@ public String getName() {
6060
}
6161

6262
@ReactMethod
63-
public void downloadUrl(final String storageUrl,
63+
public void downloadUrl(final String javascriptStorageBucket,
6464
final String path,
6565
final Callback callback) {
6666
FirebaseStorage storage = FirebaseStorage.getInstance();
67+
String storageBucket = storage.getApp().getOptions().getStorageBucket();
68+
String storageUrl = "gs://"+storageBucket;
6769
StorageReference storageRef = storage.getReferenceFromUrl(storageUrl);
68-
StorageReference fileRef = storageRef.child(path);
70+
StorageReference fileRef = storageRef.child(path);
6971

70-
Task<Uri> downloadTask = storageRef.getDownloadUrl();
72+
Task<Uri> downloadTask = fileRef.getDownloadUrl();
7173
downloadTask.addOnSuccessListener(new OnSuccessListener<Uri>() {
7274
@Override
7375
public void onSuccess(Uri uri) {

ios/Firestack.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
58B511F01A9E6C8500147676 /* Debug */ = {
245245
isa = XCBuildConfiguration;
246246
buildSettings = {
247+
CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES;
247248
DEFINES_MODULE = NO;
248249
EMBEDDED_CONTENT_CONTAINS_SWIFT = NO;
249250
ENABLE_BITCODE = YES;
@@ -272,6 +273,7 @@
272273
58B511F11A9E6C8500147676 /* Release */ = {
273274
isa = XCBuildConfiguration;
274275
buildSettings = {
276+
CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES;
275277
DEFINES_MODULE = NO;
276278
EMBEDDED_CONTENT_CONTAINS_SWIFT = NO;
277279
ENABLE_BITCODE = YES;

ios/Firestack/FirestackAuth.m

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,55 @@ @implementation FirestackAuth
1616

1717
RCT_EXPORT_MODULE(FirestackAuth);
1818

19+
RCT_EXPORT_METHOD(signInAnonymously:
20+
(RCTResponseSenderBlock) callBack)
21+
{
22+
@try {
23+
[[FIRAuth auth] signInAnonymouslyWithCompletion
24+
:^(FIRUser *user, NSError *error) {
25+
if (!user) {
26+
NSDictionary *evt = @{
27+
@"eventName": AUTH_ANONYMOUS_ERROR_EVENT,
28+
@"msg": [error localizedDescription]
29+
};
30+
31+
32+
[self sendJSEvent:AUTH_CHANGED_EVENT
33+
props: evt];
34+
35+
callBack(@[evt]);
36+
} else {
37+
NSDictionary *userProps = [self userPropsFromFIRUser:user];
38+
callBack(@[[NSNull null], userProps]);
39+
}
40+
}];
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+
}
52+
}
53+
1954
RCT_EXPORT_METHOD(signInWithCustomToken:
2055
(NSString *)customToken
2156
callback:(RCTResponseSenderBlock) callback)
2257
{
2358
[[FIRAuth auth]
2459
signInWithCustomToken:customToken
2560
completion:^(FIRUser *user, NSError *error) {
26-
61+
2762
if (user != nil) {
2863
NSDictionary *userProps = [self userPropsFromFIRUser:user];
2964
callback(@[[NSNull null], userProps]);
3065
} else {
3166
NSDictionary *err =
32-
[FirestackErrors handleFirebaseError:@"signinError"
67+
[FirestackErrors handleFirebaseError:AUTH_ERROR_EVENT
3368
error:error
3469
withUser:user];
3570
callback(@[err]);
@@ -52,7 +87,7 @@ @implementation FirestackAuth
5287
};
5388
return callback(@[err]);
5489
}
55-
90+
5691
@try {
5792
[[FIRAuth auth] signInWithCredential:credential
5893
completion:^(FIRUser *user, NSError *error) {
@@ -98,7 +133,7 @@ @implementation FirestackAuth
98133
self->authListenerHandle =
99134
[[FIRAuth auth] addAuthStateDidChangeListener:^(FIRAuth *_Nonnull auth,
100135
FIRUser *_Nullable user) {
101-
136+
102137
if (user != nil) {
103138
// User is signed in.
104139
[self userPropsFromFIRUserWithToken:user
@@ -143,10 +178,15 @@ @implementation FirestackAuth
143178
}
144179
}
145180

181+
// Helper
182+
- (Boolean) listeningForAuth {
183+
return (self->authListenerHandle != nil);
184+
}
185+
146186
RCT_EXPORT_METHOD(getCurrentUser:(RCTResponseSenderBlock)callback)
147187
{
148188
FIRUser *user = [FIRAuth auth].currentUser;
149-
189+
150190
if (user != nil) {
151191
NSDictionary *userProps = [self userPropsFromFIRUser:user];
152192
callback(@[[NSNull null], userProps]);
@@ -191,7 +231,7 @@ @implementation FirestackAuth
191231
completion:^(FIRUser *user, NSError *error) {
192232
if (user != nil) {
193233
NSDictionary *userProps = [self userPropsFromFIRUser:user];
194-
234+
195235
callback(@[[NSNull null], @{
196236
@"user": userProps
197237
}]);
@@ -209,7 +249,7 @@ @implementation FirestackAuth
209249
callback:(RCTResponseSenderBlock) callback)
210250
{
211251
FIRUser *user = [FIRAuth auth].currentUser;
212-
252+
213253
[user updateEmail:email completion:^(NSError *_Nullable error) {
214254
if (error) {
215255
// An error happened.
@@ -229,9 +269,9 @@ @implementation FirestackAuth
229269
RCT_EXPORT_METHOD(updateUserPassword:(NSString *)newPassword
230270
callback:(RCTResponseSenderBlock) callback)
231271
{
232-
272+
233273
FIRUser *user = [FIRAuth auth].currentUser;
234-
274+
235275
[user updatePassword:newPassword completion:^(NSError *_Nullable error) {
236276
if (error) {
237277
// An error happened.
@@ -251,7 +291,7 @@ @implementation FirestackAuth
251291
RCT_EXPORT_METHOD(sendPasswordResetWithEmail:(NSString *)email
252292
callback:(RCTResponseSenderBlock) callback)
253293
{
254-
294+
255295
[[FIRAuth auth] sendPasswordResetWithEmail:email
256296
completion:^(NSError *_Nullable error) {
257297
if (error) {
@@ -273,7 +313,7 @@ @implementation FirestackAuth
273313
RCT_EXPORT_METHOD(deleteUser:(RCTResponseSenderBlock) callback)
274314
{
275315
FIRUser *user = [FIRAuth auth].currentUser;
276-
316+
277317
[user deleteWithCompletion:^(NSError *_Nullable error) {
278318
if (error) {
279319
NSDictionary *err =
@@ -290,7 +330,7 @@ @implementation FirestackAuth
290330
RCT_EXPORT_METHOD(getToken:(RCTResponseSenderBlock) callback)
291331
{
292332
FIRUser *user = [FIRAuth auth].currentUser;
293-
333+
294334
[user getTokenWithCompletion:^(NSString *token, NSError *_Nullable error) {
295335
if (error) {
296336
NSDictionary *err =
@@ -308,7 +348,7 @@ @implementation FirestackAuth
308348
RCT_EXPORT_METHOD(getTokenWithCompletion:(RCTResponseSenderBlock) callback)
309349
{
310350
FIRUser *user = [FIRAuth auth].currentUser;
311-
351+
312352
[user getTokenWithCompletion:^(NSString *token , NSError *_Nullable error) {
313353
if (error) {
314354
NSDictionary *err =
@@ -338,9 +378,9 @@ @implementation FirestackAuth
338378
};
339379
return callback(@[err]);
340380
}
341-
381+
342382
FIRUser *user = [FIRAuth auth].currentUser;
343-
383+
344384
[user reauthenticateWithCredential:credential completion:^(NSError *_Nullable error) {
345385
if (error) {
346386
NSDictionary *err =
@@ -360,7 +400,7 @@ @implementation FirestackAuth
360400
{
361401
FIRUser *user = [FIRAuth auth].currentUser;
362402
FIRUserProfileChangeRequest *changeRequest = [user profileChangeRequest];
363-
403+
364404
NSMutableArray *allKeys = [[userProps allKeys] mutableCopy];
365405
for (NSString *key in allKeys) {
366406
// i.e. changeRequest.displayName = userProps[displayName];
@@ -405,12 +445,12 @@ - (NSDictionary *) userPropsFromFIRUser:(FIRUser *) user
405445
@"refreshToken": user.refreshToken,
406446
@"providerID": user.providerID
407447
} mutableCopy];
408-
448+
409449
if ([user valueForKey:@"photoURL"] != nil) {
410450
[userProps setValue: [NSString stringWithFormat:@"%@", user.photoURL]
411451
forKey:@"photoURL"];
412452
}
413-
453+
414454
return userProps;
415455
}
416456

@@ -422,7 +462,7 @@ - (void) userPropsFromFIRUserWithToken:(FIRUser *) user
422462
if (error != nil) {
423463
return callback(nil, error);
424464
}
425-
465+
426466
[userProps setValue:token forKey:@"idToken"];
427467
callback(userProps, nil);
428468
}];
@@ -449,15 +489,17 @@ - (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider
449489

450490
// Not sure how to get away from this... yet
451491
- (NSArray<NSString *> *)supportedEvents {
452-
return @[AUTH_CHANGED_EVENT];
492+
return @[AUTH_CHANGED_EVENT, AUTH_ANONYMOUS_ERROR_EVENT, AUTH_ERROR_EVENT];
453493
}
454494

455495
- (void) sendJSEvent:(NSString *)title
456496
props:(NSDictionary *)props
457497
{
458498
@try {
499+
if ([self listeningForAuth]) {
459500
[self sendEventWithName:title
460501
body:props];
502+
}
461503
}
462504
@catch (NSException *err) {
463505
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

0 commit comments

Comments
 (0)