Just import BasicAuthModule
of the module @helgoland/auth
in your application. This will provide an angular service to register additional service urls for the basic auth. It also registers a http interceptor to decide if a basic auth token is needed for the request.
Extend the configuration in your settings datasetApis
with a basicAuth
flag, for example in this way:
"datasetApis": [
{
"name": "BasicAuthSecuredAPI",
"url": "https://url-to/api/v1/",
"basicAuth": true
}
]
Use an injected service BasicAuthServiceMaintainer
and register the url in this way:
this.basicAuthServiceMaintainer.registerService('https://url-to/api/v1/');
Implement the BasicAuthInformer
with the method doBasicAuth
. Here you can provide the User a login mask (maybe a modal window), do the auth process. And fulfill the Observable in any case, if authencation was successful or not. See a simple example here:
import { Injectable } from '@angular/core';
import { BasicAuthService } from '@helgoland/auth';
import { Observable, Observer } from 'rxjs';
import { BasicAuthInformer } from '@helgoland/auth';
@Injectable()
export class BasicAuthInformerImplService implements BasicAuthInformer {
constructor(
private basicAuthSrvc: BasicAuthService
) { }
public doBasicAuth(url: string): Observable<boolean> {
return new Observable<boolean>((observer: Observer<boolean>) => {
const username = prompt('Basic Auth username for ' + url);
const password = prompt('Basic Auth password for ' + url);
this.basicAuthSrvc.auth(username, password, url).subscribe(
token => {
observer.next(true);
observer.complete();
},
error => {
observer.next(false);
observer.complete();
}
);
});
}
}