update more checking error

This commit is contained in:
Kenta420-Poom 2023-09-21 09:01:59 +07:00
parent 8221183630
commit edabaf7396
3 changed files with 17 additions and 6 deletions

View file

@ -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 {}

View file

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

View file

@ -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{}{