Browse Source

Merge pull request #19650 from abpframework/sinan/authorization-logout

logout if user is unauthorized
pull/19664/head
Masum ULU 2 years ago
committed by GitHub
parent
commit
fa23e93065
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 16
      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. 3
      npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.ts

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

@ -1,9 +1,12 @@
import { Provider } from '@angular/core'; import { Provider } from '@angular/core';
import { CUSTOM_ERROR_HANDLERS } from '../tokens'; import { CUSTOM_ERROR_HANDLERS } from '../tokens';
import { TenantResolveErrorHandlerService } from '../services/tenant-resolve-error-handler.service'; import {
import { AbpFormatErrorHandlerService } from '../services/abp-format-error-handler.service'; TenantResolveErrorHandlerService,
import { StatusCodeErrorHandlerService } from '../services/status-code-error-handler.service'; AbpFormatErrorHandlerService,
import { UnknownStatusCodeErrorHandlerService } from '../services/unknown-status-code-error-handler.service'; StatusCodeErrorHandlerService,
UnknownStatusCodeErrorHandlerService,
AbpAuthenticationErrorHandler,
} from '../services';
export const DEFAULT_HANDLERS_PROVIDERS: Provider[] = [ export const DEFAULT_HANDLERS_PROVIDERS: Provider[] = [
{ {
@ -26,4 +29,9 @@ export const DEFAULT_HANDLERS_PROVIDERS: Provider[] = [
multi: true, multi: true,
useExisting: UnknownStatusCodeErrorHandlerService, useExisting: UnknownStatusCodeErrorHandlerService,
}, },
{
provide: CUSTOM_ERROR_HANDLERS,
multi: true,
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 './status-code-error-handler.service';
export * from './unknown-status-code-error-handler.service'; export * from './unknown-status-code-error-handler.service';
export * from './router-error-handler.service'; export * from './router-error-handler.service';
export * from './authentication-error-handler.service'

3
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 authService = inject(AuthService);
protected readonly handledStatusCodes = [401, 403, 404, 500] as const; 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; readonly priority = CUSTOM_HTTP_ERROR_HANDLER_PRIORITY.normal;
@ -90,7 +90,6 @@ export class StatusCodeErrorHandlerService implements CustomHttpErrorHandlerServ
this.showConfirmation(title, message).subscribe(); this.showConfirmation(title, message).subscribe();
break; break;
case 403: case 403:
case 500: case 500:
this.showPage(); this.showPage();

Loading…
Cancel
Save