Browse Source

handle 401 authentication error in a new service

pull/19650/head
Sinan997 2 years ago
parent
commit
d2b2bc712d
  1. 14
      npm/ng-packs/packages/theme-shared/src/lib/providers/error-handlers.provider.ts
  2. 24
      npm/ng-packs/packages/theme-shared/src/lib/services/authentication-error-handler.service.ts
  3. 1
      npm/ng-packs/packages/theme-shared/src/lib/services/index.ts
  4. 30
      npm/ng-packs/packages/theme-shared/src/lib/services/session-error-handler.service.ts

14
npm/ng-packs/packages/theme-shared/src/lib/providers/error-handlers.provider.ts

@ -1,10 +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 { AbpSessionErrorHandlerService } from '../services/session-error-handler.service';
import {
TenantResolveErrorHandlerService,
AbpFormatErrorHandlerService,
StatusCodeErrorHandlerService,
UnknownStatusCodeErrorHandlerService,
AbpAuthenticationErrorHandler,
} from '../services';
export const DEFAULT_HANDLERS_PROVIDERS: Provider[] = [
{
@ -30,6 +32,6 @@ export const DEFAULT_HANDLERS_PROVIDERS: Provider[] = [
{
provide: CUSTOM_ERROR_HANDLERS,
multi: true,
useExisting: AbpSessionErrorHandlerService,
useExisting: AbpAuthenticationErrorHandler,
},
];

24
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();
}
});
}
}

1
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'

30
npm/ng-packs/packages/theme-shared/src/lib/services/session-error-handler.service.ts

@ -1,30 +0,0 @@
import { inject, Injectable } from '@angular/core';
import { AuthService } from '@abp/ng.core';
import { HttpErrorResponse } from '@angular/common/http';
import { getErrorFromRequestBody } from '../utils/error.utils';
import { CustomHttpErrorHandlerService } from '../models/common';
import { ConfirmationService } from '../services/confirmation.service';
import { CUSTOM_HTTP_ERROR_HANDLER_PRIORITY } from '../constants/default-errors';
@Injectable({ providedIn: 'root' })
export class AbpSessionErrorHandlerService implements CustomHttpErrorHandlerService {
readonly priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.high;
private authService = inject(AuthService);
canHandle(error: unknown): boolean {
console.log(error);
debugger;
if (
error instanceof HttpErrorResponse &&
error.headers.get('expires') &&
error.headers.get('pragma')
) {
return true;
}
return false;
}
execute() {
this.authService.logout();
}
}
Loading…
Cancel
Save