From edabaf73960b4714221e0e49aa3165ac6c9b9488 Mon Sep 17 00:00:00 2001 From: Kenta420-Poom Date: Thu, 21 Sep 2023 09:01:59 +0700 Subject: [PATCH] update more checking error --- client/src/app/app.module.ts | 10 +++++++++- client/src/app/core/interceptors/error.interceptor.ts | 6 +----- server/routers/auth.go | 7 +++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/client/src/app/app.module.ts b/client/src/app/app.module.ts index 4ddf2a0..f3ce6ef 100644 --- a/client/src/app/app.module.ts +++ b/client/src/app/app.module.ts @@ -4,8 +4,9 @@ import { BrowserModule } from '@angular/platform-browser'; import { AppRoutingModule } from './app-routing.module'; import { FooterComponent } from './core/layout/footer.component'; import { HeaderComponent } from './core/layout/header.component'; -import { HttpClientModule } from '@angular/common/http'; +import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; import { AppComponent } from './app.component'; +import { ErrorInterceptor } from './core/interceptors/error.interceptor'; @NgModule({ declarations: [AppComponent], @@ -16,6 +17,13 @@ import { AppComponent } from './app.component'; AppRoutingModule, HttpClientModule, ], + providers: [ + { + provide: HTTP_INTERCEPTORS, + useClass: ErrorInterceptor, + multi: true, + }, + ], bootstrap: [AppComponent], }) export class AppModule {} diff --git a/client/src/app/core/interceptors/error.interceptor.ts b/client/src/app/core/interceptors/error.interceptor.ts index 51aae63..028541d 100644 --- a/client/src/app/core/interceptors/error.interceptor.ts +++ b/client/src/app/core/interceptors/error.interceptor.ts @@ -8,7 +8,7 @@ import { } from '@angular/common/http'; import { Observable, catchError, retry, throwError } from 'rxjs'; -@Injectable() +@Injectable({ providedIn: 'root' }) export class ErrorInterceptor implements HttpInterceptor { constructor() {} @@ -19,11 +19,7 @@ export class ErrorInterceptor implements HttpInterceptor { return next.handle(request).pipe( catchError((error) => { if (error instanceof HttpErrorResponse) { - if (error.status === 401) { - return next.handle(request); - } } - return throwError(() => error); }) ); diff --git a/server/routers/auth.go b/server/routers/auth.go index 1c9444b..c53cea7 100644 --- a/server/routers/auth.go +++ b/server/routers/auth.go @@ -191,6 +191,13 @@ func (ar *AuthRouter) Route(r chi.Router) { return } + if userInfo["error"] != nil { + w.Header().Add("Content-Type", "application/json") + w.WriteHeader(http.StatusUnauthorized) + json.NewEncoder(w).Encode(userInfo) + return + } + // return user info w.Header().Add("Content-Type", "application/json") json.NewEncoder(w).Encode(map[string]interface{}{