Refactor AJAX to use fetch API.

This commit is contained in:
Spencer Ofwiti 2021-04-17 13:31:07 +03:00
parent 94da4baceb
commit 2116a55549

View File

@ -13,7 +13,6 @@ import {Observable } from 'rxjs';
})
export class AuthService {
sessionToken: any;
sessionLoginCount = 0;
privateKey: any;
mutableKeyStore: MutableKeyStore = new MutablePgpKeyStore();
@ -36,55 +35,51 @@ export class AuthService {
}
getWithToken(): void {
const xhr = new XMLHttpRequest();
xhr.responseType = 'text';
xhr.open('GET', environment.cicMetaUrl + window.location.search.substring(1));
xhr.setRequestHeader('Authorization', 'Bearer ' + this.sessionToken);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('x-cic-automerge', 'none');
xhr.addEventListener('load', (e) => {
if (xhr.status === 401) {
throw new Error('login rejected');
const headers = {
Authorization: 'Bearer ' + this.sessionToken,
'Content-Type': 'application/json;charset=utf-8',
'x-cic-automerge': 'none'
};
const options = {
headers,
};
fetch(environment.cicMetaUrl, options).then(response => {
if (response.status === 401) {
return Promise.reject({ statusText: response.statusText });
}
this.sessionLoginCount++;
this.setState('Click button to log in');
return;
});
xhr.send();
}).catch(error => this.loggingService.sendErrorLevelMessage('Login rejected', this, {error}));
}
sendResponse(hobaResponseEncoded): void {
const xhr = new XMLHttpRequest();
xhr.responseType = 'text';
xhr.open('GET', environment.cicMetaUrl + window.location.search.substring(1));
xhr.setRequestHeader('Authorization', 'HOBA ' + hobaResponseEncoded);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('x-cic-automerge', 'none');
xhr.addEventListener('load', (e) => {
if (xhr.status === 401) {
throw new Error('login rejected');
const headers = {
Authorization: 'HOBA ' + hobaResponseEncoded,
'Content-Type': 'application/json;charset=utf-8',
'x-cic-automerge': 'none'
};
const options = {
headers,
};
fetch(environment.cicMetaUrl, options).then(response => {
if (response.status === 401) {
return Promise.reject({ statusText: response.statusText });
}
this.sessionToken = xhr.getResponseHeader('Token');
this.sessionToken = response.headers.get('Token');
sessionStorage.setItem(btoa('CICADA_SESSION_TOKEN'), this.sessionToken);
this.sessionLoginCount++;
this.setState('Click button to log in');
return;
});
xhr.send();
}).catch(error => this.loggingService.sendErrorLevelMessage('Login rejected', this, {error}));
}
getChallenge(password: string): void {
const xhr = new XMLHttpRequest();
xhr.responseType = 'arraybuffer';
xhr.open('GET', environment.cicMetaUrl + window.location.search.substring(1));
xhr.onload = async (e) => {
if (xhr.status === 401) {
const authHeader = xhr.getResponseHeader('WWW-Authenticate');
fetch(environment.cicMetaUrl).then(async response => {
console.log(response);
if (response.status === 401) {
const authHeader = response.headers.get('WWW-Authenticate');
const o = hobaParseChallengeHeader(authHeader);
await this.loginResponse(o, password);
}
};
xhr.send();
}).catch(error => this.loggingService.sendErrorLevelMessage('Fetching challenge failed', this, {error}));
}
passwordLogin(password: string): boolean {