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