From d82461834f1dac03a8d6288fefc31ce976b7b090 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sat, 1 Aug 2020 16:40:27 +0000 Subject: [PATCH] Fix excessive authentication prompts Signed-off-by: William Brawner --- src/app/app.component.ts | 9 ++++++++- src/app/budgets/budget.component.ts | 4 ++++ src/app/shared/auth.interceptor.ts | 2 +- src/app/shared/twigs.http.service.ts | 2 +- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 9d7f998..ca695b4 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -6,7 +6,7 @@ import { CookieService } from 'ngx-cookie-service'; import { SwUpdate } from '@angular/service-worker'; import { first, filter, map } from 'rxjs/operators'; import { interval, concat } from 'rxjs'; -import { Router, ActivationEnd } from '@angular/router'; +import { Router, ActivationEnd, ActivatedRoute } from '@angular/router'; import { Actionable, isActionable } from './shared/actionable'; @Component({ @@ -26,13 +26,20 @@ export class AppComponent { @Inject(TWIGS_SERVICE) private twigsService: TwigsService, private location: Location, private cookieService: CookieService, + private router: Router, + private activatedRoute: ActivatedRoute, private appRef: ApplicationRef, private updates: SwUpdate, ) { if (this.cookieService.check('Authorization')) { this.twigsService.getProfile().subscribe(user => { this.user = user; + if (this.activatedRoute.pathFromRoot.length == 0) { + this.router.navigateByUrl("/budgets") + } }); + } else { + this.router.navigateByUrl("/login") } updates.available.subscribe(event => { diff --git a/src/app/budgets/budget.component.ts b/src/app/budgets/budget.component.ts index b797b38..85b38bb 100644 --- a/src/app/budgets/budget.component.ts +++ b/src/app/budgets/budget.component.ts @@ -22,6 +22,10 @@ export class BudgetsComponent implements OnInit { ngOnInit() { this.app.backEnabled = this.isLoggedIn(); this.app.title = 'Budgets'; + if (!this.isLoggedIn()) { + this.loading = false; + return; + } this.twigsService.getBudgets().subscribe( budgets => { this.budgets = budgets; diff --git a/src/app/shared/auth.interceptor.ts b/src/app/shared/auth.interceptor.ts index 7bc653a..21a1a4f 100644 --- a/src/app/shared/auth.interceptor.ts +++ b/src/app/shared/auth.interceptor.ts @@ -16,7 +16,7 @@ export class AuthInterceptor implements HttpInterceptor { } let headers = req.headers; headers = headers.append('Authorization', `Basic ${this.cookieService.get('Authorization')}`); - this.cookieService.set('Authorization', this.cookieService.get('Authorization'), 14); + this.cookieService.set('Authorization', this.cookieService.get('Authorization'), 14, null, null, true); return next.handle(req.clone({headers: headers})); } } \ No newline at end of file diff --git a/src/app/shared/twigs.http.service.ts b/src/app/shared/twigs.http.service.ts index 7e9be40..5c83dc7 100644 --- a/src/app/shared/twigs.http.service.ts +++ b/src/app/shared/twigs.http.service.ts @@ -34,7 +34,7 @@ export class TwigsHttpService implements TwigsService { // }; // return this.http.post(this.apiUrl + '/users/login', params, this.options); const credentials = btoa(`${email}:${password}`) - this.cookieService.set('Authorization', credentials, 14); + this.cookieService.set('Authorization', credentials, 14, null, null, true); return this.getProfile(); }