All files / src/auth sessionStore.js

23.53% Statements 8/34
18.75% Branches 3/16
33.33% Functions 4/12
23.53% Lines 8/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                27x                                                         27x 27x 27x           54x     54x       32x 32x                                                                                                                                        
/* @flow */
import { SessionData } from './sessionData'
import type { SessionOptions } from './sessionData'
// import { BLOCKSTACK_GAIA_HUB_LABEL } from '../storage/hub'
import {
  LOCALSTORAGE_SESSION_KEY
} from './authConstants'
import { NoSessionDataError } from '../errors'
// import { Logger } from '../logger'
 
/**
 * An abstract class representing the SessionDataStore interface.
 * @type {SessionData}
 */
export class SessionDataStore {
  constructor(sessionOptions?: SessionOptions) {
    Iif (sessionOptions) {
      const newSessionData = new SessionData(sessionOptions)
      this.setSessionData(newSessionData)
    }
  }
 
  getSessionData() : SessionData {
    throw new Error('Abstract class')
  }
 
  /* eslint-disable */
  setSessionData(session: SessionData): boolean {
    throw new Error('Abstract class')
  }
 
  deleteSessionData(): boolean {
    throw new Error('Abstract class')
  }
  /* eslint-enable */
}
 
/**
 * Stores session data in the instance of this class.
 * @type {InstanceDataStore}
 */
export class InstanceDataStore extends SessionDataStore {
  sessionData: ?SessionData
 
  constructor(sessionOptions?: SessionOptions) {
    super(sessionOptions)
    Eif (!this.sessionData) {
      this.setSessionData(new SessionData({}))
    }
  }
 
 
  getSessionData() : SessionData {
    Iif (!this.sessionData) {
      throw new NoSessionDataError('No session data was found.')
    }
    return this.sessionData
  }
 
  setSessionData(session: SessionData): boolean {
    this.sessionData = session
    return true
  }
 
  deleteSessionData(): boolean {
    this.setSessionData(new SessionData({}))
    return true
  }
}
 
/**
 * Stores session data in browser a localStorage entry.
 * @type {LocalStorageStore}
 */
export class LocalStorageStore extends SessionDataStore {
  key: string
 
  constructor(sessionOptions?: SessionOptions) {
    super(sessionOptions)
    if (sessionOptions
      && sessionOptions.storeOptions
      && sessionOptions.storeOptions.localStorageKey
      && (sessionOptions.storeOptions.localStorageKey instanceof String)) {
      this.key = sessionOptions.storeOptions.localStorageKey
    } else {
      this.key = LOCALSTORAGE_SESSION_KEY
    }
 
    const data = localStorage.getItem(this.key)
    if (!data) {
      const sessionData = new SessionData({})
      this.setSessionData(sessionData)
    }
  }
 
  getSessionData(): SessionData {
    const data = localStorage.getItem(this.key)
    if (!data) {
      throw new NoSessionDataError('No session data was found in localStorage')
    }
    const dataJSON = JSON.parse(data)
    return SessionData.fromJSON(dataJSON)
  }
 
  setSessionData(session: SessionData): boolean {
    localStorage.setItem(this.key, session.toString())
    return true
  }
 
  deleteSessionData(): boolean {
    localStorage.removeItem(this.key)
    this.setSessionData(new SessionData({}))
    return true
  }
 
  // checkForLegacyDataAndMigrate(): Promise<SessionData> {
  //   const legacyTransitKey = localStorage.getItem(BLOCKSTACK_APP_PRIVATE_KEY_LABEL)
  //   const legacyGaiaConfig = localStorage.getItem(BLOCKSTACK_GAIA_HUB_LABEL)
  //   const legacyUserData = localStorage.getItem(BLOCKSTACK_STORAGE_LABEL)
  //
  //
  //   if (legacyTransitKey) {
  //     localStorage.removeItem(BLOCKSTACK_APP_PRIVATE_KEY_LABEL)
  //   }
  //
  //
  //
  // }
}