From abc1b3672127e6376b45f243c122e32cb9d1450f Mon Sep 17 00:00:00 2001 From: Amritanshu Date: Fri, 2 May 2025 02:02:25 +0000 Subject: [PATCH] Auth errors were not showing up as they were swallowed up by the auth interceptor. Once caught, the device name was not being shown as it was triggering an expression changed after detection error. --- bookie/src/app/auth/login/login.component.ts | 32 ++++++++++---------- bookie/src/app/core/auth.interceptor.ts | 6 ++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/bookie/src/app/auth/login/login.component.ts b/bookie/src/app/auth/login/login.component.ts index 43a1c02..6a8c884 100644 --- a/bookie/src/app/auth/login/login.component.ts +++ b/bookie/src/app/auth/login/login.component.ts @@ -1,4 +1,4 @@ -import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'; +import { AfterViewInit, ChangeDetectorRef, Component, ElementRef, OnInit, ViewChild } from '@angular/core'; import { FormControl, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms'; import { MatButtonModule } from '@angular/material/button'; import { MatCardModule } from '@angular/material/card'; @@ -44,6 +44,7 @@ export class LoginComponent implements OnInit, AfterViewInit { private router: Router, private toaster: ToasterService, private cs: CookieService, + private cd: ChangeDetectorRef, ) { this.hide = true; this.unregisteredDevice = false; @@ -73,20 +74,19 @@ export class LoginComponent implements OnInit, AfterViewInit { const formModel = this.form.value; const username = formModel.username ?? ''; const password = formModel.password ?? ''; - this.auth - .login(username.trim(), password.trim()) - // .pipe(first()) - .subscribe({ - next: () => { - this.router.navigateByUrl(this.returnUrl); - }, - error: (error) => { - if (error.status === 401 && error.detail === 'Device is not registered') { - this.unregisteredDevice = true; - this.deviceName = this.cs.getCookie('device'); - } - this.toaster.show('Error', error.detail); - }, - }); + this.auth.login(username.trim(), password.trim()).subscribe({ + next: () => { + this.router.navigateByUrl(this.returnUrl); + }, + error: (error) => { + if (error.status === 401 && error.error.detail === 'Device is not registered') { + this.unregisteredDevice = true; + this.deviceName = this.cs.getCookie('device'); + console.log(this.deviceName, this.unregisteredDevice); + this.cd.detectChanges(); + } + this.toaster.show('Error', error.error.detail); + }, + }); } } diff --git a/bookie/src/app/core/auth.interceptor.ts b/bookie/src/app/core/auth.interceptor.ts index b965adf..ebd9b40 100644 --- a/bookie/src/app/core/auth.interceptor.ts +++ b/bookie/src/app/core/auth.interceptor.ts @@ -21,13 +21,13 @@ export const authInterceptor: HttpInterceptorFn = (req, next) => { if (req.url.includes('/refresh')) { inject(AuthService).logout(); } - return throwError(() => new Error(err)); + 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(() => new Error(error)); + return throwError(() => error); } // auto logout if 401 response returned from api inject(AuthService).logout(); @@ -47,7 +47,7 @@ export const authInterceptor: HttpInterceptorFn = (req, next) => { inject(Router).navigate(['login']); } }); - return throwError(() => new Error(err)); + return throwError(() => err); }), ); };