Skip to content

Commit

Permalink
Merge pull request #152 from Salakar/master
Browse files Browse the repository at this point in the history
Update v3 with latest changes
  • Loading branch information
Salakar authored Nov 21, 2016
2 parents f715d41 + 1882af7 commit 7774ac1
Show file tree
Hide file tree
Showing 11 changed files with 201 additions and 206 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ npm i react-native-firestack --save
[![npm version](https://img.shields.io/npm/v/react-native-firestack.svg)](https://www.npmjs.com/package/react-native-firestack)
[![License](https://img.shields.io/npm/l/react-native-firestack.svg)](/LICENSE)

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:
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.

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

## Firestack vs Firebase JS lib

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!
Although the [Firebase](https://www.npmjs.com/package/firebase) JavaScript library will work with React Native, it is mainly designed for the web.

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.

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).

## Example app

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

Expand Down
105 changes: 31 additions & 74 deletions android/src/main/java/io/fullstack/firestack/FirestackAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseApp;

import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
Expand All @@ -42,14 +41,12 @@ class FirestackAuthModule extends ReactContextBaseJavaModule {
// private Context context;
private ReactContext mReactContext;
private FirebaseAuth mAuth;
private FirebaseApp app;
private FirebaseUser user;
private FirebaseAuth.AuthStateListener mAuthListener;

public FirestackAuthModule(ReactApplicationContext reactContext) {
super(reactContext);
// this.context = reactContext;
mReactContext = reactContext;
mAuth = FirebaseAuth.getInstance();

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

@ReactMethod
public void listenForAuth() {
if (mAuthListener == null || mAuth == null) {
if (mAuthListener == null) {
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
WritableMap msgMap = Arguments.createMap();
msgMap.putString("eventName", "listenForAuth");

if (FirestackAuthModule.this.user != null) {
if (user != null) {
// TODO move to helper
WritableMap userMap = getUserMap();
WritableMap userMap = getUserMap(user);
msgMap.putBoolean("authenticated", true);
msgMap.putMap("user", userMap);

Expand All @@ -98,8 +96,6 @@ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
}
}
};

mAuth = FirebaseAuth.getInstance();
mAuth.addAuthStateListener(mAuthListener);
}
}
Expand All @@ -119,16 +115,13 @@ public void unlistenForAuth(final Callback callback) {

@ReactMethod
public void createUserWithEmail(final String email, final String password, final Callback callback) {
mAuth = FirebaseAuth.getInstance();

mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
if (task.isSuccessful()) {
FirestackAuthModule.this.user = task.getResult().getUser();
userCallback(FirestackAuthModule.this.user, callback);
userCallback(task.getResult().getUser(), callback);
} else {
userErrorCallback(task, callback);
}
Expand All @@ -141,16 +134,14 @@ public void onComplete(@NonNull Task<AuthResult> task) {

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

mAuth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
if (task.isSuccessful()) {
FirestackAuthModule.this.user = task.getResult().getUser();
userCallback(FirestackAuthModule.this.user, callback);
userCallback(task.getResult().getUser(), callback);
} else {
userErrorCallback(task, callback);
}
Expand All @@ -175,9 +166,6 @@ public void signInWithProvider(final String provider, final String authToken, fi
@ReactMethod
public void signInAnonymously(final Callback callback) {
Log.d(TAG, "signInAnonymously:called:");
mAuth = FirebaseAuth.getInstance();


mAuth.signInAnonymously()
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
Expand All @@ -186,8 +174,7 @@ public void onComplete(@NonNull Task<AuthResult> task) {

try {
if (task.isSuccessful()) {
FirestackAuthModule.this.user = task.getResult().getUser();
userCallback(FirestackAuthModule.this.user, callback);
userCallback(task.getResult().getUser(), callback);
} else {
userErrorCallback(task, callback);
}
Expand All @@ -200,17 +187,14 @@ public void onComplete(@NonNull Task<AuthResult> task) {

@ReactMethod
public void signInWithCustomToken(final String customToken, final Callback callback) {
mAuth = FirebaseAuth.getInstance();

mAuth.signInWithCustomToken(customToken)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCustomToken:onComplete:" + task.isSuccessful());
try {
if (task.isSuccessful()) {
FirestackAuthModule.this.user = task.getResult().getUser();
userCallback(FirestackAuthModule.this.user, callback);
userCallback(task.getResult().getUser(), callback);
} else {
userErrorCallback(task, callback);
}
Expand All @@ -231,7 +215,7 @@ public void reauthenticateWithCredentialForProvider(final String provider, final

@ReactMethod
public void updateUserEmail(final String email, final Callback callback) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseUser user = mAuth.getCurrentUser();

if (user != null) {
user
Expand All @@ -242,8 +226,7 @@ public void onComplete(@NonNull Task<Void> task) {
try {
if (task.isSuccessful()) {
Log.d(TAG, "User email address updated");
FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser();
userCallback(u, callback);
userCallback(mAuth.getCurrentUser(), callback);
} else {
userErrorCallback(task, callback);
}
Expand All @@ -259,7 +242,7 @@ public void onComplete(@NonNull Task<Void> task) {

@ReactMethod
public void updateUserPassword(final String newPassword, final Callback callback) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseUser user = mAuth.getCurrentUser();

if (user != null) {
user.updatePassword(newPassword)
Expand All @@ -269,9 +252,7 @@ public void onComplete(@NonNull Task<Void> task) {
try {
if (task.isSuccessful()) {
Log.d(TAG, "User password updated");

FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser();
userCallback(u, callback);
userCallback(mAuth.getCurrentUser(), callback);
} else {
userErrorCallback(task, callback);
}
Expand All @@ -287,8 +268,6 @@ public void onComplete(@NonNull Task<Void> task) {

@ReactMethod
public void sendPasswordResetWithEmail(final String email, final Callback callback) {
mAuth = FirebaseAuth.getInstance();

mAuth.sendPasswordResetEmail(email)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
Expand All @@ -310,7 +289,7 @@ public void onComplete(@NonNull Task<Void> task) {

@ReactMethod
public void deleteUser(final Callback callback) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
user.delete()
.addOnCompleteListener(new OnCompleteListener<Void>() {
Expand Down Expand Up @@ -339,7 +318,7 @@ public void onComplete(@NonNull Task<Void> task) {

@ReactMethod
public void sendEmailVerification(final Callback callback) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseUser user = mAuth.getCurrentUser();

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

@ReactMethod
public void getToken(final Callback callback) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseUser user = mAuth.getCurrentUser();

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

@ReactMethod
public void updateUserProfile(ReadableMap props, final Callback callback) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
FirebaseUser user = mAuth.getCurrentUser();

if (user != null) {
UserProfileChangeRequest.Builder profileBuilder = new UserProfileChangeRequest.Builder();
Expand All @@ -430,8 +409,7 @@ public void onComplete(@NonNull Task<Void> task) {
try {
if (task.isSuccessful()) {
Log.d(TAG, "User profile updated");
FirebaseUser u = FirebaseAuth.getInstance().getCurrentUser();
userCallback(u, callback);
userCallback(mAuth.getCurrentUser(), callback);
} else {
userErrorCallback(task, callback);
}
Expand All @@ -447,8 +425,7 @@ public void onComplete(@NonNull Task<Void> task) {

@ReactMethod
public void signOut(final Callback callback) {
FirebaseAuth.getInstance().signOut();
this.user = null;
mAuth.signOut();

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

@ReactMethod
public void getCurrentUser(final Callback callback) {
mAuth = FirebaseAuth.getInstance();

this.user = mAuth.getCurrentUser();
if (this.user == null) {
FirebaseUser user = mAuth.getCurrentUser();
if (user == null) {
callbackNoUser(callback, false);
} else {
Log.d("USRC", this.user.getUid());
userCallback(this.user, callback);
Log.d("USRC", user.getUid());
userCallback(user, callback);
}
}

// TODO: Check these things
@ReactMethod
public void googleLogin(String IdToken, final Callback callback) {
mAuth = FirebaseAuth.getInstance();

AuthCredential credential = GoogleAuthProvider.getCredential(IdToken, null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
if (task.isSuccessful()) {
FirestackAuthModule.this.user = task.getResult().getUser();
userCallback(FirestackAuthModule.this.user, callback);
userCallback(task.getResult().getUser(), callback);
} else {
userErrorCallback(task, callback);
}
Expand All @@ -495,17 +467,14 @@ public void onComplete(@NonNull Task<AuthResult> task) {

@ReactMethod
public void facebookLogin(String Token, final Callback callback) {
mAuth = FirebaseAuth.getInstance();

AuthCredential credential = FacebookAuthProvider.getCredential(Token);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
if (task.isSuccessful()) {
FirestackAuthModule.this.user = task.getResult().getUser();
userCallback(FirestackAuthModule.this.user, callback);
userCallback(task.getResult().getUser(), callback);
} else {
userErrorCallback(task, callback);
}
Expand All @@ -517,24 +486,15 @@ public void onComplete(@NonNull Task<AuthResult> task) {
}

// Internal helpers
private void userCallback(FirebaseUser passedUser, final Callback callback) {

if (passedUser == null) {
mAuth = FirebaseAuth.getInstance();
this.user = mAuth.getCurrentUser();
} else {
this.user = passedUser;
}

if (this.user != null) {
this.user.getToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
private void userCallback(final FirebaseUser user, final Callback callback) {
if (user != null) {
user.getToken(true).addOnCompleteListener(new OnCompleteListener<GetTokenResult>() {
@Override
public void onComplete(@NonNull Task<GetTokenResult> task) {
try {
if (task.isSuccessful()) {
WritableMap userMap = getUserMap();
final String token = task.getResult().getToken();
userMap.putString("token", token);
WritableMap userMap = getUserMap(user);
userMap.putString("token", task.getResult().getToken());
callback.invoke(null, userMap);
} else {
userErrorCallback(task, callback);
Expand Down Expand Up @@ -567,11 +527,8 @@ private void userExceptionCallback(Exception ex, final Callback onFail) {
onFail.invoke(error);
}

private WritableMap getUserMap() {
private WritableMap getUserMap(FirebaseUser user) {
WritableMap userMap = Arguments.createMap();

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

if (user != null) {
final String email = user.getEmail();
final String uid = user.getUid();
Expand Down
Loading

0 comments on commit 7774ac1

Please sign in to comment.