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

#85 create auth interceptor #88

Merged
merged 11 commits into from
Mar 3, 2021
11 changes: 4 additions & 7 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import { FooterComponent } from './footer/footer.component';
import { MaterialModule } from './shared/material/material.module';
import { RegistrationModule } from './shared/modals/registration/registration.module';
import { UserRegistrationState } from './shared/store/user-registration.state';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpErrorInterceptor } from './shared/error-interceptors/http-error.interceptor';

import { MatSnackBarModule } from '@angular/material/snack-bar';
import { UserState } from './shared/store/user.state';
import { InterceptorProviders } from './shared/interceptors/interceptorProviders';



@NgModule({
Expand Down Expand Up @@ -69,11 +70,7 @@ import { UserState } from './shared/store/user.state';
MatSnackBarModule
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: HttpErrorInterceptor,
multi: true
}
InterceptorProviders,
],
bootstrap: [AppComponent]
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Store } from '@ngxs/store';
import { Observable, throwError } from 'rxjs';
Expand Down
39 changes: 39 additions & 0 deletions src/app/shared/interceptors/http-token.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { OidcSecurityService } from 'angular-auth-oidc-client';
import { Select, Store } from '@ngxs/store';
import { UserRegistrationState } from '../store/user-registration.state';

@Injectable()
export class HttpTokenInterceptor implements HttpInterceptor {
constructor(
public store: Store,
private oidcSecurityService: OidcSecurityService,
private http: HttpClient) {}

@Select(UserRegistrationState.role)
role$: Observable<string>;
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let token = this.oidcSecurityService.getToken();

let tokenTitle= (token) ? `Bearer ${token}`: null;

if (typeof(token) === 'string') {
return next.handle(request.clone({
setHeaders: { Authorization: tokenTitle }
}))
.pipe(catchError((error) => {
return throwError(error);
}));
}
return next.handle(request).pipe(catchError((error) => {
return throwError(error);
}));
}
}
10 changes: 10 additions & 0 deletions src/app/shared/interceptors/interceptorProviders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {HTTP_INTERCEPTORS} from '@angular/common/http';

import { HttpTokenInterceptor } from './http-token.interceptor';
import { HttpErrorInterceptor } from './http-error.interceptor';

export const InterceptorProviders =
[
{ provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true },
{ provide: HTTP_INTERCEPTORS, useClass: HttpTokenInterceptor, multi: true },
];
19 changes: 17 additions & 2 deletions src/app/shell/parent/parent.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,32 @@ import { ChildActivitiesComponent } from './parent-activities/child-activities/c
import { FlexLayoutModule } from '@angular/flex-layout';
import { MatButtonModule } from '@angular/material/button';

import { MatButtonToggleModule } from '@angular/material/button-toggle';
import { MatCardModule } from '@angular/material/card';
import { MatChipsModule } from '@angular/material/chips';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from "@angular/material/form-field";
import { MatRadioModule } from '@angular/material/radio';


@NgModule({
declarations: [
ParentActivitiesComponent,
ParentConfigComponent,
ParentConfigComponent,
ChildActivitiesComponent
],

imports: [
CommonModule,
ParentRoutingModule,
MatButtonToggleModule,
MatCardModule,
MatButtonModule,
MatChipsModule,
MatFormFieldModule,
MatInputModule,
FlexLayoutModule,
MatButtonModule
MatRadioModule
]
})
export class ParentModule { }