diff --git a/npm/ng-packs/packages/theme-shared/src/lib/providers/error-handlers.provider.ts b/npm/ng-packs/packages/theme-shared/src/lib/providers/error-handlers.provider.ts index d456c82912..84e8fe9b19 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/providers/error-handlers.provider.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/providers/error-handlers.provider.ts @@ -1,9 +1,12 @@ import { Provider } from '@angular/core'; import { CUSTOM_ERROR_HANDLERS } from '../tokens'; -import { TenantResolveErrorHandlerService } from '../services/tenant-resolve-error-handler.service'; -import { AbpFormatErrorHandlerService } from '../services/abp-format-error-handler.service'; -import { StatusCodeErrorHandlerService } from '../services/status-code-error-handler.service'; -import { UnknownStatusCodeErrorHandlerService } from '../services/unknown-status-code-error-handler.service'; +import { + TenantResolveErrorHandlerService, + AbpFormatErrorHandlerService, + StatusCodeErrorHandlerService, + UnknownStatusCodeErrorHandlerService, + AbpAuthenticationErrorHandler, +} from '../services'; export const DEFAULT_HANDLERS_PROVIDERS: Provider[] = [ { @@ -26,4 +29,9 @@ export const DEFAULT_HANDLERS_PROVIDERS: Provider[] = [ multi: true, useExisting: UnknownStatusCodeErrorHandlerService, }, + { + provide: CUSTOM_ERROR_HANDLERS, + multi: true, + useExisting: AbpAuthenticationErrorHandler, + }, ]; diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/authentication-error-handler.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/authentication-error-handler.service.ts new file mode 100644 index 0000000000..602514bee8 --- /dev/null +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/authentication-error-handler.service.ts @@ -0,0 +1,24 @@ +import { inject, Injectable } from '@angular/core'; +import { AuthService, ConfigStateService } from '@abp/ng.core'; +import { HttpErrorResponse } from '@angular/common/http'; +import { CustomHttpErrorHandlerService } from '../models/common'; +import { CUSTOM_HTTP_ERROR_HANDLER_PRIORITY } from '../constants/default-errors'; + +@Injectable({ providedIn: 'root' }) +export class AbpAuthenticationErrorHandler implements CustomHttpErrorHandlerService { + readonly priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.veryHigh; + protected readonly authService = inject(AuthService); + protected readonly configStateService = inject(ConfigStateService); + + canHandle(error: unknown): boolean { + return error instanceof HttpErrorResponse && error.status === 401; + } + + execute() { + this.configStateService.refreshAppState().subscribe(({ currentUser }) => { + if (!currentUser.isAuthenticated) { + this.authService.logout(); + } + }); + } +} diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/index.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/index.ts index 41f1b84039..521d86b9c2 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/index.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/index.ts @@ -9,3 +9,4 @@ export * from './tenant-resolve-error-handler.service'; export * from './status-code-error-handler.service'; export * from './unknown-status-code-error-handler.service'; export * from './router-error-handler.service'; +export * from './authentication-error-handler.service' diff --git a/npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.ts b/npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.ts index 976bb11702..0e22ac458c 100644 --- a/npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.ts +++ b/npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.ts @@ -17,7 +17,7 @@ export class StatusCodeErrorHandlerService implements CustomHttpErrorHandlerServ protected readonly authService = inject(AuthService); protected readonly handledStatusCodes = [401, 403, 404, 500] as const; - protected status: typeof this.handledStatusCodes[number]; + protected status: (typeof this.handledStatusCodes)[number]; readonly priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.normal; @@ -90,7 +90,6 @@ export class StatusCodeErrorHandlerService implements CustomHttpErrorHandlerServ this.showConfirmation(title, message).subscribe(); break; - case 403: case 500: this.showPage();