rootstrap/mobx-session

View on GitHub
src/index.js

Summary

Maintainability
A
0 mins
Test Coverage
import { extendObservable, runInAction } from 'mobx';
import { Storage } from './storage';

import { DEFAULT_INSTANCE_NAME, SESSION_KEY } from './constants';

class Store {
  constructor() {
    extendObservable(this, {
      hasSession: false,
      initialized: false,
    });
  }

  initialize = async (options = { name: DEFAULT_INSTANCE_NAME }) => {
    Storage.initialize(options);
    const hasSession = await this.getSession() !== null;
    runInAction(() => {
      this.initialized = true;
      this.hasSession = hasSession;
    });
  }

  saveSession = async (session) => {
    await Storage.set(SESSION_KEY, session);
    runInAction(() => {
      this.hasSession = true;
    });
  }

  getSession = () => Storage.get(SESSION_KEY);

  deleteSession = () => {
    Storage.remove(SESSION_KEY);
    runInAction(() => {
      this.hasSession = false;
    });
  }
}

export default new Store();