Skip to content
This repository was archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Add basic auth header to native http
Browse files Browse the repository at this point in the history
  • Loading branch information
pheinrichs committed Jun 20, 2018
1 parent 27d3f67 commit da598f8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
17 changes: 6 additions & 11 deletions src/pages/login/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,13 @@ export class Login {
}

login(item) {
this.api.authenticate(item.url, item.token).subscribe((data) => {
this.menu.enable(true);
this.storage.set('_session', { 'url': item.url, 'token': item.token, 'version': '/api/v0' });
this.navCtrl.setRoot('Dashboard');
this.api.authenticate(item.url, item.token, item.basic).subscribe((data) => {
this.menu.enable(true);
this.storage.set('_session', { 'url': item.url, 'token': item.token, 'version': '/api/v0', 'basic': item.basic });
this.navCtrl.setRoot('Dashboard');
}, (error) => {
console.log(JSON.stringify(error));
if (error.status == 0) {
this.presentToast('Unable to reach the server.');
}
else {
this.presentToast((error.status ? error.status : '') + ':' + (error.statusText ? error.statusText : '') + "\n" + error.json().message);
}
error = JSON.parse(error);
this.presentToast((error.status ? error.status : '') + ':' + (error.message ? error.message : ''));
});
}

Expand Down
14 changes: 11 additions & 3 deletions src/providers/libre-nms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,18 @@ export class LibreNMS {
* @param token API Token
* @return Observable Object
*/
authenticate(url: string, token: string): Observable<any> {
authenticate(url: string, token: string, basic: any = {enabled: false}): Observable<any> {
return Observable.create((observer) => {
this.platform.ready().then(() => {
this.http.get(`${url}${this.api_version}`, { headers: this.headers(token) }).subscribe((response) => {
observer.next({ 'success': true });
if (basic.enabled) this.nativeHttp.useBasicAuth(basic.username, basic.password);

this.http.get(`${url}${this.api_version}`, { headers: this.headers(token) }).subscribe((response: any) => {
if (response.status == "error") {
observer.error(JSON.stringify(response));
}
else {
observer.next({ 'success': true });
}
}, (error) => {
observer.error(JSON.stringify(error));
}, () => {
Expand All @@ -47,6 +54,7 @@ export class LibreNMS {
*/
logout(): boolean {
this.storage.set('_session', { 'url': null, 'token': null });
this.nativeHttp.useBasicAuth('','');
return true;
}

Expand Down

0 comments on commit da598f8

Please sign in to comment.