Skip to content
Snippets Groups Projects

Internal Applications

1 file
+ 49
0
Compare changes
  • Side-by-side
  • Inline
 
import {HttpClient} from '@angular/common/http';
 
import {Injectable} from '@angular/core';
 
import {NshmpService, SpinnerService} from '@ghsc/nshmp-lib-ng/nshmp';
 
import {environment} from 'projects/nshmp-apps/src/environments/environment';
 
import {catchError, map} from 'rxjs';
 
 
interface AuthResponse {
 
isAuthorized: boolean;
 
}
 
 
/**
 
* Authorizer for USGS network.
 
*/
 
@Injectable({
 
providedIn: 'root',
 
})
 
export class AuthService {
 
private service = environment.webServices.aws;
 
private url = `${this.service.url}${this.service.services.auth}`;
 
 
constructor(
 
private http: HttpClient,
 
private spinnerService: SpinnerService,
 
private nshmpService: NshmpService
 
) {}
 
 
isAuthorized() {
 
const ref = this.spinnerService.show('Checking USGS network ...');
 
 
return this.http.get<AuthResponse>(this.url).pipe(
 
map(response => {
 
console.log(response);
 
ref.close();
 
 
if (!response.isAuthorized) {
 
this.nshmpService.throwError$(
 
new Error('Must be on USGS network to access internal applications')
 
);
 
}
 
 
return response.isAuthorized;
 
}),
 
catchError((error: Error) => {
 
ref.close();
 
return this.nshmpService.throwError$(error);
 
})
 
);
 
}
 
}
Loading