Skip to content

Instantly share code, notes, and snippets.

@igorwojda
Last active February 16, 2020 19:09
Show Gist options
  • Select an option

  • Save igorwojda/2a2c2946a31ecbb8e05f393e54e036f0 to your computer and use it in GitHub Desktop.

Select an option

Save igorwojda/2a2c2946a31ecbb8e05f393e54e036f0 to your computer and use it in GitHub Desktop.

Revisions

  1. igorwojda revised this gist Feb 16, 2020. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion login-user.kt
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@ val retrofit = Retrofit.Builder()
    val userService = retrofit.create(UserService::class.java)
    val accountRepository = AccountRepository(context)

    // Wee need this because userService.login function is suspended
    CoroutineScope(Dispatchers.IO).launch {
    val response = userService.login("email", "password")

    @@ -22,4 +23,11 @@ interface UserService {
    @Query("email") email: String,
    @Query("password") password: String
    ): LoginResponse?
    }
    }

    // Data model returned by login endpoint
    // (JSON is converted by moshi converter into Kotlin data class instance)
    data class LoginResponse(
    val accessToken: String,
    val refreshToken: String
    )
  2. igorwojda revised this gist Feb 16, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion login-user.kt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    val retrofit = Retrofit.Builder()
    .baseUrl("https://igor.wojda.com/app")
    .baseUrl("https://igorwojda.com/app")
    .build()

    val userService = retrofit.create(UserService::class.java)
  3. igorwojda revised this gist Feb 16, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions login-user.kt
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    val retrofit = Retrofit.Builder()
    .baseUrl("https://igor.wojda.com/app")
    .build()

    val userService = retrofit.create(UserService::class.java)
    val accountRepository = AccountRepository(context)

  4. igorwojda revised this gist Feb 16, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion login-user.kt
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ CoroutineScope(Dispatchers.IO).launch {
    // Retrofit service definition
    interface UserService {
    @POST("auth/login")
    fun authorizeAsync(
    suspend fun authorizeAsync(
    @Query("email") email: String,
    @Query("password") password: String
    ): LoginResponse?
  5. igorwojda created this gist Feb 16, 2020.
    21 changes: 21 additions & 0 deletions login-user.kt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    val userService = retrofit.create(UserService::class.java)
    val accountRepository = AccountRepository(context)

    CoroutineScope(Dispatchers.IO).launch {
    val response = userService.login("email", "password")

    if(response != null) {
    accountRepository.addAccount(response.accessToken, response.refreshToken)
    } else {
    // show error
    }
    }

    // Retrofit service definition
    interface UserService {
    @POST("auth/login")
    fun authorizeAsync(
    @Query("email") email: String,
    @Query("password") password: String
    ): LoginResponse?
    }