Extracted frontend from webservice to new directory
Updated directory structure Updated .gitignore
This commit is contained in:
30
frontend/src/app/Helpers/auth.interceptor.ts
Normal file
30
frontend/src/app/Helpers/auth.interceptor.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { HTTP_INTERCEPTORS, HttpEvent } from '@angular/common/http';
|
||||
import { Injectable } from '@angular/core';
|
||||
import {
|
||||
HttpInterceptor,
|
||||
HttpHandler,
|
||||
HttpRequest,
|
||||
} from '@angular/common/http';
|
||||
import { TokenStorageService } from '../Services/token.service';
|
||||
import { Observable } from 'rxjs';
|
||||
const TOKEN_HEADER_KEY = 'Authorization'; // for Spring Boot back-end
|
||||
@Injectable()
|
||||
export class AuthInterceptor implements HttpInterceptor {
|
||||
constructor(private token: TokenStorageService) {}
|
||||
intercept(
|
||||
req: HttpRequest<any>,
|
||||
next: HttpHandler
|
||||
): Observable<HttpEvent<any>> {
|
||||
let authReq = req;
|
||||
const token = this.token.getToken();
|
||||
if (token != null) {
|
||||
authReq = req.clone({
|
||||
headers: req.headers.set(TOKEN_HEADER_KEY, 'Bearer ' + token),
|
||||
});
|
||||
}
|
||||
return next.handle(authReq);
|
||||
}
|
||||
}
|
||||
export const authInterceptorProviders = [
|
||||
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true },
|
||||
];
|
||||
Reference in New Issue
Block a user