From 7d6536eb1408cbd192b8a428867f39b3d6f55718 Mon Sep 17 00:00:00 2001 From: salakar Date: Tue, 17 Jan 2017 11:59:21 +0000 Subject: [PATCH 01/10] standardise auth messages to match web sdk error messages --- .../firestack/auth/FirestackAuth.java | 13 ++++---- lib/modules/auth.js | 30 +++++++++---------- lib/utils/index.js | 13 +++++--- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java b/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java index 89cce37..bc7ae97 100644 --- a/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java +++ b/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java @@ -25,6 +25,7 @@ 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; @@ -513,19 +514,15 @@ public void onComplete(@NonNull Task 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); } diff --git a/lib/modules/auth.js b/lib/modules/auth.js index a067952..75117db 100644 --- a/lib/modules/auth.js +++ b/lib/modules/auth.js @@ -3,7 +3,7 @@ import { NativeModules, NativeEventEmitter } from 'react-native'; import User from './user'; import { Base } from './base'; -import { promisify } from '../utils'; +import { promisify, toWebSDKErrorCode } from '../utils'; const FirestackAuth = NativeModules.FirestackAuth; const FirestackAuthEvt = new NativeEventEmitter(FirestackAuth); @@ -75,7 +75,7 @@ export default class Auth extends Base { */ createUserWithEmailAndPassword(email: string, password: string): Promise { this.log.info('Creating user with email and password', email); - return promisify('createUserWithEmail', FirestackAuth)(email, password); + return promisify('createUserWithEmail', FirestackAuth, 'auth/')(email, password); } /** @@ -86,7 +86,7 @@ export default class Auth extends Base { */ signInWithEmailAndPassword(email: string, password: string): Promise { this.log.info('Signing in user with email and password', email); - return promisify('signInWithEmail', FirestackAuth)(email, password); + return promisify('signInWithEmail', FirestackAuth, 'auth/')(email, password); } // TODO move user methods to User class @@ -97,14 +97,14 @@ export default class Auth extends Base { * @return {Promise} A promise resolved upon completion */ updateEmail(email: string): Promise { - return promisify('updateUserEmail', FirestackAuth)(email); + return promisify('updateUserEmail', FirestackAuth, 'auth/')(email); } /** * Send verification email to current user. */ sendEmailVerification(): Promise { - return promisify('sendEmailVerification', FirestackAuth)(); + return promisify('sendEmailVerification', FirestackAuth, 'auth/')(); } /** @@ -113,7 +113,7 @@ export default class Auth extends Base { * @return {Promise} */ updatePassword(password: string): Promise { - return promisify('updateUserPassword', FirestackAuth)(password); + return promisify('updateUserPassword', FirestackAuth, 'auth/')(password); } /** @@ -122,7 +122,7 @@ export default class Auth extends Base { * @return {Promise} */ updateProfile(updates: Object = {}): Promise { - return promisify('updateUserProfile', FirestackAuth)(updates); + return promisify('updateUserProfile', FirestackAuth, 'auth/')(updates); } /** @@ -139,7 +139,7 @@ export default class Auth extends Base { * @return {Promise} A promise resolved upon completion */ signInWithCredential(credential: CredentialType): Promise { - return promisify('signInWithProvider', FirestackAuth)(credential.provider, credential.token, credential.secret); + return promisify('signInWithProvider', FirestackAuth, 'auth/')(credential.provider, credential.token, credential.secret); } /** @@ -147,7 +147,7 @@ export default class Auth extends Base { * @return {Promise} A promise resolved upon completion */ reauthenticateUser(credential: CredentialType): Promise { - return promisify('reauthenticateWithCredentialForProvider', FirestackAuth)(credential.provider, credential.token, credential.secret); + return promisify('reauthenticateWithCredentialForProvider', FirestackAuth, 'auth/')(credential.provider, credential.token, credential.secret); } /** @@ -155,7 +155,7 @@ export default class Auth extends Base { * @return {Promise} A promise resolved upon completion */ signInAnonymously(): Promise { - return promisify('signInAnonymously', FirestackAuth)(); + return promisify('signInAnonymously', FirestackAuth, 'auth/')(); } /** @@ -163,7 +163,7 @@ export default class Auth extends Base { * @param {string} email The email to send password reset instructions */ sendPasswordResetEmail(email: string): Promise { - return promisify('sendPasswordResetWithEmail', FirestackAuth)(email); + return promisify('sendPasswordResetWithEmail', FirestackAuth, 'auth/')(email); } /** @@ -171,7 +171,7 @@ export default class Auth extends Base { * @return {Promise} */ deleteUser(): Promise { - return promisify('deleteUser', FirestackAuth)(); + return promisify('deleteUser', FirestackAuth, 'auth/')(); } /** @@ -179,7 +179,7 @@ export default class Auth extends Base { * @return {Promise} */ getToken(): Promise { - return promisify('getToken', FirestackAuth)(); + return promisify('getToken', FirestackAuth, 'auth/')(); } @@ -188,7 +188,7 @@ export default class Auth extends Base { * @return {Promise} */ signOut(): Promise { - return promisify('signOut', FirestackAuth)(); + return promisify('signOut', FirestackAuth, 'auth/')(); } /** @@ -196,7 +196,7 @@ export default class Auth extends Base { * @return {Promise} */ getCurrentUser(): Promise { - return promisify('getCurrentUser', FirestackAuth)(); + return promisify('getCurrentUser', FirestackAuth, 'auth/')(); } /** diff --git a/lib/utils/index.js b/lib/utils/index.js index 54bd80d..1baff1e 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -4,14 +4,18 @@ const hasOwnProperty = Object.hasOwnProperty; const DEFAULT_CHUNK_SIZE = 50; // internal promise handler -const _handler = (resolve, reject, err, resp) => { +const _handler = (resolve, reject, errorPrefix, err, resp) => { // resolve / reject after events etc setImmediate(() => { - if (err) return reject(err); + if (err) return reject(errorPrefix ? { code: toWebSDKErrorCode(err.code), message: err.message } : err); return resolve(resp); }); }; +export function toWebSDKErrorCode(code, prefix) { + return code.toLowerCase().replace('error_', prefix).replace(/_/g, '-'); +} + /** * Deep get a value from an object. * @website https://github.com/Salakar/deeps @@ -131,13 +135,14 @@ export function noop(): void { * Wraps a native module method to support promises. * @param fn * @param NativeModule + * @param errorPrefix */ -export function promisify(fn: Function, NativeModule: Object): Function { +export function promisify(fn: Function, NativeModule: Object, errorPrefix): Function { return (...args) => { return new Promise((resolve, reject) => { const _fn = typeof fn === 'function' ? fn : NativeModule[fn]; if (!_fn || typeof _fn !== 'function') return reject(new Error('Missing function for promisify.')); - return _fn.apply(NativeModule, [...args, _handler.bind(_handler, resolve, reject)]); + return _fn.apply(NativeModule, [...args, _handler.bind(_handler, resolve, reject, errorPrefix)]); }); }; } From 661f378450b6494ec8704b482988c371afebe605 Mon Sep 17 00:00:00 2001 From: salakar Date: Tue, 17 Jan 2017 12:02:10 +0000 Subject: [PATCH 02/10] standardise auth messages to match web sdk error messages --- lib/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/index.js b/lib/utils/index.js index 1baff1e..d4d0bdb 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -7,7 +7,7 @@ const DEFAULT_CHUNK_SIZE = 50; const _handler = (resolve, reject, errorPrefix, err, resp) => { // resolve / reject after events etc setImmediate(() => { - if (err) return reject(errorPrefix ? { code: toWebSDKErrorCode(err.code), message: err.message } : err); + if (err) return reject(errorPrefix ? { code: toWebSDKErrorCode(err.code, errorPrefix), message: err.message } : err); return resolve(resp); }); }; From 145949bf1e3d8f269b0541831536e0e8c21051db Mon Sep 17 00:00:00 2001 From: salakar Date: Tue, 17 Jan 2017 18:05:15 +0000 Subject: [PATCH 03/10] - JS: added EmailAuthProvider interface - .credential(email, password); - Android/JS: Added auth().link(credential); TODO other providers. @chrisbianca - ios needs doing if you need this / have time =] @ehesp needs testing --- .../firestack/auth/FirestackAuth.java | 43 ++++++++++++++++++- lib/modules/auth/Email.js | 9 ++++ lib/modules/{auth.js => auth/index.js} | 18 ++++++-- 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 lib/modules/auth/Email.js rename lib/modules/{auth.js => auth/index.js} (93%) diff --git a/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java b/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java index bc7ae97..86e320a 100644 --- a/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java +++ b/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java @@ -29,6 +29,8 @@ 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; @@ -51,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 @@ -166,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() { + @Override + public void onComplete(@NonNull Task 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:"); @@ -514,7 +553,7 @@ public void onComplete(@NonNull Task task) { private void userErrorCallback(Task task, final Callback onFail) { WritableMap error = Arguments.createMap(); - error.putString("code", ((FirebaseAuthException)task.getException()).getErrorCode()); + error.putString("code", ((FirebaseAuthException) task.getException()).getErrorCode()); error.putString("message", task.getException().getMessage()); onFail.invoke(error); } diff --git a/lib/modules/auth/Email.js b/lib/modules/auth/Email.js new file mode 100644 index 0000000..92f4d68 --- /dev/null +++ b/lib/modules/auth/Email.js @@ -0,0 +1,9 @@ +export default { + credential(email, password) { + return { + token: email, + secret: password, + provider: 'password', + }; + }, +}; diff --git a/lib/modules/auth.js b/lib/modules/auth/index.js similarity index 93% rename from lib/modules/auth.js rename to lib/modules/auth/index.js index 75117db..f8ee611 100644 --- a/lib/modules/auth.js +++ b/lib/modules/auth/index.js @@ -1,9 +1,10 @@ // @flow import { NativeModules, NativeEventEmitter } from 'react-native'; -import User from './user'; -import { Base } from './base'; -import { promisify, toWebSDKErrorCode } from '../utils'; +import User from './../user'; +import { Base } from './../base'; +import EmailAuthProvider from './Email'; +import { promisify } from './../../utils'; const FirestackAuth = NativeModules.FirestackAuth; const FirestackAuthEvt = new NativeEventEmitter(FirestackAuth); @@ -22,6 +23,9 @@ export default class Auth extends Base { this._authResult = null; this.authenticated = false; + // attach auth providers + // TODO add missing providers + this.EmailAuthProvider = EmailAuthProvider; // start listening straight away // generally though the initial event fired will get ignored // but this is ok as we fake it with the getCurrentUser below @@ -125,6 +129,14 @@ export default class Auth extends Base { return promisify('updateUserProfile', FirestackAuth, 'auth/')(updates); } + /** + * + * @param credential + */ + link(credential: CredentialType) { + return promisify('link', FirestackAuth, 'auth/')(credential.provider, credential.token, credential.secret); + } + /** * Sign the user in with a custom auth token * @param {string} customToken A self-signed custom auth token. From d20ecda1d75f9cdf1d21ed4899c30b320f124e0a Mon Sep 17 00:00:00 2001 From: salakar Date: Wed, 18 Jan 2017 10:09:44 +0000 Subject: [PATCH 04/10] misc --- lib/utils/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/utils/index.js b/lib/utils/index.js index d4d0bdb..bce8ba7 100644 --- a/lib/utils/index.js +++ b/lib/utils/index.js @@ -7,7 +7,7 @@ const DEFAULT_CHUNK_SIZE = 50; const _handler = (resolve, reject, errorPrefix, err, resp) => { // resolve / reject after events etc setImmediate(() => { - if (err) return reject(errorPrefix ? { code: toWebSDKErrorCode(err.code, errorPrefix), message: err.message } : err); + if (err) return reject(errorPrefix ? { code: toWebSDKErrorCode(err.code || err.errCode || err.errorCode || '', errorPrefix), message: err.message } : err); return resolve(resp); }); }; From bd574be5bbdcf46c27adfa14ca12b39bad5dae0e Mon Sep 17 00:00:00 2001 From: Chris Bianca Date: Wed, 18 Jan 2017 14:35:36 +0000 Subject: [PATCH 05/10] Add storage documentation --- docs/api/storage | 1 - docs/api/storage.md | 91 ++++++++++++++++++++++++++++++++ lib/modules/storage/reference.js | 2 - 3 files changed, 91 insertions(+), 3 deletions(-) delete mode 100644 docs/api/storage create mode 100644 docs/api/storage.md diff --git a/docs/api/storage b/docs/api/storage deleted file mode 100644 index 8b13789..0000000 --- a/docs/api/storage +++ /dev/null @@ -1 +0,0 @@ - diff --git a/docs/api/storage.md b/docs/api/storage.md new file mode 100644 index 0000000..11c7bf4 --- /dev/null +++ b/docs/api/storage.md @@ -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() diff --git a/lib/modules/storage/reference.js b/lib/modules/storage/reference.js index 267153f..6abfe99 100644 --- a/lib/modules/storage/reference.js +++ b/lib/modules/storage/reference.js @@ -66,8 +66,6 @@ export default class StorageRef extends ReferenceBase { } //Additional methods compared to Web API - - //TODO: Listeners /** * Downloads a reference to the device * @param {String} filePath Where to store the file From f5ed4a1c363fe0690a89dfbdac94d8156b849f79 Mon Sep 17 00:00:00 2001 From: Chris Bianca Date: Wed, 18 Jan 2017 14:35:55 +0000 Subject: [PATCH 06/10] Fix incorrect db path --- lib/modules/database/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/database/index.js b/lib/modules/database/index.js index fe0a21a..f745391 100644 --- a/lib/modules/database/index.js +++ b/lib/modules/database/index.js @@ -192,7 +192,7 @@ export default class Database extends Base { if (this.subscriptions[handle] && this.subscriptions[handle][eventName]) { this.subscriptions[handle][eventName].forEach((cb) => { - cb(new Snapshot(new Reference(this, path.split('/'), modifiersString.split('|')), snapshot), body); + cb(new Snapshot(new Reference(this, path, modifiersString.split('|')), snapshot), body); }); } else { FirestackDatabase.off(path, modifiersString, eventName, () => { From 72d3d38a62ab9824978f9d4c9574f69b31728b82 Mon Sep 17 00:00:00 2001 From: salakar Date: Wed, 18 Jan 2017 17:26:52 +0000 Subject: [PATCH 07/10] added currentUser.reload() method - android only, needs ios --- .../firestack/auth/FirestackAuth.java | 23 +++++++++++++++++++ lib/modules/auth/index.js | 8 +++++++ lib/modules/user.js | 4 ++++ 3 files changed, 35 insertions(+) diff --git a/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java b/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java index 86e320a..3f194ba 100644 --- a/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java +++ b/android/src/main/java/io/fullstack/firestack/auth/FirestackAuth.java @@ -475,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() { + @Override + public void onComplete(@NonNull Task 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 { diff --git a/lib/modules/auth/index.js b/lib/modules/auth/index.js index f8ee611..e5d0591 100644 --- a/lib/modules/auth/index.js +++ b/lib/modules/auth/index.js @@ -186,6 +186,14 @@ export default class Auth extends Base { return promisify('deleteUser', FirestackAuth, 'auth/')(); } + /** + * Delete the current user + * @return {Promise} + */ + reloadUser(): Promise { + return promisify('reloadUser', FirestackAuth, 'auth/')(); + } + /** * get the token of current user * @return {Promise} diff --git a/lib/modules/user.js b/lib/modules/user.js index 429bd67..348a604 100644 --- a/lib/modules/user.js +++ b/lib/modules/user.js @@ -92,6 +92,10 @@ export default class User { return this._auth.deleteUser(...args); } + reload(...args) { + return this._auth.reload(...args); + } + // TODO valueOrNul token - optional promise getToken(...args) { return this._auth.getToken(...args); From 2c3eb5bb033bdbd5ae5105cd8aaa63698a6c99e7 Mon Sep 17 00:00:00 2001 From: salakar Date: Wed, 18 Jan 2017 17:47:13 +0000 Subject: [PATCH 08/10] messaging.send() now only accepts an object of type RemoteMessage as defined in messaging.js - this now includes all the previously missing options such as ttl and collapse key @chrisbianca / note to self - ios needs doing ;p --- .../messaging/FirestackMessaging.java | 31 +++++++++++------ lib/modules/messaging.js | 34 ++++++++++++------- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/android/src/main/java/io/fullstack/firestack/messaging/FirestackMessaging.java b/android/src/main/java/io/fullstack/firestack/messaging/FirestackMessaging.java index 7ef06d2..4114e76 100644 --- a/android/src/main/java/io/fullstack/firestack/messaging/FirestackMessaging.java +++ b/android/src/main/java/io/fullstack/firestack/messaging/FirestackMessaging.java @@ -165,21 +165,31 @@ 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)); } } @@ -187,9 +197,10 @@ public void send(String senderId, String messageId, String messageType, Readable 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()); diff --git a/lib/modules/messaging.js b/lib/modules/messaging.js index eb5da3b..1a59d70 100644 --- a/lib/modules/messaging.js +++ b/lib/modules/messaging.js @@ -5,6 +5,15 @@ import { promisify } from '../utils'; const FirestackMessaging = NativeModules.FirestackMessaging || NativeModules.FirestackCloudMessaging; const FirestackMessagingEvt = new NativeEventEmitter(FirestackMessaging); +type RemoteMessage = { + id: string, + type: string, + ttl?: number, + sender: string, + collapseKey?: string, + data: Object, +}; + /** * @class Messaging */ @@ -39,20 +48,21 @@ export default class Messaging extends Base { return promisify('getToken', FirestackMessaging)(); } - sendMessage(details: Object = {}, type: string = 'local') { - const methodName = `send${type == 'local' ? 'Local' : 'Remote'}`; - this.log.info('sendMessage', methodName, details); - return promisify(methodName, FirestackMessaging)(details); - } - - scheduleMessage(details: Object = {}, type: string = 'local') { - const methodName = `schedule${type == 'local' ? 'Local' : 'Remote'}`; - return promisify(methodName, FirestackMessaging)(details); - } + // sendMessage(details: Object = {}, type: string = 'local') { + // const methodName = `send${type == 'local' ? 'Local' : 'Remote'}`; + // this.log.info('sendMessage', methodName, details); + // return promisify(methodName, FirestackMessaging)(details); + // } + // + // scheduleMessage(details: Object = {}, type: string = 'local') { + // const methodName = `schedule${type == 'local' ? 'Local' : 'Remote'}`; + // return promisify(methodName, FirestackMessaging)(details); + // } // OLD - send(senderId, messageId, messageType, msg) { - return promisify('send', FirestackMessaging)(senderId, messageId, messageType, msg); + send(remoteMessage: RemoteMessage) { + if (!remoteMessage || !remoteMessage.data) return Promise.reject(new Error('Invalid remote message format provided.')); + return promisify('send', FirestackMessaging)(remoteMessage); } // From 8cda6c90f3864208c81415aca5fe5cebe921b585 Mon Sep 17 00:00:00 2001 From: salakar Date: Wed, 18 Jan 2017 17:52:58 +0000 Subject: [PATCH 09/10] auth().currentUser.reload() now correctly calls reloadUser - woops --- lib/modules/user.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/modules/user.js b/lib/modules/user.js index 348a604..a86f36f 100644 --- a/lib/modules/user.js +++ b/lib/modules/user.js @@ -93,7 +93,7 @@ export default class User { } reload(...args) { - return this._auth.reload(...args); + return this._auth.reloadUser(...args); } // TODO valueOrNul token - optional promise From 6a5c87867599933182b84a9ed8169cdb99898324 Mon Sep 17 00:00:00 2001 From: Elliot Hesp Date: Thu, 19 Jan 2017 22:04:44 +0000 Subject: [PATCH 10/10] Update authentication.md --- docs/api/authentication.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/authentication.md b/docs/api/authentication.md index 05f2ffe..4ae8684 100644 --- a/docs/api/authentication.md +++ b/docs/api/authentication.md @@ -227,7 +227,7 @@ Refreshes the current user. ```javascript firestack.auth().currentUser - .getToken() + .reload() .then((user) => {}) .catch(); ```