-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathfirestack.js
283 lines (243 loc) · 7.09 KB
/
firestack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* @providesModule Firestack
* @flow
*/
const firebase = require('firebase');
const app = require('firebase/app');
const db = require('firebase/database');
const storage = require('firebase/storage');
import {NativeModules, NativeAppEventEmitter, AsyncStorage} from 'react-native';
// TODO: Break out modules into component pieces
// i.e. auth component, storage component, etc.
const FirebaseHelper = NativeModules.Firestack;
import promisify from './promisify'
import RemoteConfig from './remoteConfig'
export class Firestack {
constructor(options) {
this.options = options || {};
this._remoteConfig = options.remoteConfig || {};
delete options.remoteConfig;
this.configured = this.options.configure || false;
this._debug = options.debug || false;
this.auth = null;
this.eventHandlers = {};
this.configure(options);
}
configure(opts) {
opts = opts || {};
const firestackOptions = Object.assign({}, this.options, opts);
this.appInstance = app.initializeApp(firestackOptions);
return promisify('configureWithOptions')(firestackOptions)
.then((...args) => {
this.configured = true;
return args;
}).catch((err) => {})
}
// Auth
listenForAuth(callback) {
this.on('listenForAuth', auth => {
this._updateUserTokenFromPromise(Promise.resolve(auth));
});
const sub = this.on('listenForAuth', callback);
FirebaseHelper.listenForAuth();
return promisify(() => sub)(sub);
}
unlistenForAuth() {
this.off('listenForAuth');
return promisify('unlistenForAuth')();
}
setStore(store) {
if (store) {
this._store = store;
}
}
/**
* Create a user with the email/password functionality
* @param {string} email The user's email
* @param {string} password The user's password
* @return {Promise} A promise indicating the completion
*/
createUserWithEmail(email, password) {
return promisify('createUserWithEmail')(email, password);
}
/**
* Sign a user in with email/password
* @param {string} email The user's email
* @param {string} password The user's password
* @return {Promise} A promise that is resolved upon completion
*/
signInWithEmail(email, password) {
return this._updateUserTokenFromPromise(promisify('signInWithEmail')(email, password))
}
/**
* Sign the user in with a third-party authentication provider
* @param {string} provider The name of the provider to use for login
* @param {string} authToken The authToken granted by the provider
* @param {string} authSecret The authToken secret granted by the provider
* @return {Promise} A promise resolved upon completion
*/
signInWithProvider(provider, authToken, authSecret) {
return this._updateUserTokenFromPromise(promisify('signInWithProvider')(provider, authToken, authSecret))
}
/**
* Sign the user in with a custom auth token
* @param {string} customToken A self-signed custom auth token.
* @return {Promise} A promise resolved upon completion
*/
signInWithCustomToken(customToken) {
return this._updateUserTokenFromPromise(promisify('signInWithCustomToken')(customToken))
}
/**
* Reauthenticate a user with a third-party authentication provider
* @param {string} provider The provider name
* @param {string} token The authToken granted by the provider
* @param {string} secret The authTokenSecret granted by the provider
* @return {Promise} A promise resolved upon completion
*/
reauthenticateWithCredentialForProvider(provider, token, secret) {
return this._updateUserTokenFromPromise(promisify('reauthenticateWithCredentialForProvider')(provider, token, secret))
}
_updateUserTokenFromPromise(promise) {
return promise;
}
/**
* Update the current user's email
* @param {string} email The user's _new_ email
* @return {Promise} A promise resolved upon completion
*/
updateUserEmail(email) {
return promisify('updateUserEmail')(email);
}
/**
* Update the current user's password
* @param {string} email the new password
* @return {Promise}
*/
updatePassword(password) {
return promisify('updateUserPassword')(password);
}
/**
* Send reset password instructions via email
* @param {string} email The email to send password reset instructions
*/
sendPasswordResetWithEmail(email) {
return promisify('sendPasswordResetWithEmail')(email);
}
/**
* Delete the current user
* @return {Promise}
*/
deleteUser() {
return promisify('deleteUser')()
}
/**
* get the token of current user
* @return {Promise}
*/
getToken() {
return promisify('getToken')()
}
/**
* Update the current user's profile
* @param {Object} obj An object containing the keys listed [here](https://firebase.google.com/docs/auth/ios/manage-users#update_a_users_profile)
* @return {Promise}
*/
updateUserProfile(obj) {
return promisify('updateUserProfile')(obj);
}
/**
* Sign the current user out
* @return {Promise}
*/
signOut() {
return promisify('signOut')();
}
/**
* Get the currently signed in user
* @return {Promise}
*/
getCurrentUser() {
return promisify('getCurrentUser')();
}
// Analytics
/**
* Log an event
* @param {string} name The name of the event
* @param {object} props An object containing string-keys
* @return {Promise}
*/
logEventWithName(name, props) {
return promisify('logEventWithName')(name, props);
}
// Storage
//
// /**
// * Configure the library to store the storage url
// * @param {string} url A string of your firebase storage url
// * @return {Promise}
// */
// setStorageUrl(url) {
// return promisify('setStorageUrl')(url);
// }
/**
* Upload a filepath
* @param {string} name The destination for the file
* @param {string} filepath The local path of the file
* @param {object} metadata An object containing metadata
* @return {Promise}
*/
uploadFile(name, filepath, metadata) {
return promisify('uploadFile')(name, filepath, metadata);
}
// database
get database() {
db.enableLogging(this._debug);
return this.appInstance.database();
}
/**
* The native storage object provided by Firebase
* @return {instance}
*/
get storage() {
return storage();
}
// other
get ServerValue() {
return db.ServerValue;
}
/**
* remote config
*/
get remoteConfig() {
if (!this.remoteConfig) {
this.remoteConfig = new RemoteConfig(this._remoteConfig);
}
return this.remoteConfig;
}
/**
* app instance
**/
get app() {
return this.appInstance;
}
/**
* Redux store
**/
get store() {
return this._store;
}
on(name, cb) {
if (!this.eventHandlers[name]) {
this.eventHandlers[name] = [];
}
const sub = NativeAppEventEmitter.addListener(name, cb);
this.eventHandlers[name].push(sub);
return sub;
}
off(name) {
if (this.eventHandlers[name]) {
this.eventHandlers.forEach(subscription => subscription.remove());
}
}
}
export default Firestack