token stuff
This commit is contained in:
@ -7,18 +7,17 @@ import {
|
|||||||
} from '@angular/common/http';
|
} from '@angular/common/http';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
import { MessageService } from './message.service';
|
import { MessageService } from './message.service';
|
||||||
|
import { TokenService } from './token.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthHandlerInterceptor implements HttpInterceptor {
|
export class AuthHandlerInterceptor implements HttpInterceptor {
|
||||||
|
|
||||||
token: String = "abc"
|
constructor(private tokenService: TokenService, private messageService: MessageService) {}
|
||||||
|
|
||||||
constructor(private messageService: MessageService) {}
|
|
||||||
|
|
||||||
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||||
if (request.url.includes('api.hv.nober.de')) {
|
if (request.url.includes('api.hv.nober.de')) {
|
||||||
const clone = request.clone({
|
const clone = request.clone({
|
||||||
setHeaders: { Authorization: `Bearer ${this.token}`}
|
setHeaders: { Authorization: `Bearer ${this.tokenService.getToken()}`}
|
||||||
})
|
})
|
||||||
return next.handle(clone)
|
return next.handle(clone)
|
||||||
}
|
}
|
||||||
|
16
hv2-ui/src/app/token.service.spec.ts
Normal file
16
hv2-ui/src/app/token.service.spec.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { TokenService } from './token.service';
|
||||||
|
|
||||||
|
describe('TokenService', () => {
|
||||||
|
let service: TokenService;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({});
|
||||||
|
service = TestBed.inject(TokenService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be created', () => {
|
||||||
|
expect(service).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
20
hv2-ui/src/app/token.service.ts
Normal file
20
hv2-ui/src/app/token.service.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { MessageService } from './message.service';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root'
|
||||||
|
})
|
||||||
|
export class TokenService {
|
||||||
|
|
||||||
|
private token: String;
|
||||||
|
|
||||||
|
constructor(private messageService: MessageService) {
|
||||||
|
this.messageService.add("TokenService started, token created")
|
||||||
|
this.token = "abc"
|
||||||
|
}
|
||||||
|
|
||||||
|
getToken(): String {
|
||||||
|
this.messageService.add("Providing token to other service")
|
||||||
|
return this.token
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user