-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathfirestack.ios.js
More file actions
207 lines (176 loc) · 4.92 KB
/
firestack.ios.js
File metadata and controls
207 lines (176 loc) · 4.92 KB
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
/**
* @providesModule Firestack
* @flow
*/
const FirebaseManager = require('firebase');
const app = require('firebase/app');
const db = require('firebase/database');
const storage = require('firebase/storage');
import {NativeModules, NativeAppEventEmitter} from 'react-native';
const FirebaseHelper = NativeModules.Firestack;
const promisify = fn => (...args) => {
return new Promise((resolve, reject) => {
const handler = (err, resp) => err ? reject(err) : resolve(resp);
args.push(handler);
(typeof fn === 'function' ? fn : FirebaseHelper[fn])
.call(FirebaseHelper, ...args);
});
};
export default class Firestack {
constructor(options) {
this.options = options;
this.appInstance = app.initializeApp(options);
this.configured = false;
this.eventHandlers = {};
}
configure() {
return promisify('configure')()
.then((...args) => {
this.configured = true;
return args;
});
}
// Auth
listenForAuth(callback) {
const sub = this.on('listenForAuth', callback);
FirebaseHelper.listenForAuth();
return promisify(() => sub)(sub);
}
unlistenForAuth() {
this.off('listenForAuth');
return promisify('unlistenForAuth')();
}
/**
* 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 promisify('signInWithEmail')(email, password);
}
signInWithProvider(provider, authToken, authSecret) {
return promisify('signInWithProvider')(provider, authToken, authSecret);
}
reauthenticateWithCredentialForProvider(provider, token, secret) {
return promisify('reauthenticateWithCredentialForProvider')(provider, token, secret);
}
/**
* 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')()
}
/**
* 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() {
return db();
}
/**
* The native storage object provided by Firebase
* @return {instance}
*/
get storage() {
return storage();
}
// other
get ServerValue() {
return db.ServerValue;
}
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());
}
}
}