Mess commit.
This commit is contained in:
23
frontend/src/app/core/interceptor/jwt.interceptor.ts
Normal file
23
frontend/src/app/core/interceptor/jwt.interceptor.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { Observable } from 'rxjs';
|
||||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
|
||||
import { AuthenticationService } from '../service/authentication.service';
|
||||
|
||||
@Injectable()
|
||||
export class JwtInterceptor implements HttpInterceptor {
|
||||
private readonly authenticationService = inject(AuthenticationService);
|
||||
|
||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||
const jwt = this.authenticationService.getToken();
|
||||
|
||||
if (jwt) {
|
||||
const cloned = request.clone({
|
||||
headers: request.headers.set('Authorization', `Bearer ${jwt}`)
|
||||
});
|
||||
|
||||
return next.handle(cloned);
|
||||
}
|
||||
|
||||
return next.handle(request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface Picture {
|
||||
id: string,
|
||||
publishedAt: Date
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { HttpClient, HttpParams } from "@angular/common/http";
|
||||
import { inject, Injectable } from "@angular/core";
|
||||
import { Picture } from "./model/picture";
|
||||
import { lastValueFrom } from "rxjs";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class PictureRestService {
|
||||
private readonly httpClient = inject(HttpClient);
|
||||
|
||||
getAllOfCurrentUser(): Promise<Picture[]> {
|
||||
return lastValueFrom(this.httpClient.get<Picture[]>('/api/pictures/current-user'));
|
||||
}
|
||||
|
||||
uploadPicture(pictureFile: File): Promise<string> {
|
||||
const formData = new FormData();
|
||||
formData.append("file", pictureFile);
|
||||
return lastValueFrom(this.httpClient.post<string>('/api/pictures', formData));
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,14 @@ export class AuthenticationService {
|
||||
return result;
|
||||
}
|
||||
|
||||
getAuthenticatedUser(): User | undefined {
|
||||
return this.extractUserFromLocalStorage();
|
||||
}
|
||||
|
||||
getToken(): string | undefined {
|
||||
return localStorage.getItem(JWT_PARAM) ?? undefined;
|
||||
}
|
||||
|
||||
private extractUserFromLocalStorage(): User | undefined {
|
||||
let result: User | undefined = undefined;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user