-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebaseHelper.js
More file actions
68 lines (61 loc) · 1.92 KB
/
firebaseHelper.js
File metadata and controls
68 lines (61 loc) · 1.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
var fb = require('firebase');
var fs = require('fs');
var token = "";
var chUUID = "";
var url = "";
var attachments = [];
var printError = function(e) {
for(var propName in e) {
console.log(propName + ": " + e[propName]);
}
}
function UndefinedPropertyError(property) {
this.kind = 'error#undefined-property';
this.property = property;
}
function ConfigFileNotFoundError(error) {
this.kind = 'error#config-file-not-found';
this.method = 'readFile';
this.error = error;
}
var startConnection = function(configFile, callback) {
if(callback) {
if (configFile) {
fs.readFile(configFile, function(e, data) {
if (e) {
console.log("Error opening config file.");
printError(e);
hostCallback(new ConfigFileNotFoundError(e));
} else {
console.log("Config file opened.");
var obj = JSON.parse(data);
token = obj.token;
chUUID = obj.uuid;
url = obj.firebaseUrl
connectToFirebase(fb, token, chUUID, url, callback);
}
});
} else { // if configFile
callback(new UndefinedPropertyError('configFile'));
} } else { // if callback
throw new UndefinedPropertyError('callback');
}
}
var connectToFirebase = function(fb, token, chUUID, url, callback) {
var firebase = new fb(url);
firebase.authWithCustomToken(token, function(e, auth) {
if (e) {
console.log("Error logging into firebase.");
printError(e);
// pass the error back to the host
callback(e);
} else {
console.log("Successful firebase login.");
new_firebase = new fb(url + '/users/' + auth.uid + '/devices/chillhubs/' + chUUID);
callback(null, new_firebase);
}
} );
}
module.exports = {
startConnection: startConnection
};