58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
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);
|
|
}
|
|
}
|
|
}
|