 define [
   'underscore'
+  'lib/utils/promises',
   'lib/utils/emitter',
   'lib/api/api',
   'lib/api/reporting',
   'lib/utils/entity',
   'lib/utils/config',
   'lib/api/current-user'
-  ], (_, emitter, api, reporting, entity, configParser, currentUser)->
+  ], (_, promises, emitter, api, reporting, entity, configParser, currentUser)->
 
     create = (config)->
       _emitter = emitter()

        _emitter.on 'hull.auth.login', (me)-> _reporting.track('hull.auth.login', me)
        _emitter.on 'hull.auth.logout', ()-> _reporting.track('hull.auth.logout')

        resolveUserPromise = (promise)->
          promise.then (user)->
            _emitter.emit('hull.auth.login', user)
          , (error) ->
            _emitter.emit('hull.auth.fail', error)

          promise

        created =
          config: configParser(config, _emitter)

          flag: _reporting.flag
          api: api.api
          currentUser: currentUser(_emitter)
          signUp: (args...)->
            handleUserPromise(api.auth.signUp(args...))
          login: (args...)->
            if (api.auth.isAuthenticating())
              return console.info "Authentication is in progress. Use `Hull.on('hull.auth.login', fn)` to call `fn` when done."

            handleUserPromise(api.auth.login(args...))
          logout: api.auth.logout
          linkIdentity: (provider, options = {}, callback)->
            return noCurentUserDeferred.promise unless created.currentUser()

            options.mode = 'connect'
            created.login(provider, options, callback)
          unlinkIdentity: (provider, callback)->
            return noCurentUserDeferred.promise unless created.currentUser()

            promise = api.api("me/identities/#{provider}", 'delete').then(api.api.bind(api, 'me'))
            promise.then (user)->
              _emitter.emit('hull.auth.login', user)
              callback(user)

            promise
          util:
            entity: entity
            eventEmitter: _emitter
