Rename and add interceptor; adjust service

This commit is contained in:
Peter Rossa
2023-05-11 13:06:17 +02:00
parent 54905ed995
commit 2ab8331640
6 changed files with 177 additions and 175 deletions

View File

@@ -0,0 +1,57 @@
import {
HttpInterceptor,
HttpRequest,
HttpHandler,
HttpEvent,
HttpHeaders,
} from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { AuthService } from 'src/app/auth/auth.service';
import { LoginService } from 'src/app/auth/login.service';
import { UserService } from 'src/app/services/user.service';
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(
private authService: AuthService,
private loginService: LoginService
) {}
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (
this.authService.checkUserLoggedIn() &&
req.url.indexOf('authenticate') === -1
) {
console.log('if');
let jwtToken: String = this.loginService.getUserSecurityToken();
// headers: HttpHeaders = new HttpHeaders({
// authorization: "bearer " + jwtToken,
// }),
// options = { headers: headers };
const authReq = req.clone({
headers: new HttpHeaders({
'Content-Type': 'application/json',
authorization: 'bearer ' + jwtToken,
// headers: new HttpHeaders({
// 'Content-Type': 'application/json',
// Authorization: `Basic ${window.btoa(
// this.authenticationService.username +
// ':' +
// this.authenticationService.password
// )}`,
}),
});
console.log(authReq);
return next.handle(authReq);
} else {
console.log('else');
return next.handle(req);
}
}
}

View File

@@ -1,49 +0,0 @@
// import {
// HttpInterceptor,
// HttpRequest,
// HttpHandler,
// HttpEvent,
// HttpHeaders,
// } from '@angular/common/http';
// import { Injectable } from '@angular/core';
// import { Observable } from 'rxjs';
// import { AuthenticationService } from 'src/app/services/auth.service';
// @Injectable()
// export class HttpInterceptorService implements HttpInterceptor {
// constructor(private authenticationService: AuthenticationService) {}
// intercept(
// req: HttpRequest<any>,
// next: HttpHandler
// ): Observable<HttpEvent<any>> {
// console.log(
// this.authenticationService.username,
// this.authenticationService.password
// );
// if (
// this.authenticationService.isUserLoggedIn() &&
// req.url.indexOf('basicauth') === -1
// ) {
// console.log('if');
// const authReq = req.clone({
// headers: new HttpHeaders({
// 'Content-Type': 'application/json',
// Authorization: `Basic ${window.btoa('pezi:Password123!')}`,
// // headers: new HttpHeaders({
// // 'Content-Type': 'application/json',
// // Authorization: `Basic ${window.btoa(
// // this.authenticationService.username +
// // ':' +
// // this.authenticationService.password
// // )}`,
// }),
// });
// console.log(authReq);
// return next.handle(authReq);
// } else {
// console.log('else');
// return next.handle(req);
// }
// }
// }