import { Feed , Observable } from '../feed/feed' ; import { LoginResponse } from '../http/messages' ; import { WebClient } from '../http/web-client' ; import { IndexedDBLoginStore } from '../indexeddb/indexeddb-login-store' ; import { Query } from '../query/query' ; import { FactEnvelope , FactRecord , FactReference } from '../storage' ; import { Authentication } from './authentication' ; export class AuthenticationOffline implements Authentication { constructor ( private inner , private store , private client ) { } async login ( ) { try { return await this . loginRemote ( ) ; } catch ( err ) { if ( err === 'Unauthorized' ) { throw err ; } try { return await this . loginLocal ( ) ; } catch ( err2 ) { throw err ; } } } local ( ) { throw new Error ( 'Local device has no persistence.' ) ; } async save ( envelopes ) { const saved = await this . inner . save ( envelopes ) ; return saved ; } query ( start , query ) { return this . inner . query ( start , query ) ; } exists ( fact ) { throw new Error ( "Exists method not implemented on AuthenticationImpl." ) ; } load ( references ) { return this . inner . load ( references ) ; } from ( fact , query ) { return this . inner . from ( fact , query ) ; } private async loginRemote ( ) { const result = await this . client . login ( ) ; if ( result && result . userFact && result . profile ) { await this . store . saveLogin ( 'token' , result . userFact , result . profile . displayName ) ; } return result ; } private async loginLocal ( ) { const result = await this . store . loadLogin ( 'token' ) ; return { userFact : result . userFact , profile : { displayName : result . displayName } } ; } }