Skip to content

Commit 7774ac1

Browse files
authored
Merge pull request #152 from Salakar/master
Update v3 with latest changes
2 parents f715d41 + 1882af7 commit 7774ac1

File tree

11 files changed

+201
-206
lines changed

11 files changed

+201
-206
lines changed

Diff for: README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ npm i react-native-firestack --save
1010
[![npm version](https://img.shields.io/npm/v/react-native-firestack.svg)](https://www.npmjs.com/package/react-native-firestack)
1111
[![License](https://img.shields.io/npm/l/react-native-firestack.svg)](/LICENSE)
1212

13-
Firestack is a _light-weight_ layer sitting on-top of the native Firebase libraries for both iOS and Android which mirrors the React Native JS api as closely as possible. It features:
13+
Firestack is a _light-weight_ layer sitting on-top of the native Firebase libraries for both iOS and Android which mirrors the React Native JS api as closely as possible.
1414

1515
Featuring; authentication, storage, real-time database, presence, analytics, cloud messaging, remote configuration, redux support and more!
1616

1717
## Firestack vs Firebase JS lib
1818

19-
Although the [Firebase](https://www.npmjs.com/package/firebase) JavaScript library will work with React Native, it's designed for the web and/or server. The native SDKs provide much needed features specifically for mobile applications such as offline persistance. Firestack provides a JavaScript interface into the native SDKs to allow your React Native application to utilise these features, and more!
19+
Although the [Firebase](https://www.npmjs.com/package/firebase) JavaScript library will work with React Native, it is mainly designed for the web.
20+
21+
The native SDK's are much better for performance compared to the web SDK. The web SDK will run on the same thread as your apps ([JS thread](https://facebook.github.io/react-native/docs/performance.html#javascript-frame-rate)) therefore limiting your JS framerate, potentially affecting things touch events and transitions/animations.
22+
23+
The native SDK's also contains functionality that the web SDK's do not, for example [Analytics](/docs/api/analytics.md) and [Remote Config](/docs/api/remote-config.md).
2024

2125
## Example app
2226

@@ -36,6 +40,7 @@ We have a working application example available in at [fullstackreact/FirestackA
3640
* [Presence](docs/api/presence.md)
3741
* [ServerValue](docs/api/server-value.md)
3842
* [Cloud Messaging](docs/api/cloud-messaging.md)
43+
* [Remote Config](docs/api/remote-config.md)
3944
* [Events](docs/api/events.md)
4045
* [Redux](docs/redux.md)
4146

Diff for: android/src/main/java/io/fullstack/firestack/FirestackAuth.java

+31-74
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
import com.google.android.gms.tasks.OnCompleteListener;
2121
import com.google.android.gms.tasks.Task;
22-
import com.google.firebase.FirebaseApp;
2322

2423
import com.google.firebase.auth.AuthCredential;
2524
import com.google.firebase.auth.AuthResult;
@@ -42,14 +41,12 @@ class FirestackAuthModule extends ReactContextBaseJavaModule {
4241
// private Context context;
4342
private ReactContext mReactContext;
4443
private FirebaseAuth mAuth;
45-
private FirebaseApp app;
46-
private FirebaseUser user;
4744
private FirebaseAuth.AuthStateListener mAuthListener;
4845

4946
public FirestackAuthModule(ReactApplicationContext reactContext) {
5047
super(reactContext);
51-
// this.context = reactContext;
5248
mReactContext = reactContext;
49+
mAuth = FirebaseAuth.getInstance();
5350

5451
Log.d(TAG, "New FirestackAuth instance");
5552
}
@@ -78,16 +75,17 @@ private void callbackNoUser(Callback callback, Boolean isError) {
7875

7976
@ReactMethod
8077
public void listenForAuth() {
81-
if (mAuthListener == null || mAuth == null) {
78+
if (mAuthListener == null) {
8279
mAuthListener = new FirebaseAuth.AuthStateListener() {
8380
@Override
8481
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
82+
FirebaseUser user = firebaseAuth.getCurrentUser();
8583
WritableMap msgMap = Arguments.createMap();
8684
msgMap.putString("eventName", "listenForAuth");
8785

88-
if (FirestackAuthModule.this.user != null) {
86+
if (user != null) {
8987
// TODO move to helper
90-
WritableMap userMap = getUserMap();
88+
WritableMap userMap = getUserMap(user);
9189
msgMap.putBoolean("authenticated", true);
9290
msgMap.putMap("user", userMap);
9391

@@ -98,8 +96,6 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
9896
}
9997
}
10098
};
101-
102-
mAuth = FirebaseAuth.getInstance();
10399
mAuth.addAuthStateListener(mAuthListener);
104100
}
105101
}
@@ -119,16 +115,13 @@ public void unlistenForAuth(final Callback callback) {
119115

120116
@ReactMethod
121117
public void createUserWithEmail(final String email, final String password, final Callback callback) {
122-
mAuth = FirebaseAuth.getInstance();
123-
124118
mAuth.createUserWithEmailAndPassword(email, password)
125119
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
126120
@Override
127121
public void onComplete(@NonNull Task<AuthResult> task) {
128122
try {
129123
if (task.isSuccessful()) {
130-
FirestackAuthModule.this.user = task.getResult().getUser();
131-
userCallback(FirestackAuthModule.this.user, callback);
124+
userCallback(task.getResult().getUser(), callback);
132125
} else {
133126
userErrorCallback(task, callback);
134127
}
@@ -141,16 +134,14 @@ public void onComplete(@NonNull Task<AuthResult> task) {
141134

142135
@ReactMethod
143136
public void signInWithEmail(final String email, final String password, final Callback callback) {
144-
mAuth = FirebaseAuth.getInstance();
145137

146138
mAuth.signInWithEmailAndPassword(email, password)
147139
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
148140
@Override
149141
public void onComplete(@NonNull Task<AuthResult> task) {
150142
try {
151143
if (task.isSuccessful()) {
152-
FirestackAuthModule.this.user = task.getResult().getUser();
153-
userCallback(FirestackAuthModule.this.user, callback);
144+
userCallback(task.getResult().getUser(), callback);
154145
} else {
155146
userErrorCallback(task, callback);
156147
}
@@ -175,9 +166,6 @@ public void signInWithProvider(final String provider, final String authToken, fi
175166
@ReactMethod
176167
public void signInAnonymously(final Callback callback) {
177168
Log.d(TAG, "signInAnonymously:called:");
178-
mAuth = FirebaseAuth.getInstance();
179-
180-
181169
mAuth.signInAnonymously()
182170
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
183171
@Override
@@ -186,8 +174,7 @@ public void onComplete(@NonNull Task<AuthResult> task) {
186174

187175
try {
188176
if (task.isSuccessful()) {
189-
FirestackAuthModule.this.user = task.getResult().getUser();
190-
userCallback(FirestackAuthModule.this.user, callback);
177+
userCallback(task.getResult().getUser(), callback);
191178
} else {
192179
userErrorCallback(task, callback);
193180
}
@@ -200,17 +187,14 @@ public void onComplete(@NonNull Task<AuthResult> task) {
200187

201188
@ReactMethod
202189
public void signInWithCustomToken(final String customToken, final Callback callback) {
203-
mAuth = FirebaseAuth.getInstance();
204-
205190
mAuth.signInWithCustomToken(customToken)
206191
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
207192
@Override
208193
public void onComplete(@NonNull Task<AuthResult> task) {
209194
Log.d(TAG, "signInWithCustomToken:onComplete:" + task.isSuccessful());
210195
try {
211196
if (task.isSuccessful()) {
212-
FirestackAuthModule.this.user = task.getResult().getUser();
213-
userCallback(FirestackAuthModule.this.user, callback);
197+
userCallback(task.getResult().getUser(), callback);
214198
} else {
215199
userErrorCallback(task, callback);
216200
}
@@ -231,7 +215,7 @@ public void reauthenticateWithCredentialForProvider(final String provider, final
231215

232216
@ReactMethod
233217
public void updateUserEmail(final String email, final Callback callback) {
234-
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
218+
FirebaseUser user = mAuth.getCurrentUser();
235219

236220
if (user != null) {
237221
user
@@ -242,8 +226,7 @@ public void onComplete(@NonNull Task<Void> task) {
242226
try {
243227
if (task.isSuccessful()) {
244228
Log.d(TAG, "User email address updated");
245-
FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser();
246-
userCallback(u, callback);
229+
userCallback(mAuth.getCurrentUser(), callback);
247230
} else {
248231
userErrorCallback(task, callback);
249232
}
@@ -259,7 +242,7 @@ public void onComplete(@NonNull Task<Void> task) {
259242

260243
@ReactMethod
261244
public void updateUserPassword(final String newPassword, final Callback callback) {
262-
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
245+
FirebaseUser user = mAuth.getCurrentUser();
263246

264247
if (user != null) {
265248
user.updatePassword(newPassword)
@@ -269,9 +252,7 @@ public void onComplete(@NonNull Task<Void> task) {
269252
try {
270253
if (task.isSuccessful()) {
271254
Log.d(TAG, "User password updated");
272-
273-
FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser();
274-
userCallback(u, callback);
255+
userCallback(mAuth.getCurrentUser(), callback);
275256
} else {
276257
userErrorCallback(task, callback);
277258
}
@@ -287,8 +268,6 @@ public void onComplete(@NonNull Task<Void> task) {
287268

288269
@ReactMethod
289270
public void sendPasswordResetWithEmail(final String email, final Callback callback) {
290-
mAuth = FirebaseAuth.getInstance();
291-
292271
mAuth.sendPasswordResetEmail(email)
293272
.addOnCompleteListener(new OnCompleteListener<Void>() {
294273
@Override
@@ -310,7 +289,7 @@ public void onComplete(@NonNull Task<Void> task) {
310289

311290
@ReactMethod
312291
public void deleteUser(final Callback callback) {
313-
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
292+
FirebaseUser user = mAuth.getCurrentUser();
314293
if (user != null) {
315294
user.delete()
316295
.addOnCompleteListener(new OnCompleteListener<Void>() {
@@ -339,7 +318,7 @@ public void onComplete(@NonNull Task<Void> task) {
339318

340319
@ReactMethod
341320
public void sendEmailVerification(final Callback callback) {
342-
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
321+
FirebaseUser user = mAuth.getCurrentUser();
343322

344323
if (user != null) {
345324
user.sendEmailVerification()
@@ -371,7 +350,7 @@ public void onComplete(@NonNull Task<Void> task) {
371350

372351
@ReactMethod
373352
public void getToken(final Callback callback) {
374-
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
353+
FirebaseUser user = mAuth.getCurrentUser();
375354

376355
if (user != null) {
377356
user.getToken(true)
@@ -403,7 +382,7 @@ public void onComplete(@NonNull Task<GetTokenResult> task) {
403382

404383
@ReactMethod
405384
public void updateUserProfile(ReadableMap props, final Callback callback) {
406-
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
385+
FirebaseUser user = mAuth.getCurrentUser();
407386

408387
if (user != null) {
409388
UserProfileChangeRequest.Builder profileBuilder = new UserProfileChangeRequest.Builder();
@@ -430,8 +409,7 @@ public void onComplete(@NonNull Task<Void> task) {
430409
try {
431410
if (task.isSuccessful()) {
432411
Log.d(TAG, "User profile updated");
433-
FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser();
434-
userCallback(u, callback);
412+
userCallback(mAuth.getCurrentUser(), callback);
435413
} else {
436414
userErrorCallback(task, callback);
437415
}
@@ -447,8 +425,7 @@ public void onComplete(@NonNull Task<Void> task) {
447425

448426
@ReactMethod
449427
public void signOut(final Callback callback) {
450-
FirebaseAuth.getInstance().signOut();
451-
this.user = null;
428+
mAuth.signOut();
452429

453430
WritableMap resp = Arguments.createMap();
454431
resp.putString("status", "complete");
@@ -458,31 +435,26 @@ public void signOut(final Callback callback) {
458435

459436
@ReactMethod
460437
public void getCurrentUser(final Callback callback) {
461-
mAuth = FirebaseAuth.getInstance();
462-
463-
this.user = mAuth.getCurrentUser();
464-
if (this.user == null) {
438+
FirebaseUser user = mAuth.getCurrentUser();
439+
if (user == null) {
465440
callbackNoUser(callback, false);
466441
} else {
467-
Log.d("USRC", this.user.getUid());
468-
userCallback(this.user, callback);
442+
Log.d("USRC", user.getUid());
443+
userCallback(user, callback);
469444
}
470445
}
471446

472447
// TODO: Check these things
473448
@ReactMethod
474449
public void googleLogin(String IdToken, final Callback callback) {
475-
mAuth = FirebaseAuth.getInstance();
476-
477450
AuthCredential credential = GoogleAuthProvider.getCredential(IdToken, null);
478451
mAuth.signInWithCredential(credential)
479452
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
480453
@Override
481454
public void onComplete(@NonNull Task<AuthResult> task) {
482455
try {
483456
if (task.isSuccessful()) {
484-
FirestackAuthModule.this.user = task.getResult().getUser();
485-
userCallback(FirestackAuthModule.this.user, callback);
457+
userCallback(task.getResult().getUser(), callback);
486458
} else {
487459
userErrorCallback(task, callback);
488460
}
@@ -495,17 +467,14 @@ public void onComplete(@NonNull Task<AuthResult> task) {
495467

496468
@ReactMethod
497469
public void facebookLogin(String Token, final Callback callback) {
498-
mAuth = FirebaseAuth.getInstance();
499-
500470
AuthCredential credential = FacebookAuthProvider.getCredential(Token);
501471
mAuth.signInWithCredential(credential)
502472
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
503473
@Override
504474
public void onComplete(@NonNull Task<AuthResult> task) {
505475
try {
506476
if (task.isSuccessful()) {
507-
FirestackAuthModule.this.user = task.getResult().getUser();
508-
userCallback(FirestackAuthModule.this.user, callback);
477+
userCallback(task.getResult().getUser(), callback);
509478
} else {
510479
userErrorCallback(task, callback);
511480
}
@@ -517,24 +486,15 @@ public void onComplete(@NonNull Task<AuthResult> task) {
517486
}
518487

519488
// Internal helpers
520-
private void userCallback(FirebaseUser passedUser, final Callback callback) {
521-
522-
if (passedUser == null) {
523-
mAuth = FirebaseAuth.getInstance();
524-
this.user = mAuth.getCurrentUser();
525-
} else {
526-
this.user = passedUser;
527-
}
528-
529-
if (this.user != null) {
530-
this.user.getToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
489+
private void userCallback(final FirebaseUser user, final Callback callback) {
490+
if (user != null) {
491+
user.getToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
531492
@Override
532493
public void onComplete(@NonNull Task<GetTokenResult> task) {
533494
try {
534495
if (task.isSuccessful()) {
535-
WritableMap userMap = getUserMap();
536-
final String token = task.getResult().getToken();
537-
userMap.putString("token", token);
496+
WritableMap userMap = getUserMap(user);
497+
userMap.putString("token", task.getResult().getToken());
538498
callback.invoke(null, userMap);
539499
} else {
540500
userErrorCallback(task, callback);
@@ -567,11 +527,8 @@ private void userExceptionCallback(Exception ex, final Callback onFail) {
567527
onFail.invoke(error);
568528
}
569529

570-
private WritableMap getUserMap() {
530+
private WritableMap getUserMap(FirebaseUser user) {
571531
WritableMap userMap = Arguments.createMap();
572-
573-
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
574-
575532
if (user != null) {
576533
final String email = user.getEmail();
577534
final String uid = user.getUid();

0 commit comments

Comments
 (0)