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); }), ); };