forked from NetsBlox/Snap--Build-Your-Own-Blocks
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
45 lines (38 loc) · 1.3 KB
/
main.js
File metadata and controls
45 lines (38 loc) · 1.3 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
/* globals SnapCloud, SERVER_URL, NetsBloxMorph, WorldMorph, utils */
var world;
window.onload = function () {
world = new WorldMorph(document.getElementById('world'));
world.worldCanvas.focus();
new NetsBloxMorph().openIn(world);
loop();
// if not logged in, make sure
if (!SnapCloud.username) {
// gets user info: username, email
var getProfile = function() {
const request = new XMLHttpRequest();
request.open('POST', `${SERVER_URL}/api`, true);
request.withCredentials = true;
const data = {
api: false,
return_user: true,
silent: true
};
return utils.requestPromise(request, data)
.then(function(res) {
if (!res.responseText) throw new Error('Access denied. You are not logged in.');
let user = JSON.parse(res.responseText);
return user;
});
};
// check to see if loggedin
getProfile().then(user => {
// notify the client that we are logged in
SnapCloud.username = user.username;
SnapCloud.password = true;
});
}
};
function loop() {
requestAnimationFrame(loop);
world.doOneCycle();
}