Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zenkiet committed Nov 6, 2023
1 parent f4af98f commit c526f39
Show file tree
Hide file tree
Showing 51 changed files with 1,345 additions and 1,066 deletions.
2 changes: 1 addition & 1 deletion client/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
]
}
]
}
}
5 changes: 3 additions & 2 deletions client/angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"packageManager": "pnpm",
"analytics": "a93cc6e0-d443-4a09-94c9-d96f0003f569",
"schematicCollections": [
"@angular-eslint/schematics"
"@angular-eslint/schematics",
"@ngrx/schematics"
]
},
"newProjectRoot": "projects",
Expand Down Expand Up @@ -150,4 +151,4 @@
}
}
}
}
}
4 changes: 4 additions & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
"@angular/platform-browser-dynamic": "16.2.7",
"@angular/router": "16.2.7",
"@ngneat/transloco": "4.2.7",
"@ngrx/component": "^16.3.0",
"@ngrx/effects": "16.3.0",
"@ngrx/entity": "16.3.0",
"@ngrx/store": "^16.3.0",
"@ngrx/store-devtools": "16.3.0",
"@rive-app/canvas-advanced": "^2.7.0",
"apexcharts": "3.40.0",
"crypto-js": "3.3.0",
"highlight.js": "11.8.0",
Expand All @@ -59,6 +62,7 @@
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@compodoc/compodoc": "1.1.21",
"@ngrx/eslint-plugin": "^16.3.0",
"@ngrx/schematics": "^16.3.0",
"@tailwindcss/typography": "0.5.9",
"@types/chroma-js": "2.4.1",
"@types/crypto-js": "3.1.47",
Expand Down
46 changes: 46 additions & 0 deletions client/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 4 additions & 10 deletions client/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpClientModule } from '@angular/common/http';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { provideAnimations } from '@angular/platform-browser/animations';
import { DateAdapter, MAT_DATE_FORMATS } from '@angular/material/core';
import { LuxonDateAdapter } from '@angular/material-luxon-adapter';
import { provideTransloco } from './core/transloco/transloco.provider';
import { provideAuth } from './core/auth/auth.provider';
import { provideIcons } from './core/icons/icons.provider';
import { provideFuse } from '@fuse';
import { mockApiServices } from './mock-api';
import { ProjectModule } from './modules/admin/dashboards/project/project.module';
import { SettingsModule } from './modules/common/settings/settings.module';
import { TranslocoModule } from '@ngneat/transloco';
import { PlanModule } from './modules/admin/apps/plan/plan.module';
import { StateModule } from './core/state/state.module';
import { CoreModule } from './core/core.module';
import { TranslocoRootModule } from './transloco-root.module';

@NgModule({
declarations: [AppComponent],
imports: [
BrowserModule,
HttpClientModule,
CoreModule,
AppRoutingModule,
ProjectModule,
PlanModule,
SettingsModule,
TranslocoModule,
StateModule,
TranslocoRootModule,
],
providers: [
{
Expand All @@ -51,9 +48,6 @@ import { StateModule } from './core/state/state.module';
},
},

// Transloco Config
provideTransloco(),

// Fuse
provideAuth(),
provideIcons(),
Expand Down
88 changes: 36 additions & 52 deletions client/src/app/core/auth/auth.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,46 @@
import {
HttpErrorResponse,
HttpEvent,
HttpHandlerFn,
HttpHandler,
HttpInterceptor,
HttpRequest,
HttpErrorResponse,
} from '@angular/common/http';
import { inject } from '@angular/core';
import { Injectable } from '@angular/core';
import { throwError, Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { AuthService } from 'app/core/auth/auth.service';
import { AuthUtils } from 'app/core/auth/auth.utils';
import { catchError, Observable, throwError } from 'rxjs';

/**
* Intercept
*
* @param req
* @param next
*/
export const authInterceptor = (
req: HttpRequest<unknown>,
next: HttpHandlerFn
): Observable<HttpEvent<unknown>> => {
const authService = inject(AuthService);

// Clone the request object
let newReq = req.clone();
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private authService: AuthService) {}

// Request
//
// If the access token didn't expire, add the Authorization header.
// We won't add the Authorization header if the access token expired.
// This will force the server to return a "401 Unauthorized" response
// for the protected API routes which our response interceptor will
// catch and delete the access token from the local storage while logging
// the user out from the app.
if (
authService.accessToken &&
!AuthUtils.isTokenExpired(authService.accessToken)
) {
newReq = req.clone({
headers: req.headers.set(
'Authorization',
'Bearer ' + authService.accessToken
),
});
}

// Response
return next(newReq).pipe(
catchError(error => {
// Catch "401 Unauthorized" responses
if (error instanceof HttpErrorResponse && error.status === 401) {
// Sign out
authService.signOut();
intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
let newReq = req;

// Reload the app
location.reload();
}
if (
this.authService.accessToken &&
!AuthUtils.isTokenExpired(this.authService.accessToken)
) {
newReq = req.clone({
headers: req.headers.set(
'Authorization',
'Bearer ' + this.authService.accessToken
),
});
}

return throwError(() => new Error(error));
})
);
};
return next.handle(newReq).pipe(
catchError((error: HttpErrorResponse) => {
if (error.status === 401) {
this.authService.signOut();
location.reload();
}
return throwError(() => new Error(error.message));
})
);
}
}
28 changes: 15 additions & 13 deletions client/src/app/core/auth/auth.provider.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { ENVIRONMENT_INITIALIZER, EnvironmentProviders, inject, Provider } from '@angular/core';
import { authInterceptor } from 'app/core/auth/auth.interceptor';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { Provider } from '@angular/core';
import { AuthInterceptor } from 'app/core/auth/auth.interceptor';
import { AuthService } from 'app/core/auth/auth.service';

export const provideAuth = (): Array<Provider | EnvironmentProviders> =>
{
return [
provideHttpClient(withInterceptors([authInterceptor])),
{
provide : ENVIRONMENT_INITIALIZER,
useValue: () => inject(AuthService),
multi : true,
},
];
export const provideAuth = (): Array<Provider> => {
return [
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
{
provide: AuthService,
useClass: AuthService,
},
];
};
Loading

0 comments on commit c526f39

Please sign in to comment.