Skip to content

Instantly share code, notes, and snippets.

@igorwojda
Last active February 16, 2020 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorwojda/2a2c2946a31ecbb8e05f393e54e036f0 to your computer and use it in GitHub Desktop.
Save igorwojda/2a2c2946a31ecbb8e05f393e54e036f0 to your computer and use it in GitHub Desktop.
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