-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources.js
172 lines (140 loc) · 5.93 KB
/
resources.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
/*
NanoPlay Website
Copyright (C) Subnodal Technologies. All Rights Reserved.
https://nanoplay.subnodal.com
Licenced by the Subnodal Open-Source Licence, which can be found at LICENCE.md.
*/
namespace("com.subnodal.nanoplay.website.resources", function(exports) {
var subElements = require("com.subnodal.subelements");
var firebaseConfig = {
apiKey: "AIzaSyDE0WI96lbx9mUXjN-ohNAWdBljrsB_B1A",
authDomain: "nanoplay-platform.firebaseapp.com",
projectId: "nanoplay-platform",
databaseURL: "https://nanoplay-platform-default-rtdb.europe-west1.firebasedatabase.app",
storageBucket: "nanoplay-platform.appspot.com",
messagingSenderId: "478037672867",
appId: "1:478037672867:web:799be2947cbc2dffc31016",
measurementId: "G-X5YC4X97CZ",
};
firebase.initializeApp(firebaseConfig);
firebase.analytics();
var currentUser = {
uid: null,
username: null
};
var userChangeListeners = [];
exports.errorCodes = {
UNKNOWN: 0,
ACCOUNT_SUSPENDED: 1,
PRIVATE_TOKEN_CHANGED: 2,
UNABLE_TO_SET: 3
};
exports.userSettings = {
USERNAME: "users/{uid}/username"
};
function triggerUserChangeListeners(isSignedIn, isNewUser) {
for (var i = 0; i < userChangeListeners.length; i++){
userChangeListeners[i](isSignedIn, isNewUser);
}
}
exports.authenticateWithSubnodal = function(public, private) {
return new Promise(function(resolve, reject) {
function authenticateFirstTimeUser() {
firebase.auth().createUserWithEmailAndPassword(public + "@accounts.subnodal.com", private).then(function() {
resolve(true);
}).catch(function(error) {
reject({message: error, code: exports.errorCodes.UNKNOWN});
});
}
firebase.auth().signInWithEmailAndPassword(public + "@accounts.subnodal.com", private).then(function() {
resolve(false);
}).catch(function(error) {
switch (error.code) {
case "auth/invalid-email":
reject({message: "Public token is malformatted", code: exports.errorCodes.UNKNOWN});
break;
case "auth/user-disabled":
reject({message: "This user account has been suspended", code: exports.errorCodes.ACCOUNT_SUSPENDED});
break;
case "auth/user-not-found":
authenticateFirstTimeUser();
break;
case "auth/wrong-password":
reject({message: "The private token was recently changed", code: exports.errorCodes.PRIVATE_TOKEN_CHANGED});
break;
default:
reject({message: error.message, code: exports.errorCodes.UNKNOWN});
break;
}
})
});
};
exports.applyUserSetting = function(setting, value) {
return new Promise(function(resolve, reject) {
firebase.database().ref(setting.replace(/{uid}/g, currentUser.uid)).set(value).then(resolve).catch(function(error) {
reject({message: error.message, code: exports.errorCodes.UNABLE_TO_SET})
});
});
};
exports.getCurrentUser = function() {
return currentUser;
};
exports.registerUserChangeListener = function(callback) {
userChangeListeners.push(callback);
};
exports.syncAppToCloud = function(code, manifest, isNew = false) {
if (typeof(manifest.id) != "string" || manifest.id.trim() == "") {
throw new Error("Manifest has no app ID");
}
return firebase.database().ref("users/" + currentUser.uid + "/apps/" + manifest.id).set({
code: code,
manifest: {
dateCreated: new Date().getTime(),
...manifest,
dateModified: new Date().getTime()
}
});
};
exports.getAppFromCloud = function(id) {
if (typeof(id) != "string" || id.trim() == "") {
throw new Error("Manifest has no app ID");
}
return new Promise(function(resolve, reject) {
firebase.database().ref("users/" + currentUser.uid + "/apps/" + id).once("value", function(snapshot) {
if (snapshot.val() == null) {
reject("No app with the specified app ID could not be found");
return;
}
resolve(snapshot.val());
}).catch(function(error) {
reject(error);
});
});
};
exports.getAppsListFromCloud = function() {
return new Promise(function(resolve, reject) {
firebase.database().ref("users/" + currentUser.uid + "/apps").orderByChild("manifest/dateModified").once("value", function(snapshot) {
var appsList = [];
snapshot.forEach(function(childSnapshot) {
appsList.unshift(childSnapshot.val());
})
resolve(appsList);
});
});
};
subElements.ready(function() {
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
currentUser.uid = user.uid;
firebase.database().ref("users/" + currentUser.uid + "/username").on("value", function(snapshot) {
currentUser.username = snapshot.val();
triggerUserChangeListeners(true, currentUser.username == null);
});
} else {
currentUser.uid = null;
currentUser.username = null;
triggerUserChangeListeners(false, false);
}
});
});
});