authorization

beta.r3js.org
-=yb4f310 2017-10-10 14:02:14 +02:00
parent 9ad44012ce
commit 73bbd79aea
2 changed files with 79 additions and 82 deletions

View File

@ -423,50 +423,34 @@ GameLib.Component.prototype.saveToRemoteAPI = function() {
GameLib.Component.prototype.save = function(remote) { GameLib.Component.prototype.save = function(remote) {
var event = GameLib.Event.GET_API_URL; this.buildIdToObject();
if (remote) { for (var property in this.idToObject) {
event = GameLib.Event.GET_REMOTE_API_URL if (
} this.idToObject.hasOwnProperty(property) &&
this.idToObject[property] instanceof GameLib.Component
) {
this.publish( var apiObject = this.idToObject[property].toApiObject();
event,
null,
function(data) {
this.buildIdToObject();
for (var property in this.idToObject) { apiObject.componentType = this.idToObject[property].componentType;
if (
this.idToObject.hasOwnProperty(property) &&
this.idToObject[property] instanceof GameLib.Component
) {
var apiObject = this.idToObject[property].toApiObject(); var storageDependencies = this.idToObject[property].getStorageDependencies();
apiObject.componentType = this.idToObject[property].componentType; for (var storageProperty in storageDependencies) {
if (storageDependencies.hasOwnProperty(storageProperty)) {
var storageDependencies = this.idToObject[property].getStorageDependencies(); apiObject[storageProperty] = storageDependencies[storageProperty];
for (var storageProperty in storageDependencies) {
if (storageDependencies.hasOwnProperty(storageProperty)) {
apiObject[storageProperty] = storageDependencies[storageProperty];
}
}
this.publish(
GameLib.Event.SAVE_COMPONENT,
{
apiUrl : data.apiUrl,
apiObject : apiObject
}
);
} }
} }
}.bind(this),
function(error) { this.publish(
console.error(error); GameLib.Event.SAVE_COMPONENT,
throw new Error('Failed to get API URL: ' + error.message); {
apiObject: apiObject,
remote: remote
}
);
} }
); }
}; };

View File

@ -199,6 +199,7 @@ GameLib.System.Storage.prototype.delete = function(data) {
xhr.setRequestHeader("Accept", "application/json"); xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("x-authorization", urlData.passwoid);
xhr.onreadystatechange = function () { xhr.onreadystatechange = function () {
if (this.readyState === 4) { if (this.readyState === 4) {
@ -246,62 +247,74 @@ GameLib.System.Storage.prototype.delete = function(data) {
}; };
/** /**
* 'Saves' data to baseURL * 'Saves' data to somewhere
*/ */
GameLib.System.Storage.prototype.save = function(data) { GameLib.System.Storage.prototype.save = function(data) {
if (typeof XMLHttpRequest === 'undefined') { var event = GameLib.Event.GET_API_URL;
console.log('Implement server side save here');
return; if (data.remote) {
event = GameLib.Event.GET_REMOTE_API_URL
} }
var xhr = new XMLHttpRequest(); this.publish(
event,
xhr.open( null,
'POST', function(urlData) {
data.apiUrl + '/component/create' if (typeof XMLHttpRequest === 'undefined') {
); console.log('Implement server side save here');
return;
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
try {
var response = JSON.parse(this.responseText)
} catch (error) {
GameLib.Event.Emit(
GameLib.Event.SAVE_COMPONENT_ERROR,
{
message: this.responseText
}
)
} }
if (response.result === 'success') { var xhr = new XMLHttpRequest();
GameLib.Event.Emit(
GameLib.Event.COMPONENT_SAVED, xhr.open(
{ 'POST',
message: response.message || 'Successfully saved the component' urlData.apiUrl + '/component/create'
);
xhr.setRequestHeader("Accept", "application/json");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("x-authorization", urlData.passwoid);
xhr.onreadystatechange = function () {
if (this.readyState === 4) {
try {
var response = JSON.parse(this.responseText)
} catch (error) {
GameLib.Event.Emit(
GameLib.Event.SAVE_COMPONENT_ERROR,
{
message: this.responseText
}
)
} }
)
} else { if (response.result === 'success') {
GameLib.Event.Emit( GameLib.Event.Emit(
GameLib.Event.SAVE_COMPONENT_ERROR, GameLib.Event.COMPONENT_SAVED,
{ {
message: response.message || 'The server responded but failed to save the component' message: response.message || 'Successfully saved the component'
}
)
} else {
GameLib.Event.Emit(
GameLib.Event.SAVE_COMPONENT_ERROR,
{
message: response.message || 'The server responded but failed to save the component'
}
)
} }
) }
} };
xhr.send(JSON.stringify({
component : data.apiObject,
session : this.token
}));
} }
}; );
xhr.send(JSON.stringify({
component : data.apiObject,
session : this.token
}));
}; };