From e1d2cf55139ace3f3206dccdc1a9ecf010425d77 Mon Sep 17 00:00:00 2001 From: Katalin Balint Date: Tue, 30 Apr 2024 10:55:02 +0200 Subject: [PATCH] feat: added authorization for import --- .../manage-products.service.ts | 8 ++++---- src/app/core/api.service.ts | 13 +++++++++++- .../interceptors/error-print.interceptor.ts | 20 ++++++++++++++----- src/environments/environment.ts | 4 ++-- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/app/admin/manage-products/manage-products.service.ts b/src/app/admin/manage-products/manage-products.service.ts index 7edab0db..ae0992d4 100644 --- a/src/app/admin/manage-products/manage-products.service.ts +++ b/src/app/admin/manage-products/manage-products.service.ts @@ -12,7 +12,7 @@ export class ManageProductsService extends ApiService { uploadProductsCSV(file: File): Observable { if (!this.endpointEnabled('import')) { console.warn( - 'Endpoint "import" is disabled. To enable change your environment.ts config' + 'Endpoint "import" is disabled. To enable change your environment.ts config', ); return EMPTY; } @@ -24,18 +24,18 @@ export class ManageProductsService extends ApiService { // eslint-disable-next-line @typescript-eslint/naming-convention 'Content-Type': 'text/csv', }, - }) - ) + }), + ), ); } private getPreSignedUrl(fileName: string): Observable { const url = this.getUrl('import', 'import'); - return this.http.get(url, { params: { name: fileName, }, + headers: this.auth, }); } } diff --git a/src/app/core/api.service.ts b/src/app/core/api.service.ts index 76c1c3ae..aa9483c1 100644 --- a/src/app/core/api.service.ts +++ b/src/app/core/api.service.ts @@ -2,7 +2,7 @@ import { Injectable, Injector } from '@angular/core'; import { ApiEndpoint, Config } from '../../environments/config.interface'; import { CONFIG_TOKEN } from './injection-tokens/config.token'; import { Location } from '@angular/common'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; /** Base class for services working with APIs */ @Injectable() @@ -15,6 +15,17 @@ export abstract class ApiService { this.http = injector.get(HttpClient); } + get auth(): HttpHeaders { + const authorization_token = localStorage.getItem('authorization_token'); + const headers = new HttpHeaders({ + 'Content-Type': 'application/json', + ...(authorization_token + ? { Authorization: 'Basic ' + btoa(authorization_token) } + : {}), + }); + return headers; + } + endpointEnabled(api: ApiEndpoint): boolean { return this.config.apiEndpointsEnabled[api]; } diff --git a/src/app/core/interceptors/error-print.interceptor.ts b/src/app/core/interceptors/error-print.interceptor.ts index ac35640b..3058fae1 100644 --- a/src/app/core/interceptors/error-print.interceptor.ts +++ b/src/app/core/interceptors/error-print.interceptor.ts @@ -1,5 +1,6 @@ import { Injectable } from '@angular/core'; import { + HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, @@ -15,19 +16,28 @@ export class ErrorPrintInterceptor implements HttpInterceptor { intercept( request: HttpRequest, - next: HttpHandler + next: HttpHandler, ): Observable> { return next.handle(request).pipe( tap({ - error: () => { + error: (err) => { const url = new URL(request.url); + let cause = 'Check the console for the details'; + if (err instanceof HttpErrorResponse) { + if (err.status === 401) { + cause = 'Authentication failed. Please check your credentials.'; + } else if (err.status === 403) { + cause = + 'Authorization failed. You do not have the necessary permissions.'; + } + } this.notificationService.showError( - `Request to "${url.pathname}" failed. Check the console for the details`, - 0 + `Request to "${url.pathname}" failed. ${cause}`, + 0, ); }, - }) + }), ); } } diff --git a/src/environments/environment.ts b/src/environments/environment.ts index 5831fcd0..2c5ae8e5 100644 --- a/src/environments/environment.ts +++ b/src/environments/environment.ts @@ -7,10 +7,10 @@ import { Config } from './config.interface'; export const environment: Config = { production: false, apiEndpoints: { - product: 'https://ny8ieu19k1.execute-api.eu-central-1.amazonaws.com/dev', + product: 'https://kwgco4rksg.execute-api.eu-central-1.amazonaws.com/dev', order: 'https://.execute-api.eu-west-1.amazonaws.com/dev', import: 'https://0e86mwnks8.execute-api.eu-central-1.amazonaws.com/dev', - bff: 'https://ny8ieu19k1.execute-api.eu-central-1.amazonaws.com/dev', + bff: 'https://kwgco4rksg.execute-api.eu-central-1.amazonaws.com/dev', cart: 'https://.execute-api.eu-west-1.amazonaws.com/dev', }, apiEndpointsEnabled: {