Skip to content

Commit

Permalink
fix: add auth headers to scorm adapter xhr requests
Browse files Browse the repository at this point in the history
  • Loading branch information
satikaj committed Jun 5, 2024
1 parent d904ffd commit 97e1ea1
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/app/api/services/scorm-adapter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class ScormAdapterService {

if (this.context.mode === 'review') {
this.xhr.open('GET', `${API_URL}/test_attempts/${this.context.attemptId}/review`, false);
this.xhr.setRequestHeader('Auth-Token', this.context.user.authenticationToken);
this.xhr.setRequestHeader('Username', this.context.user.username);

this.xhr.onload = () => {
if (this.xhr.status >= 200 && this.xhr.status < 400) {
Expand Down Expand Up @@ -88,6 +90,8 @@ export class ScormAdapterService {
`${API_URL}/projects/${this.context.projectId}/task_def_id/${this.context.taskDefId}/test_attempts/latest`,
false,
);
this.xhr.setRequestHeader('Auth-Token', this.context.user.authenticationToken);
this.xhr.setRequestHeader('Username', this.context.user.username);

let noTestFound = false;
let startNewTest = false;
Expand Down Expand Up @@ -119,6 +123,8 @@ export class ScormAdapterService {

if (!startNewTest) {
this.xhr.open('PATCH', `${API_URL}/test_attempts/${this.context.attemptId}`, false);
this.xhr.setRequestHeader('Auth-Token', this.context.user.authenticationToken);
this.xhr.setRequestHeader('Username', this.context.user.username);
this.xhr.send();
console.log(this.xhr.responseText);

Expand All @@ -133,6 +139,8 @@ export class ScormAdapterService {
`${API_URL}/projects/${this.context.projectId}/task_def_id/${this.context.taskDefId}/test_attempts`,
false,
);
this.xhr.setRequestHeader('Auth-Token', this.context.user.authenticationToken);
this.xhr.setRequestHeader('Username', this.context.user.username);
this.xhr.send();
console.log(this.xhr.responseText);

Expand Down Expand Up @@ -163,6 +171,8 @@ export class ScormAdapterService {
}

this.xhr.open('PATCH', `${API_URL}/test_attempts/${this.context.attemptId}`, false);
this.xhr.setRequestHeader('Auth-Token', this.context.user.authenticationToken);
this.xhr.setRequestHeader('Username', this.context.user.username);
this.xhr.setRequestHeader('Content-Type', 'application/json');
const requestData = {
cmi_datamodel: JSON.stringify(this.dataModel.dump()),
Expand Down Expand Up @@ -233,26 +243,28 @@ export class ScormAdapterService {
break;
}

const xhr = new XMLHttpRequest();
xhr.open('PATCH', `${API_URL}/test_attempts/${this.context.attemptId}`, true);
xhr.setRequestHeader('Content-Type', 'application/json');
this.xhr.open('PATCH', `${API_URL}/test_attempts/${this.context.attemptId}`, true);
this.xhr.setRequestHeader('Auth-Token', this.context.user.authenticationToken);
this.xhr.setRequestHeader('Username', this.context.user.username);
this.xhr.setRequestHeader('Content-Type', 'application/json');
const requestData = {
cmi_datamodel: JSON.stringify(this.dataModel.dump()),
};
xhr.send(JSON.stringify(requestData));


xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 400) {
this.xhr.onload = () => {
if (this.xhr.status >= 200 && this.xhr.status < 400) {
console.log('DataModel saved successfully.');
} else {
console.error('Error saving DataModel:', xhr.responseText);
console.error('Error saving DataModel:', this.xhr.responseText);
}
};

xhr.onerror = () => {
this.xhr.onerror = () => {
console.error('Request failed.');
};

this.xhr.send(JSON.stringify(requestData));
this.context.errorCode = 0;
return 'true';
}
Expand Down

0 comments on commit 97e1ea1

Please sign in to comment.