use token to auth at api

This commit is contained in:
2021-06-17 20:07:02 +02:00
parent f6697bec83
commit 677ed3dfd0
6 changed files with 16 additions and 6 deletions

View File

@ -1 +1,2 @@
export const serviceBaseUrl = "https://api.hv.nober.de";
// export const serviceBaseUrl = "http://172.16.10.38:5000";

View File

@ -19,10 +19,9 @@ export class ErrorHandlerInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
return next.handle(request).pipe(
catchError((errorResponse: HttpErrorResponse) => {
this.messageService.add(`Intercepted http error: ${errorResponse}`)
if (errorResponse.status === 401) {
this.router.navigateByUrl('/login')
} else {
this.messageService.add(`Intercepted http error: ${errorResponse}`)
}
return throwError(errorResponse)
})

View File

@ -4,6 +4,7 @@ import { HttpClient, HttpHeaders } from '@angular/common/http';
import { MessageService } from './message.service';
import { serviceBaseUrl } from './config';
import { TestOutput } from './testOutput';
@Injectable({
providedIn: 'root'
@ -11,8 +12,8 @@ import { serviceBaseUrl } from './config';
export class TestOutputService {
constructor(private messageService: MessageService, private http: HttpClient) { }
getTestOutput(): Promise<String> {
getTestOutput(): Promise<TestOutput> {
this.messageService.add(`TestOutputService: fetch test output`);
return this.http.get<String>(`${serviceBaseUrl}/v1/test`).toPromise()
return this.http.get<TestOutput>(`${serviceBaseUrl}/v1/test`).toPromise()
}
}

View File

@ -9,7 +9,11 @@
<div>
<h2>Mein Test</h2>
<pre>{{testOutput}}</pre>
<pre>
{{testOutput.message}}
{{testOutput.details}}
</pre>
</div>

View File

@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { MessageService } from '../message.service';
import { TestOutputService } from '../test-output.service';
import { TestOutput } from '../testOutput';
@Component({
selector: 'app-test-output',
@ -11,7 +12,7 @@ export class TestOutputComponent implements OnInit {
constructor(private testOutputService: TestOutputService, private messageService: MessageService) { }
testOutput : String
testOutput : TestOutput = { "message": "abc", "details": "123" }
async getTestOutput() {
try {

View File

@ -0,0 +1,4 @@
export interface TestOutput {
message: string
details: string
}