 url = require 'url'
-https = require 'https'
-remote = require 'remote'
 shell = require 'shell'
 path = require 'path'
+version = require './version'
+fetch = require './fetch'
 _token = require './token'
-config = require './config'
-BrowserWindow = remote.BrowserWindow
+{learnCo} = require './config'
+{BrowserWindow} = require 'remote'
 
-workspaceView = atom.views.getView(atom.workspace)
+AUTH_URL = "#{learnCo}/api/v1/users/me?ile_version=#{version}"
 
 confirmOauthToken = (token) ->
-  return new Promise (resolve, reject) ->
-    try
-      authRequest = https.get
-        host: 'learn.co'
-        path: '/api/v1/users/me?ile_version=' + atom.appVersion
-        headers:
-          'Authorization': 'Bearer ' + token
-      , (response) ->
-        body = ''
+  headers = new Headers({'Authorization': "Bearer #{token}"})
 
-        response.on 'data', (d) ->
-          body += d
-
-        response.on 'error', reject
-
-        response.on 'end', ->
-          try
-            parsed = JSON.parse(body)
-
-            if parsed.email
-              resolve parsed
-            else
-              resolve false
-          catch
-            reject false
-
-      authRequest.on 'error', (err) ->
-        reject err
-
-    catch err
-      reject err
+  fetch(AUTH_URL, {headers}).then (data) ->
+    if data.email? then data else false
 
 githubLogin = () ->
   new Promise (resolve, reject) ->

        win.destroy()
        resolve()

    if not win.loadURL("#{config.learnCo}/ide/token?ide_config=true")
      promptManualEntry()

learnSignIn = () ->
  new Promise (resolve, reject) ->

        win.destroy()
        githubLogin().then(resolve)

    if not win.loadURL("#{learnCo}/ide/sign_in?ide_onboard=true")
      win.destroy()
      githubLogin.then(resolve)

githubLogout = ->
  win = new BrowserWindow(show: false)
