title: Kotlin Fuel HTTP Async VS. Blocking for user authentication
body: <p>I'm having issues with the way I authenticate my user in my Android application, here's how I do it right now:</p>
  
  <p>When the Login button is pressed:</p>
  
  <pre><code>// Get user's email and password from the input
  val user = User()
  user.email = emailInput?.text.toString()
  user.password = passwordInput?.text.toString()
  
   // Execute sign in request
   if (userService.signIn(user)) {
         Log.d("HTTP", "Success, switching to profile")
         // Login success, show profile page
         errorText?.text = ""
         val profileFragment = ProfileFragment()
         fragmentManager.beginTransaction()
               .add(R.id.fragment_container, profileFragment).addToBackStack("profile fragment").commit()
  } else {
         // Login failed, show error message
         errorText?.text = "Login Error"
  }
  </code></pre>
  
  <p>And here's my Fuel POST request:</p>
  
  <pre><code>fun signIn(user: User): Boolean {
      val url = "$ENDPOINT/signin"
      var success = false
  
      val json = JSONObject()
      json.put("email", user.email).put("password", user.password)
  
      val (request, response, result) = Fuel.post(url).body(json.toString()).responseJson()
  
      Log.i("Login", "Request to sign in user ${user.email}...")
      result.success { jsonRes: Json -&gt;
          Log.i("Login", "Got response $jsonRes")
          val code: String = jsonRes.obj().getString("code")
          if (code == "200") {
              success = true
          }
      }
      result.failure {
          Log.d("HTTP", "FAILURE : $result")
      }
  
      return success
  }
  </code></pre>
  
  <p>As you can see my request is blocking (thank to <a href="https://github.com/kittinunf/Fuel" rel="nofollow noreferrer">Fuel</a>) since I want to return a Boolean to confirm that the log in was a success, but this brings up the 
  <code>NetworkOnMainThreadException</code> exception.</p>
  
  <p>Is there a better / more common way to handle sign-ins in an Asynchronous fashion? How can I tell my Fragment that the login was a success without waiting for the request on another thread?</p>