diff --git a/src/app/app.component.ts b/src/app/app.component.ts index d6b0e3f..d343468 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -23,7 +23,7 @@ export class AppComponent { activePattern: Pattern; newCommandForInput: string; resetCommand$: Observable = this.redisConnectService.execCommandTime$; - isAuth$: Observable = this.githubDataService.isAuth; + isAuth$: Observable = this.githubDataService.isAuth$; constructor( private githubDataService: GithubDataService, diff --git a/src/app/core/core.module.ts b/src/app/core/core.module.ts index dbd5bcd..1f773b9 100644 --- a/src/app/core/core.module.ts +++ b/src/app/core/core.module.ts @@ -4,6 +4,7 @@ import { CommonModule } from '@angular/common'; import { ConfigService } from './services/config.service'; import { CacheInterceptor } from './interceptors/cache-interceptor'; +import { AuthInterceptor } from './interceptors/auth.interceptor'; import { HeaderComponent } from '@app/core/components/header/header.component'; @NgModule({ @@ -30,6 +31,11 @@ import { HeaderComponent } from '@app/core/components/header/header.component'; provide: HTTP_INTERCEPTORS, useClass: CacheInterceptor, multi: true + }, + { + provide: HTTP_INTERCEPTORS, + useClass: AuthInterceptor, + multi: true } ], }) diff --git a/src/app/core/interceptors/auth.interceptor.ts b/src/app/core/interceptors/auth.interceptor.ts new file mode 100644 index 0000000..08923bb --- /dev/null +++ b/src/app/core/interceptors/auth.interceptor.ts @@ -0,0 +1,32 @@ +import { Injectable } from '@angular/core'; +import { + HttpEvent, + HttpHandler, + HttpInterceptor, + HttpRequest +} from '@angular/common/http'; +import { Observable } from 'rxjs'; +import { GithubDataService } from '@app/core/services/github-data.service'; +​ +@Injectable({ + providedIn: 'root' +}) +export class AuthInterceptor implements HttpInterceptor { + public constructor(private githubDataService: GithubDataService) { } +​ + public intercept(request: HttpRequest, next: HttpHandler ): Observable> { + if (this.isAuthenticated()) { + request = this.createAuthenticatedRequest(request); + } +​ + return next.handle(request); + } + + private createAuthenticatedRequest(request: HttpRequest) { + return request.clone({ setHeaders: { Authorization: `token ${this.githubDataService.accessToken}` } }); + } + + private isAuthenticated(): boolean { + return this.githubDataService.isAuth; + } +} \ No newline at end of file diff --git a/src/app/core/interceptors/cache-interceptor.ts b/src/app/core/interceptors/cache-interceptor.ts index a21073d..cbe0bfe 100644 --- a/src/app/core/interceptors/cache-interceptor.ts +++ b/src/app/core/interceptors/cache-interceptor.ts @@ -11,34 +11,43 @@ import { tap } from 'rxjs/operators'; ​ import { environment } from '@app/../environments/environment'; import { CachingService } from '@app/core/services/caching.service'; -import { GithubDataService } from '@app/core/services/github-data.service'; ​ @Injectable({ providedIn: 'root' }) export class CacheInterceptor implements HttpInterceptor { ​ - constructor(private cachingService: CachingService, private githubDataService: GithubDataService) { } + public constructor(private readonly cachingService: CachingService) { } ​ - intercept(request: HttpRequest, next: HttpHandler ): Observable> { - if (this.githubDataService.accessToken && this.githubDataService.accessToken.length) { - request = request.clone({ setHeaders: { Authorization: `token ${this.githubDataService.accessToken}` } }); - } -​ - if (!request.headers.has(environment.cacheableHeaderKey)) { + public intercept(request: HttpRequest, next: HttpHandler): Observable> { + if (!this.isCacheableRequest(request)) { return next.handle(request); } - /** remove cacheable headers form original request */ - request = request.clone({ headers: request.headers.delete(environment.cacheableHeaderKey) }); - const cachedResponse = this.cachingService.get(request.url); - return cachedResponse ? of(cachedResponse) : this.sendRequest(request, next); + + const cachedResponse = this.getCachedResponse(request); + if (cachedResponse) { + return of(cachedResponse); + } + + return this.nextAndCache(request, next); + } + + private getCachedResponse(request: HttpRequest): HttpResponse { + return this.cachingService.get(request.url); + } + + private isCacheableRequest(request: HttpRequest): boolean { + return request.headers.has(environment.cacheableHeaderKey); } ​ - sendRequest(req: HttpRequest, next: HttpHandler): Observable> { - return next.handle(req).pipe( + private nextAndCache(request: HttpRequest, next: HttpHandler): Observable> { + /* remove cacheable headers form original request */ + request = request.clone({ headers: request.headers.delete(environment.cacheableHeaderKey) }); + + return next.handle(request).pipe( tap((event) => { if (event instanceof HttpResponse) { - this.cachingService.set(req.url, event); + this.cachingService.set(request.url, event); } }) ); diff --git a/src/app/core/services/github-data.service.ts b/src/app/core/services/github-data.service.ts index 7f6f21a..bd14912 100644 --- a/src/app/core/services/github-data.service.ts +++ b/src/app/core/services/github-data.service.ts @@ -101,16 +101,20 @@ export class GithubDataService { const isToken = (res.data.access_token) ? true : false; this.authorized.next(isToken); this.accessToken = res.data && res.data.access_token; - return this.isAuth; + return this.isAuth$; }), catchError(() => { this.authorized.next(false); - return this.isAuth; + return this.isAuth$; }) ); } - get isAuth() { + get isAuth$() { return this.authorized.asObservable(); } + + get isAuth(): boolean { + return this.authorized.value; + } }