Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added authorization for import #4

Open
wants to merge 1 commit into
base: task-5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/app/admin/manage-products/manage-products.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class ManageProductsService extends ApiService {
uploadProductsCSV(file: File): Observable<unknown> {
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;
}
Expand All @@ -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<string> {
const url = this.getUrl('import', 'import');

return this.http.get<string>(url, {
params: {
name: fileName,
},
headers: this.auth,
});
}
}
13 changes: 12 additions & 1 deletion src/app/core/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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];
}
Expand Down
20 changes: 15 additions & 5 deletions src/app/core/interceptors/error-print.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@angular/core';
import {
HttpErrorResponse,
HttpEvent,
HttpHandler,
HttpInterceptor,
Expand All @@ -15,19 +16,28 @@ export class ErrorPrintInterceptor implements HttpInterceptor {

intercept(
request: HttpRequest<unknown>,
next: HttpHandler
next: HttpHandler,
): Observable<HttpEvent<unknown>> {
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,
);
},
})
}),
);
}
}
4 changes: 2 additions & 2 deletions src/environments/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down