mirror of
https://github.com/dbeaver/cloudbeaver.git
synced 2026-09-24 16:04:36 +08:00
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
/*
|
|
* CloudBeaver - Cloud Database Manager
|
|
* Copyright (C) 2020-2024 DBeaver Corp and others
|
|
*
|
|
* Licensed under the Apache License, Version 2.0.
|
|
* you may not use this file except in compliance with the License.
|
|
*/
|
|
import { Bootstrap, injectable } from '@cloudbeaver/core-di';
|
|
import { Executor, IExecutor } from '@cloudbeaver/core-executor';
|
|
import { EServerErrorCode, GQLError, GraphQLService, SessionError } from '@cloudbeaver/core-sdk';
|
|
import { errorOf } from '@cloudbeaver/core-utils';
|
|
@injectable()
|
|
export class SessionExpireService extends Bootstrap {
|
|
expired = false;
|
|
|
|
onSessionExpire: IExecutor;
|
|
constructor(private readonly graphQLService: GraphQLService) {
|
|
super();
|
|
this.onSessionExpire = new Executor();
|
|
}
|
|
|
|
register(): void {
|
|
this.graphQLService.registerInterceptor(this.sessionExpiredInterceptor.bind(this));
|
|
}
|
|
|
|
load(): void {}
|
|
|
|
sessionExpired(): void {
|
|
if (this.expired) {
|
|
return;
|
|
}
|
|
|
|
const e = new SessionError('Session expired');
|
|
this.graphQLService.blockRequests(e);
|
|
this.expired = true;
|
|
this.onSessionExpire.execute();
|
|
}
|
|
|
|
private async sessionExpiredInterceptor(request: Promise<any>): Promise<any> {
|
|
try {
|
|
return await request;
|
|
} catch (exception: any) {
|
|
const gqlError = errorOf(exception, GQLError);
|
|
if (gqlError?.errorCode === EServerErrorCode.sessionExpired) {
|
|
this.sessionExpired();
|
|
}
|
|
throw exception;
|
|
}
|
|
}
|
|
}
|