Skip to content

Commit 9bb5e5d

Browse files
committed
Merge branch 'master' of github.com:fullstackreact/react-native-firestack
* 'master' of github.com:fullstackreact/react-native-firestack: Cleanup listeners and add a little bit of documentation Remove auth methods from instance root & update documentation Fix Java Database Listeners Fix listeners getting called when it's not their turn Android: Fix .on() listeners not getting removed / called immediately on setup Upgrade google play services and firebase to 9.8.0 Android: fix userMap not being used in userCallback & anonymousUserCallback Fix passedUser not being used, make it more clear what user is being used
2 parents 4e18f7e + 9a05b23 commit 9bb5e5d

File tree

5 files changed

+116
-135
lines changed

5 files changed

+116
-135
lines changed

Diff for: README.md

+14-12
Original file line numberDiff line numberDiff line change
@@ -280,12 +280,14 @@ All methods return a promise.
280280

281281
Firestack handles authentication for us out of the box, both with email/password-based authentication and through oauth providers (with a separate library to handle oauth providers).
282282

283+
> Android requires the Google Play services to installed for authentication to function.
284+
283285
#### listenForAuth()
284286

285287
Firebase gives us a reactive method for listening for authentication. That is we can set up a listener to call a method when the user logs in and out. To set up the listener, call the `listenForAuth()` method:
286288

287289
```javascript
288-
firestack.listenForAuth(function(evt) {
290+
firestack.auth.listenForAuth(function(evt) {
289291
// evt is the authentication event
290292
// it contains an `error` key for carrying the
291293
// error message in case of an error
@@ -306,15 +308,15 @@ firestack.listenForAuth(function(evt) {
306308
We can remove this listener by calling the `unlistenForAuth()` method. This is important to release resources from our app when we don't need to hold on to the listener any longer.
307309

308310
```javascript
309-
firestack.unlistenForAuth()
311+
firestack.auth.unlistenForAuth()
310312
```
311313

312314
#### createUserWithEmail()
313315

314316
We can create a user by calling the `createUserWithEmail()` function. The `createUserWithEmail()` accepts two parameters, an email and a password.
315317

316318
```javascript
317-
firestack.createUserWithEmail('[email protected]', '123456')
319+
firestack.auth.createUserWithEmail('[email protected]', '123456')
318320
.then((user) => {
319321
console.log('user created', user)
320322
})
@@ -342,7 +344,7 @@ firestack.auth.signInWithEmail('[email protected]', '123456')
342344
To sign a user using a self-signed custom token, use the `signInWithCustomToken()` function. It accepts one parameter, the custom token:
343345

344346
```javascript
345-
firestack.signInWithCustomToken(TOKEN)
347+
firestack.auth.signInWithCustomToken(TOKEN)
346348
.then((user) => {
347349
console.log('User successfully logged in', user)
348350
})
@@ -397,7 +399,7 @@ When the auth token has expired, we can ask firebase to reauthenticate with the
397399
We can update the current user's email by using the command: `updateUserEmail()`. It accepts a single argument: the user's new email:
398400

399401
```javascript
400-
firestack.updateUserEmail('[email protected]')
402+
firestack.auth.updateUserEmail('[email protected]')
401403
.then((res) => console.log('Updated user email'))
402404
.catch(err => console.error('There was an error updating user email'))
403405
```
@@ -407,7 +409,7 @@ firestack.updateUserEmail('[email protected]')
407409
We can update the current user's password using the `updateUserPassword()` method. It accepts a single parameter: the new password for the current user
408410

409411
```javascript
410-
firestack.updateUserPassword('somethingReallyS3cr3t733t')
412+
firestack.auth.updateUserPassword('somethingReallyS3cr3t733t')
411413
.then(res => console.log('Updated user password'))
412414
.catch(err => console.error('There was an error updating your password'))
413415
```
@@ -417,7 +419,7 @@ firestack.updateUserPassword('somethingReallyS3cr3t733t')
417419
To send a password reset for a user based upon their email, we can call the `sendPasswordResetWithEmail()` method. It accepts a single parameter: the email of the user to send a reset email.
418420

419421
```javascript
420-
firestack.sendPasswordResetWithEmail('[email protected]')
422+
firestack.auth.sendPasswordResetWithEmail('[email protected]')
421423
.then(res => console.log('Check your inbox for further instructions'))
422424
.catch(err => console.error('There was an error :('))
423425
```
@@ -431,7 +433,7 @@ It accepts a single parameter:
431433
* object which contains updated key/values for the user's profile. Possible keys are listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile).
432434

433435
```javascript
434-
firestack.updateUserProfile({
436+
firestack.auth.updateUserProfile({
435437
displayName: 'Ari Lerner'
436438
})
437439
.then(res => console.log('Your profile has been updated'))
@@ -443,7 +445,7 @@ firestack.updateUserProfile({
443445
It's possible to delete a user completely from your account on Firebase. Calling the `deleteUser()` method will take care of this for you.
444446

445447
```javascript
446-
firestack.deleteUser()
448+
firestack.auth.deleteUser()
447449
.then(res => console.log('Sad to see you go'))
448450
.catch(err => console.error('There was an error - Now you are trapped!'))
449451
```
@@ -453,7 +455,7 @@ firestack.deleteUser()
453455
If you want user's token, use `getToken()` method.
454456

455457
```javascript
456-
firestack.getToken()
458+
firestack.auth.getToken()
457459
.then(res => console.log(res.token))
458460
.catch(err => console.error('error'))
459461
```
@@ -463,7 +465,7 @@ firestack.getToken()
463465
To sign the current user out, use the `signOut()` method. It accepts no parameters
464466

465467
```javascript
466-
firestack.signOut()
468+
firestack.auth.signOut()
467469
.then(res => console.log('You have been signed out'))
468470
.catch(err => console.error('Uh oh... something weird happened'))
469471
```
@@ -473,7 +475,7 @@ firestack.signOut()
473475
Although you _can_ get the current user using the `getCurrentUser()` method, it's better to use this from within the callback function provided by `listenForAuth()`. However, if you need to get the current user, call the `getCurrentUser()` method:
474476

475477
```javascript
476-
firestack.getCurrentUser()
478+
firestack.auth.getCurrentUser()
477479
.then(user => console.log('The currently logged in user', user))
478480
.catch(err => console.error('An error occurred'))
479481
```

Diff for: android/build.gradle

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ android {
2020

2121
dependencies {
2222
compile 'com.facebook.react:react-native:0.20.+'
23-
compile 'com.google.android.gms:play-services-base:9.6.1'
23+
compile 'com.google.android.gms:play-services-base:9.8.0'
2424

25-
compile 'com.google.firebase:firebase-core:9.6.0'
26-
compile 'com.google.firebase:firebase-auth:9.6.0'
27-
compile 'com.google.firebase:firebase-analytics:9.6.0'
28-
compile 'com.google.firebase:firebase-database:9.6.0'
29-
compile 'com.google.firebase:firebase-storage:9.6.0'
30-
compile 'com.google.firebase:firebase-messaging:9.6.0'
25+
compile 'com.google.firebase:firebase-core:9.8.0'
26+
compile 'com.google.firebase:firebase-auth:9.8.0'
27+
compile 'com.google.firebase:firebase-analytics:9.8.0'
28+
compile 'com.google.firebase:firebase-database:9.8.0'
29+
compile 'com.google.firebase:firebase-storage:9.8.0'
30+
compile 'com.google.firebase:firebase-messaging:9.8.0'
3131
}
3232

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

+35-38
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
6868
WritableMap msgMap = Arguments.createMap();
6969
msgMap.putString("eventName", "listenForAuth");
7070

71-
if (user != null) {
71+
if (FirestackAuthModule.this.user != null) {
7272
WritableMap userMap = getUserMap();
7373

7474
msgMap.putBoolean("authenticated", true);
@@ -107,8 +107,8 @@ public void createUserWithEmail(final String email, final String password, final
107107
@Override
108108
public void onComplete(@NonNull Task<AuthResult> task) {
109109
if (task.isSuccessful()) {
110-
user = task.getResult().getUser();
111-
userCallback(user, onComplete);
110+
FirestackAuthModule.this.user = task.getResult().getUser();
111+
userCallback(FirestackAuthModule.this.user, onComplete);
112112
}else{
113113
userErrorCallback(task, onComplete);
114114
}
@@ -125,8 +125,8 @@ public void signInWithEmail(final String email, final String password, final Cal
125125
@Override
126126
public void onComplete(@NonNull Task<AuthResult> task) {
127127
if (task.isSuccessful()) {
128-
user = task.getResult().getUser();
129-
userCallback(user, callback);
128+
FirestackAuthModule.this.user = task.getResult().getUser();
129+
userCallback(FirestackAuthModule.this.user, callback);
130130
} else {
131131
userErrorCallback(task, callback);
132132
}
@@ -156,8 +156,8 @@ public void onComplete(@NonNull Task<AuthResult> task) {
156156
Log.d(TAG, "signInAnonymously:onComplete:" + task.isSuccessful());
157157

158158
if (task.isSuccessful()) {
159-
user = task.getResult().getUser();
160-
anonymousUserCallback(user, callback);
159+
FirestackAuthModule.this.user = task.getResult().getUser();
160+
anonymousUserCallback(FirestackAuthModule.this.user, callback);
161161
}else{
162162
userErrorCallback(task, callback);
163163
}
@@ -176,8 +176,8 @@ public void signInWithCustomToken(final String customToken, final Callback callb
176176
public void onComplete(@NonNull Task<AuthResult> task) {
177177
Log.d(TAG, "signInWithCustomToken:onComplete:" + task.isSuccessful());
178178
if (task.isSuccessful()) {
179-
user = task.getResult().getUser();
180-
userCallback(user, callback);
179+
FirestackAuthModule.this.user = task.getResult().getUser();
180+
userCallback(FirestackAuthModule.this.user, callback);
181181
} else {
182182
userErrorCallback(task, callback);
183183
}
@@ -356,7 +356,7 @@ public void onComplete(@NonNull Task<Void> task) {
356356
@ReactMethod
357357
public void signOut(final Callback callback) {
358358
FirebaseAuth.getInstance().signOut();
359-
user = null;
359+
this.user = null;
360360

361361
WritableMap resp = Arguments.createMap();
362362
resp.putString("status", "complete");
@@ -368,11 +368,11 @@ public void signOut(final Callback callback) {
368368
public void getCurrentUser(final Callback callback) {
369369
mAuth = FirebaseAuth.getInstance();
370370

371-
user = mAuth.getCurrentUser();
372-
if(user == null){
371+
this.user = mAuth.getCurrentUser();
372+
if(this.user == null){
373373
noUserCallback(callback);
374374
}else{
375-
userCallback(user, callback);
375+
userCallback(this.user, callback);
376376
}
377377
}
378378

@@ -387,8 +387,8 @@ public void googleLogin(String IdToken, final Callback callback) {
387387
@Override
388388
public void onComplete(@NonNull Task<AuthResult> task) {
389389
if (task.isSuccessful()) {
390-
user = task.getResult().getUser();
391-
userCallback(user, callback);
390+
FirestackAuthModule.this.user = task.getResult().getUser();
391+
userCallback(FirestackAuthModule.this.user, callback);
392392
}else{
393393
userErrorCallback(task, callback);
394394
}
@@ -406,8 +406,8 @@ public void facebookLogin(String Token, final Callback callback) {
406406
@Override
407407
public void onComplete(@NonNull Task<AuthResult> task) {
408408
if (task.isSuccessful()) {
409-
user = task.getResult().getUser();
410-
userCallback(user, callback);
409+
FirestackAuthModule.this.user = task.getResult().getUser();
410+
userCallback(FirestackAuthModule.this.user, callback);
411411
}else{
412412
userErrorCallback(task, callback);
413413
}
@@ -417,26 +417,24 @@ public void onComplete(@NonNull Task<AuthResult> task) {
417417

418418
// Internal helpers
419419
public void userCallback(FirebaseUser passedUser, final Callback onComplete) {
420-
WritableMap userMap = getUserMap();
421420

422421
if (passedUser == null) {
423422
mAuth = FirebaseAuth.getInstance();
424-
final FirebaseUser user = mAuth.getCurrentUser();
423+
this.user = mAuth.getCurrentUser();
425424
} else {
426-
final FirebaseUser user = passedUser;
425+
this.user = passedUser;
427426
}
428427

429-
user.getToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
428+
this.user.getToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
430429
@Override
431430
public void onComplete(@NonNull Task<GetTokenResult> task) {
432-
WritableMap msgMap = Arguments.createMap();
433-
WritableMap userMap = Arguments.createMap();
434-
435-
if (user != null) {
436-
final String token = task.getResult().getToken();
431+
WritableMap msgMap = Arguments.createMap();
432+
WritableMap userMap = getUserMap();
433+
if (FirestackAuthModule.this.user != null) {
434+
final String token = task.getResult().getToken();
437435

438-
userMap.putString("token", token);
439-
userMap.putBoolean("anonymous", false);
436+
userMap.putString("token", token);
437+
userMap.putBoolean("anonymous", false);
440438
}
441439

442440
msgMap.putMap("user", userMap);
@@ -448,26 +446,25 @@ public void onComplete(@NonNull Task<GetTokenResult> task) {
448446

449447
// TODO: Reduce to one method
450448
public void anonymousUserCallback(FirebaseUser passedUser, final Callback onComplete) {
451-
WritableMap userMap = getUserMap();
452449

453450
if (passedUser == null) {
454451
mAuth = FirebaseAuth.getInstance();
455-
final FirebaseUser user = mAuth.getCurrentUser();
452+
this.user = mAuth.getCurrentUser();
456453
} else {
457-
final FirebaseUser user = passedUser;
454+
this.user = passedUser;
458455
}
459456

460-
user.getToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
457+
this.user.getToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
461458
@Override
462459
public void onComplete(@NonNull Task<GetTokenResult> task) {
463-
WritableMap msgMap = Arguments.createMap();
464-
WritableMap userMap = Arguments.createMap();
460+
WritableMap msgMap = Arguments.createMap();
461+
WritableMap userMap = getUserMap();
465462

466-
if (user != null) {
467-
final String token = task.getResult().getToken();
463+
if (FirestackAuthModule.this.user != null) {
464+
final String token = task.getResult().getToken();
468465

469-
userMap.putString("token", token);
470-
userMap.putBoolean("anonymous", true);
466+
userMap.putString("token", token);
467+
userMap.putBoolean("anonymous", true);
471468
}
472469

473470
msgMap.putMap("user", userMap);

0 commit comments

Comments
 (0)