text: import request from './fakeRequest'
  
  let localStorage
  
  // If we're testing, use a local storage polyfill
  if (global.process && process.env.NODE_ENV === 'test') {
    localStorage = require('localStorage')
  } else {
    // If not, use the browser one
    localStorage = global.window.localStorage
  }
  
  let auth = {
    /**
    * Logs a user in, returning a promise with `true` when done
    * @param  {string} username The username of the user
    * @param  {string} password The password of the user
    */
    login (username, password) {
      if (auth.loggedIn()) return Promise.resolve(true)
  
      // Post a fake request
      return request.post('/login', {username, password})
        .then(response => {
          // Save token to local storage
          localStorage.token = response.token
          localStorage.groups = response.groups
          return Promise.resolve(true)
        })
    },
  
    /**
    * Logs the current user out
    */
    logout () {
      return request.post('/logout')
    },
  
    /**
    * Checks if a user is logged in
    */
    loggedIn () {
      return !!localStorage.token
    },
  
    /**
    * Returns the user's key
    */
    getToken () {
      return localStorage.token
    },
    /**
    * Returns the user's group
    */
    getGroup () {
      return localStorage.groups
    },
  
    /**
    * Registers a user and then logs them in
    * @param  {string} username The username of the user
    * @param  {string} password The password of the user
    */
    register (username, password) {
      // Post a fake request
      return request.post('/register', {username, password})
        // Log user in after registering
        .then(() => auth.login(username, password))
    },
    onChange () {}
  }
  
  export default auth
  