(function() {
  var api, cache, errors;

  api = require("./api");

  cache = require("./cache");

  errors = require("./errors");

  module.exports = {
    get: function() {
      return cache.getUser();
    },
    set: function(user) {
      return cache.setUser(user);
    },
    getLoginUrl: function() {
      return api.getLoginUrl();
    },
    logIn: function(code) {
      return api.createSignin(code).then((function(_this) {
        return function(user) {
          return _this.set(user)["return"](user);
        };
      })(this));
    },
    logOut: function() {
      var remove;
      remove = function() {
        return cache.removeUser();
      };
      return this.get().then(function(user) {
        var authToken;
        authToken = user && user.authToken;
        if (authToken) {
          return api.createSignout(authToken).then(remove);
        } else {
          return remove();
        }
      });
    },
    ensureAuthToken: function() {
      return this.get().then(function(user) {
        var at;
        if (user && (at = user.authToken)) {
          return at;
        } else {
          return errors["throw"]("NOT_LOGGED_IN");
        }
      });
    }
  };

}).call(this);
