diff --git a/packages/services/src/Domain/Api/ApiServiceInterface.ts b/packages/services/src/Domain/Api/ApiServiceInterface.ts index b2ac9d93c..598f61838 100644 --- a/packages/services/src/Domain/Api/ApiServiceInterface.ts +++ b/packages/services/src/Domain/Api/ApiServiceInterface.ts @@ -1,12 +1,15 @@ -import { AbstractService } from '../Service/AbstractService' -import { Uuid } from '@standardnotes/common' +import { Either, Uuid } from '@standardnotes/common' import { Role } from '@standardnotes/auth' import { FilesApiInterface } from '@standardnotes/files' +import { Session } from '@standardnotes/domain-core' + +import { AbstractService } from '../Service/AbstractService' /* istanbul ignore file */ export enum ApiServiceEvent { MetaReceived = 'MetaReceived', + SessionRefreshed = 'SessionRefreshed', } export type MetaReceivedData = { @@ -14,6 +17,10 @@ export type MetaReceivedData = { userRoles: Role[] } -export interface ApiServiceInterface - extends AbstractService, - FilesApiInterface {} +export type SessionRefreshedData = { + session: Session +} + +export type ApiServiceEventData = Either + +export interface ApiServiceInterface extends AbstractService, FilesApiInterface {} diff --git a/packages/snjs/lib/Application/Application.ts b/packages/snjs/lib/Application/Application.ts index 0322ed32b..653f8667e 100644 --- a/packages/snjs/lib/Application/Application.ts +++ b/packages/snjs/lib/Application/Application.ts @@ -75,6 +75,7 @@ import { AuthManager, RevisionClientInterface, RevisionManager, + ApiServiceEvent, } from '@standardnotes/services' import { BackupServiceInterface, FilesClientInterface } from '@standardnotes/files' import { ComputePrivateUsername } from '@standardnotes/encryption' @@ -1288,6 +1289,7 @@ export class SNApplication implements ApplicationInterface, AppGroupManagedAppli this.internalEventBus.addEventHandler(this.integrityService, ExternalServices.SyncEvent.SyncRequestsIntegrityCheck) this.internalEventBus.addEventHandler(this.syncService, ExternalServices.IntegrityEvent.IntegrityCheckCompleted) this.internalEventBus.addEventHandler(this.userService, AccountEvent.SignedInOrRegistered) + this.internalEventBus.addEventHandler(this.sessionManager, ApiServiceEvent.SessionRefreshed) } private clearInternalEventBus(): void { diff --git a/packages/snjs/lib/Services/Api/ApiService.ts b/packages/snjs/lib/Services/Api/ApiService.ts index bf70a4b01..65fd1283f 100644 --- a/packages/snjs/lib/Services/Api/ApiService.ts +++ b/packages/snjs/lib/Services/Api/ApiService.ts @@ -10,7 +10,6 @@ import { ItemsServerInterface, StorageKey, ApiServiceEvent, - MetaReceivedData, DiagnosticInfo, KeyValueStoreInterface, API_MESSAGE_GENERIC_SYNC_FAIL, @@ -31,6 +30,7 @@ import { API_MESSAGE_INVALID_SESSION, API_MESSAGE_LOGIN_IN_PROGRESS, API_MESSAGE_TOKEN_REFRESH_IN_PROGRESS, + ApiServiceEventData, } from '@standardnotes/services' import { FilesApiInterface } from '@standardnotes/files' import { ServerSyncPushContextualPayload, SNFeatureRepo, FileContent } from '@standardnotes/models' @@ -56,7 +56,7 @@ const V0_API_VERSION = '20200115' type InvalidSessionObserver = (revoked: boolean) => void export class SNApiService - extends AbstractService + extends AbstractService implements ApiServiceInterface, FilesApiInterface, @@ -443,6 +443,11 @@ export class SNApiService this.setSession(session) this.processResponse(response) + + await this.notifyEventSync(ApiServiceEvent.SessionRefreshed, { + session, + }) + return response }) .catch((errorResponse) => { diff --git a/packages/snjs/lib/Services/Session/SessionManager.ts b/packages/snjs/lib/Services/Session/SessionManager.ts index 04869d3f8..5a96a4bd3 100644 --- a/packages/snjs/lib/Services/Session/SessionManager.ts +++ b/packages/snjs/lib/Services/Session/SessionManager.ts @@ -22,6 +22,10 @@ import { UNSUPPORTED_KEY_DERIVATION, UNSUPPORTED_PROTOCOL_VERSION, Challenge, + InternalEventHandlerInterface, + InternalEventInterface, + ApiServiceEvent, + SessionRefreshedData, } from '@standardnotes/services' import { Base64String } from '@standardnotes/sncrypto-common' import { ClientDisplayableError, SessionBody } from '@standardnotes/responses' @@ -67,7 +71,10 @@ export enum SessionEvent { * server credentials, such as the session token. It also exposes methods for registering * for a new account, signing into an existing one, or changing an account password. */ -export class SNSessionManager extends AbstractService implements SessionsClientInterface { +export class SNSessionManager + extends AbstractService + implements SessionsClientInterface, InternalEventHandlerInterface +{ private user?: Responses.User private isSessionRenewChallengePresented = false @@ -94,6 +101,12 @@ export class SNSessionManager extends AbstractService implements S }) } + async handleEvent(event: InternalEventInterface): Promise { + if (event.type === ApiServiceEvent.SessionRefreshed) { + this.httpService.setSession((event.payload as SessionRefreshedData).session) + } + } + override deinit(): void { ;(this.protocolService as unknown) = undefined ;(this.diskStorageService as unknown) = undefined