use retrofir to make rest api call for email login

This commit is contained in:
Satindar Dhillon 2022-08-09 19:47:15 -07:00
parent 0f13d234e4
commit 65a420ad8c
4 changed files with 62 additions and 1 deletions

View file

@ -91,5 +91,12 @@ dependencies {
// optional - Test helpers for Lifecycle runtime
// testImplementation ("androidx.lifecycle:lifecycle-runtime-testing:$lifecycle_version")
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
// coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
}

View file

@ -13,6 +13,7 @@
android:supportsRtl="true"
android:theme="@style/Theme.Omnivore"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true"
@ -26,4 +27,5 @@
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>

View file

@ -3,9 +3,25 @@ package app.omnivore.omnivore
import android.content.ContentValues
import android.util.Log
import androidx.lifecycle.ViewModel
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
class LoginViewModel: ViewModel() {
fun login(email: String, password: String) {
Log.v(ContentValues.TAG, "in view model $email / $password")
val emailLogin = RetrofitHelper.getInstance().create(EmailLoginSubmit::class.java)
GlobalScope.launch {
val result = emailLogin.submitEmailLogin(
EmailLoginCredentials(email = email, password = password)
)
// TODO: parse out result and store auth token
// set some variable that compose can observe
if (result != null) {
Log.d(ContentValues.TAG, result.body().toString())
} else {
Log.d(ContentValues.TAG, result.body().toString())
}
}
}
}

View file

@ -0,0 +1,36 @@
package app.omnivore.omnivore
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.Headers
import retrofit2.http.POST
data class EmailAuthPayload(
val authCookieString: String?,
val authToken: String?,
val pendingEmailVerification: Boolean?
)
data class EmailLoginCredentials(
val email: String,
val password: String
)
interface EmailLoginSubmit {
@Headers("Content-Type: application/json")
@POST("/api/mobile-auth/email-sign-in")
suspend fun submitEmailLogin(@Body credentials: EmailLoginCredentials): Response<EmailAuthPayload>
}
object RetrofitHelper {
private const val baseUrl = "https://api-demo.omnivore.app"
fun getInstance(): Retrofit {
return Retrofit.Builder().baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
}