Chore: Upgrade to Angular v18

Chore: Upgrade to Python 3.12
Chore: Upgrade to psycopg3
This commit is contained in:
2024-06-03 13:22:56 +05:30
parent 56c1be5e05
commit 010e9a84db
573 changed files with 5727 additions and 6528 deletions

View File

@ -0,0 +1,16 @@
import { HttpInterceptorFn } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { authInterceptor } from './auth.interceptor';
describe('authInterceptor', () => {
const interceptor: HttpInterceptorFn = (req, next) => TestBed.runInInjectionContext(() => authInterceptor(req, next));
beforeEach(() => {
TestBed.configureTestingModule({});
});
it('should be created', () => {
expect(interceptor).toBeTruthy();
});
});

View File

@ -0,0 +1,53 @@
import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AuthService } from '../auth/auth.service';
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
import { ToasterService } from './toaster.service';
export const authInterceptor: HttpInterceptorFn = (req, next) => {
return next(req).pipe(
catchError((err) => {
// We don't want to refresh token for some requests like login or refresh token itself
// So we verify url and we throw an error if it's the case
if (req.url.includes('/refresh') || req.url.includes('/token')) {
// We do another check to see if refresh token failed
// In this case we want to logout user and to redirect it to login page
if (req.url.includes('/refresh')) {
inject(AuthService).logout();
}
return throwError(() => new Error(err));
}
// If error status is different than 401 we want to skip refresh token
// So we check that and throw the error if it's the case
if (err.status !== 401) {
const error = err.error.message || err.error.detail || err.statusText;
return throwError(() => new Error(error));
}
// auto logout if 401 response returned from api
inject(AuthService).logout();
inject(ToasterService).show('Danger', 'User has been logged out');
const dialogRef = inject(MatDialog).open(ConfirmDialogComponent, {
width: '250px',
data: {
title: 'Logged out!',
content:
'You have been logged out.\n' +
'You can press Cancel to stay on page and login in another tab to resume here, ' +
'or you can press Ok to navigate to the login page.',
},
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
inject(Router).navigate(['login']);
}
});
return throwError(() => new Error(err));
}),
);
};

View File

@ -1,21 +0,0 @@
import { CommonModule } from '@angular/common';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule } from '@angular/material/menu';
import { MatToolbarModule } from '@angular/material/toolbar';
import { RouterModule } from '@angular/router';
import { ErrorInterceptor } from './http-auth-interceptor';
import { JwtInterceptor } from './jwt.interceptor';
@NgModule({
imports: [CommonModule, MatButtonModule, MatIconModule, MatMenuModule, MatToolbarModule, RouterModule],
declarations: [],
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: ErrorInterceptor, multi: true },
],
})
export class CoreModule {}

View File

@ -0,0 +1,15 @@
import { inject, TestBed } from '@angular/core/testing';
import { ErrorLoggerService } from './error-logger.service';
describe('ErrorLoggerService', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [ErrorLoggerService],
});
});
it('should be created', inject([ErrorLoggerService], (service: ErrorLoggerService) => {
expect(service).toBeTruthy();
}));
});

View File

@ -1,13 +1,11 @@
import { Injectable } from '@angular/core';
import { throwError } from 'rxjs';
import { Observable } from 'rxjs';
import { Observable, throwError } from 'rxjs';
@Injectable({
providedIn: 'root',
})
export class ErrorLoggerService {
public static log(serviceName = 'error-logger', message: string) {
// eslint-disable-next-line no-console
console.log(`${serviceName}Service: ${message}`);
}
@ -18,11 +16,10 @@ export class ErrorLoggerService {
* @param operation - name of the operation that failed
* @param result - optional value to return as the observable result
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this
public handleError<T>(serviceName = 'error-logger', operation = 'operation', result?: T) {
public handleError<T>(serviceName = 'error-logger', operation = 'operation') {
return (error: unknown): Observable<T> => {
ErrorLoggerService.log(serviceName, `${operation} failed: ${error}`);
return throwError(error);
return throwError(() => error);
};
}
}

View File

@ -1,63 +0,0 @@
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AuthService } from '../auth/auth.service';
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
import { ToasterService } from './toaster.service';
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(
private authService: AuthService,
private router: Router,
private dialog: MatDialog,
private toaster: ToasterService,
) {}
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
return next.handle(request).pipe(
catchError((err) => {
// We don't want to refresh token for some requests like login or refresh token itself
// So we verify url and we throw an error if it's the case
if (request.url.includes('/refresh') || request.url.includes('/token')) {
// We do another check to see if refresh token failed
// In this case we want to logout user and to redirect it to login page
if (request.url.includes('/refresh')) {
this.authService.logout();
}
return throwError(() => err);
}
// If error status is different than 401 we want to skip refresh token
// So we check that and throw the error if it's the case
if (err.status !== 401) {
const error = err.error.message ?? err.error.detail ?? err.statusText;
return throwError(() => error);
}
// auto logout if 401 response returned from api
this.authService.logout();
this.toaster.show('Error', 'User has been logged out');
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {
title: 'Logged out!',
content:
'You have been logged out.\n' +
'You can press Cancel to stay on page and login in another tab to resume here,' +
' or you can press Ok to navigate to the login page.',
},
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.router.navigate(['login']);
}
});
return throwError(() => err);
}),
);
}
}

View File

@ -0,0 +1,16 @@
import { HttpInterceptorFn } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { jwtInterceptor } from './jwt.interceptor';
describe('jwtInterceptor', () => {
const interceptor: HttpInterceptorFn = (req, next) => TestBed.runInInjectionContext(() => jwtInterceptor(req, next));
beforeEach(() => {
TestBed.configureTestingModule({});
});
it('should be created', () => {
expect(interceptor).toBeTruthy();
});
});

View File

@ -1,35 +1,18 @@
import { HttpRequest, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { AuthService } from '../auth/auth.service';
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
private isRefreshing = false;
constructor(private authService: AuthService) {}
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
// add authorization header with jwt token if available
// We use this line to debug token refreshing
if (!this.isRefreshing && this.authService.user && this.authService.needsRefreshing()) {
this.isRefreshing = true;
this.authService.refreshToken().subscribe(() => {
this.isRefreshing = false;
});
}
const currentUser = this.authService.user;
if (currentUser?.access_token) {
// eslint-disable-next-line no-param-reassign
request = request.clone({
setHeaders: {
Authorization: `Bearer ${currentUser.access_token}`,
},
});
}
return next.handle(request);
export const jwtInterceptor: HttpInterceptorFn = (req, next) => {
// add authorization header with jwt token if available
const currentUser = inject(AuthService).user;
if (currentUser?.access_token) {
req = req.clone({
setHeaders: {
Authorization: `Bearer ${currentUser.access_token}`,
},
});
}
}
return next(req);
};

View File

@ -0,0 +1,17 @@
import { HttpInterceptorFn } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { refreshInterceptor } from './refresh.interceptor';
describe('refreshInterceptor', () => {
const interceptor: HttpInterceptorFn = (req, next) =>
TestBed.runInInjectionContext(() => refreshInterceptor(req, next));
beforeEach(() => {
TestBed.configureTestingModule({});
});
it('should be created', () => {
expect(interceptor).toBeTruthy();
});
});

View File

@ -0,0 +1,17 @@
import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { AuthService } from '../auth/auth.service';
export const refreshInterceptor: HttpInterceptorFn = (req, next) => {
const authService = inject(AuthService);
if (!authService.isRefreshing && authService.user && authService.needsRefreshing()) {
authService.isRefreshing = true;
authService.refreshToken().subscribe({
next: () => {
authService.isRefreshing = false;
},
});
}
return next(req);
};