mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
use retrofir to make rest api call for email login
This commit is contained in:
parent
0f13d234e4
commit
65a420ad8c
4 changed files with 62 additions and 1 deletions
|
|
@ -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'
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue