Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc authentication #252

Merged
merged 11 commits into from
Jan 23, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import com.google.firebase.auth.UserProfileChangeRequest;
import com.google.firebase.auth.FacebookAuthProvider;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GetTokenResult;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.auth.EmailAuthProvider;


import io.fullstack.firestack.Utils;

Expand All @@ -50,7 +53,7 @@ public FirestackAuth(ReactApplicationContext reactContext) {
mReactContext = reactContext;
mAuth = FirebaseAuth.getInstance();

Log.d(TAG, "New FirestackAuth instance");
Log.d(TAG, "New FirestackAuth instance");
}

@Override
Expand Down Expand Up @@ -165,6 +168,43 @@ public void signInWithProvider(final String provider, final String authToken, fi
Utils.todoNote(TAG, "signInWithProvider", callback);
}

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

if (user != null) {
AuthCredential credential = EmailAuthProvider.getCredential(email, password);
user
.linkWithCredential(credential)
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
try {
if (task.isSuccessful()) {
Log.d(TAG, "user linked with password credential");
userCallback(mAuth.getCurrentUser(), callback);
} else {
userErrorCallback(task, callback);
}
} catch (Exception ex) {
userExceptionCallback(ex, callback);
}
}
});
} else {
callbackNoUser(callback, true);
}
}

@ReactMethod
public void link(final String provider, final String authToken, final String authSecret, final Callback callback) {
if (provider.equals("password")) {
linkPassword(authToken, authSecret, callback);
} else
// TODO other providers
Utils.todoNote(TAG, "linkWithProvider", callback);
}

@ReactMethod
public void signInAnonymously(final Callback callback) {
Log.d(TAG, "signInAnonymously:called:");
Expand Down Expand Up @@ -435,9 +475,32 @@ public void signOut(final Callback callback) {
callback.invoke(null, resp);
}

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

if (user == null) {
callbackNoUser(callback, false);
} else {
user.reload()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Log.d(TAG, "user reloaded");
userCallback(mAuth.getCurrentUser(), callback);
} else {
userErrorCallback(task, callback);
}
}
});
}
}

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

if (user == null) {
callbackNoUser(callback, false);
} else {
Expand Down Expand Up @@ -513,19 +576,15 @@ public void onComplete(@NonNull Task<GetTokenResult> task) {

private void userErrorCallback(Task task, final Callback onFail) {
WritableMap error = Arguments.createMap();
error.putInt("errorCode", task.getException().hashCode());
error.putString("errorMessage", task.getException().getMessage());
error.putString("allErrorMessage", task.getException().toString());

error.putString("code", ((FirebaseAuthException) task.getException()).getErrorCode());
error.putString("message", task.getException().getMessage());
onFail.invoke(error);
}

private void userExceptionCallback(Exception ex, final Callback onFail) {
WritableMap error = Arguments.createMap();
error.putInt("errorCode", ex.hashCode());
error.putString("errorMessage", ex.getMessage());
error.putString("allErrorMessage", ex.toString());

error.putInt("code", ex.hashCode());
error.putString("message", ex.getMessage());
onFail.invoke(error);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,31 +165,42 @@ public void unsubscribeFromTopic(String topic, final Callback callback) {
}
}

// String senderId, String messageId, String messageType,
@ReactMethod
public void send(String senderId, String messageId, String messageType, ReadableMap params, final Callback callback) {
public void send(ReadableMap params, final Callback callback) {
ReadableMap data = params.getMap("data");
FirebaseMessaging fm = FirebaseMessaging.getInstance();
RemoteMessage.Builder remoteMessage = new RemoteMessage.Builder(senderId);
remoteMessage.setMessageId(messageId);
remoteMessage.setMessageType(messageType);
ReadableMapKeySetIterator iterator = params.keySetIterator();
RemoteMessage.Builder remoteMessage = new RemoteMessage.Builder(params.getString("sender"));

remoteMessage.setMessageId(params.getString("id"));
remoteMessage.setMessageType(params.getString("type"));

if (params.hasKey("ttl")) {
remoteMessage.setTtl(params.getInt("ttl"));
}

if (params.hasKey("collapseKey")) {
remoteMessage.setCollapseKey(params.getString("collapseKey"));
}

ReadableMapKeySetIterator iterator = data.keySetIterator();

while (iterator.hasNextKey()) {
String key = iterator.nextKey();
ReadableType type = params.getType(key);
ReadableType type = data.getType(key);
if (type == ReadableType.String) {
remoteMessage.addData(key, params.getString(key));
Log.d(TAG, "Firebase send: " + key);
Log.d(TAG, "Firebase send: " + params.getString(key));
remoteMessage.addData(key, data.getString(key));
}
}

try {
fm.send(remoteMessage.build());
WritableMap res = Arguments.createMap();
res.putString("status", "success");
Log.d(TAG, "send: Message sent");
callback.invoke(null, res);
} catch (Exception e) {
Log.e(TAG, "Error sending message", e);
Log.e(TAG, "send: error sending message", e);
WritableMap error = Arguments.createMap();
error.putString("code", e.toString());
error.putString("message", e.toString());
Expand Down
2 changes: 1 addition & 1 deletion docs/api/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ Refreshes the current user.

```javascript
firestack.auth().currentUser
.getToken()
.reload()
.then((user) => {})
.catch();
```
Expand Down
1 change: 0 additions & 1 deletion docs/api/storage

This file was deleted.

91 changes: 91 additions & 0 deletions docs/api/storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

# Storage

Firestack mimics the [Web Firebase SDK Storage](https://firebase.google.com/docs/storage/web/start), whilst
providing some iOS and Android specific functionality.

All Storage operations are accessed via `storage()`.

## Uploading files

### Simple

```javascript
firestack.storage()
.ref('/files/1234')
.putFile('/path/to/file/1234')
.then(uploadedFile => {
//success
})
.catch(err => {
//Error
});
```

### Listen to upload state

```javascript
const unsubscribe = firestack.storage()
.ref('/files/1234')
.putFile('/path/to/file/1234')
.on('state_changed', snapshot => {
//Current upload state
}, err => {
//Error
unsubscribe();
}, uploadedFile => {
//Success
unsubscribe();
});
```

## Downloading files

### Simple

```javascript
firestack.storage()
.ref('/files/1234')
.downloadFile('/path/to/save/file')
.then(downloadedFile => {
//success
})
.catch(err => {
//Error
});
```

### Listen to download state

```javascript
const unsubscribe = firestack.storage()
.ref('/files/1234')
.downloadFile('/path/to/save/file')
.on('state_changed', snapshot => {
//Current download state
}, err => {
//Error
unsubscribe();
}, downloadedFile => {
//Success
unsubscribe();
});
```

## TODO

There are a few methods which have not yet been implemented for Storage:

### Reference
- put()
- putString()

### UploadTask
- cancel()
- pause()
- resume()

### DownloadTask
- cancel()
- pause()
- resume()
9 changes: 9 additions & 0 deletions lib/modules/auth/Email.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
credential(email, password) {
return {
token: email,
secret: password,
provider: 'password',
};
},
};
Loading