mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
use silnt auth for google
This commit is contained in:
parent
758ec4a237
commit
4af03846e0
3 changed files with 32 additions and 7 deletions
|
|
@ -1,10 +1,11 @@
|
|||
package app.omnivore.omnivore.ui.auth
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.ContentValues
|
||||
import android.util.Log
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.ActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.material.ExperimentalMaterialApi
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.res.painterResource
|
||||
|
|
@ -13,6 +14,8 @@ import app.omnivore.omnivore.R
|
|||
import com.google.android.gms.auth.api.signin.GoogleSignIn
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
|
||||
import com.google.android.gms.common.api.ApiException
|
||||
import com.google.android.gms.tasks.OnCompleteListener
|
||||
import com.google.android.gms.tasks.Task
|
||||
|
||||
@Composable
|
||||
|
|
@ -20,10 +23,8 @@ fun GoogleAuthButton(viewModel: LoginViewModel) {
|
|||
val context = LocalContext.current
|
||||
|
||||
val signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
|
||||
.requestEmail()
|
||||
.requestIdToken(stringResource(R.string.gcp_id))
|
||||
.requestId()
|
||||
.requestProfile()
|
||||
// .requestServerAuthCode(stringResource(R.string.gcp_id), true)
|
||||
.build()
|
||||
|
||||
val startForResult =
|
||||
|
|
@ -39,6 +40,8 @@ fun GoogleAuthButton(viewModel: LoginViewModel) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
LoadingButtonWithIcon(
|
||||
text = "Continue with Google",
|
||||
loadingText = "Signing in...",
|
||||
|
|
@ -46,7 +49,21 @@ fun GoogleAuthButton(viewModel: LoginViewModel) {
|
|||
icon = painterResource(id = R.drawable.ic_logo_google),
|
||||
onClick = {
|
||||
val googleSignIn = GoogleSignIn.getClient(context, signInOptions)
|
||||
startForResult.launch(googleSignIn.signInIntent)
|
||||
val silentSignInTask = googleSignIn.silentSignIn()
|
||||
Log.d(ContentValues.TAG, "silent auth success?: ${silentSignInTask.isSuccessful}")
|
||||
val result = silentSignInTask.getResult(ApiException::class.java)
|
||||
Log.d(ContentValues.TAG, "silent id token?: ${result.idToken}")
|
||||
|
||||
|
||||
|
||||
if (silentSignInTask.isSuccessful) {
|
||||
silentSignInTask.addOnCompleteListener(OnCompleteListener<GoogleSignInAccount?> { task ->
|
||||
viewModel.handleGoogleAuthTask(task)
|
||||
})
|
||||
} else {
|
||||
// googleSignIn.signOut() // TODO: Should use this on logout instead
|
||||
startForResult.launch(googleSignIn.signInIntent)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,17 @@ import androidx.compose.runtime.mutableStateOf
|
|||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.*
|
||||
import app.omnivore.omnivore.*
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignIn
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
|
||||
import com.google.android.gms.common.api.ApiException
|
||||
import com.google.android.gms.tasks.OnCompleteListener
|
||||
import com.google.android.gms.tasks.Task
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
|
||||
enum class RegistrationState {
|
||||
AuthProviderButtons,
|
||||
EmailSignIn
|
||||
|
|
@ -82,8 +85,12 @@ class LoginViewModel @Inject constructor(
|
|||
}
|
||||
|
||||
fun handleGoogleAuthTask(task: Task<GoogleSignInAccount>) {
|
||||
val googleIdToken = task?.getResult(ApiException::class.java).idToken
|
||||
Log.d(ContentValues.TAG, "Google Result?: $googleIdToken")
|
||||
val result = task?.getResult(ApiException::class.java)
|
||||
Log.d(ContentValues.TAG, "server auth code?: ${result.serverAuthCode}")
|
||||
Log.d(ContentValues.TAG, "is Expired?: ${result.isExpired}")
|
||||
Log.d(ContentValues.TAG, "granted Scopes?: ${result.grantedScopes}")
|
||||
val googleIdToken = result.idToken
|
||||
Log.d(ContentValues.TAG, "Google id token?: $googleIdToken")
|
||||
// TODO: submit id token to backend
|
||||
// If token is missing then set the error message
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
<resources>
|
||||
<string name="app_name">Omnivore</string>
|
||||
<string name="gcp_id">590528454690-mpmbvcb4c3ifmnojmrultt37eo8f64at.apps.googleusercontent.com</string>
|
||||
<!-- <string name="web_client_id">590528454690-g535h6sd708andjrvrmqgaj1sergtqi9.apps.googleusercontent.com</string>-->
|
||||
<string name="welcome_title">Never miss a great read</string>
|
||||
<string name="learn_more">Learn More</string>
|
||||
<string name="welcome_subtitle">Save articles and read them later in our distraction-free reader.</string>
|
||||
|
|
|
|||
Loading…
Reference in a new issue