Last active
February 16, 2020 19:09
-
-
Save igorwojda/2a2c2946a31ecbb8e05f393e54e036f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val retrofit = Retrofit.Builder() | |
.baseUrl("https://igorwojda.com/app") | |
.build() | |
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") | |
if(response != null) { | |
accountRepository.addAccount(response.accessToken, response.refreshToken) | |
} else { | |
// show error | |
} | |
} | |
// Retrofit service definition | |
interface UserService { | |
@POST("auth/login") | |
suspend fun authorizeAsync( | |
@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 | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment