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
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with
38 additions and
6 deletions
-
npm/ng-packs/packages/theme-shared/src/lib/providers/error-handlers.provider.ts
-
npm/ng-packs/packages/theme-shared/src/lib/services/authentication-error-handler.service.ts
-
npm/ng-packs/packages/theme-shared/src/lib/services/index.ts
-
npm/ng-packs/packages/theme-shared/src/lib/services/status-code-error-handler.service.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, |
|
|
|
}, |
|
|
|
]; |
|
|
|
|
|
|
|
@ -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(); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -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' |
|
|
|
|
|
|
|
@ -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(); |
|
|
|
|