wire up google auth for android
This commit is contained in:
@ -121,6 +121,8 @@ dependencies {
|
||||
|
||||
implementation 'androidx.compose.material3:material3:1.0.0-alpha16'
|
||||
implementation 'androidx.compose.material3:material3-window-size-class:1.0.0-alpha16'
|
||||
|
||||
implementation 'com.google.android.gms:play-services-auth:20.2.0'
|
||||
}
|
||||
|
||||
apollo {
|
||||
|
||||
@ -1,9 +1,14 @@
|
||||
package app.omnivore.omnivore
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.util.Log
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.*
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
|
||||
import com.google.android.gms.common.api.ApiException
|
||||
import com.google.android.gms.tasks.Task
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.launch
|
||||
@ -65,4 +70,11 @@ class LoginViewModel @Inject constructor(
|
||||
fun resetErrorMessage() {
|
||||
errorMessage = null
|
||||
}
|
||||
|
||||
fun handleGoogleAuthTask(task: Task<GoogleSignInAccount>) {
|
||||
val googleIdToken = task?.getResult(ApiException::class.java).idToken
|
||||
Log.d(ContentValues.TAG, "Google Result?: $googleIdToken")
|
||||
// TODO: submit id token to backend
|
||||
// If token is missing then set the error message
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,11 +1,16 @@
|
||||
package app.omnivore.omnivore
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.result.ActivityResult
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.activity.viewModels
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
@ -27,6 +32,13 @@ import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.core.view.ViewCompat
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.core.view.WindowInsetsCompat
|
||||
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.GoogleSignInClient
|
||||
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
|
||||
import com.google.android.gms.common.GoogleApiAvailability
|
||||
import com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable
|
||||
import com.google.android.gms.tasks.Task
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@ -41,7 +53,7 @@ class MainActivity : ComponentActivity() {
|
||||
OmnivoreTheme {
|
||||
// A surface container using the 'background' color from the theme
|
||||
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) {
|
||||
PrimaryView(viewModel)
|
||||
WelcomeView(viewModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -58,7 +70,7 @@ class MainActivity : ComponentActivity() {
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun PrimaryView(viewModel: LoginViewModel) {
|
||||
fun WelcomeView(viewModel: LoginViewModel) {
|
||||
val hasAuthToken: Boolean by viewModel.hasAuthTokenLiveData.observeAsState(false)
|
||||
|
||||
if (hasAuthToken) {
|
||||
@ -68,6 +80,26 @@ fun PrimaryView(viewModel: LoginViewModel) {
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun LoginView(viewModel: LoginViewModel) {
|
||||
val isGoogleAuthAvailable: Boolean = GoogleApiAvailability
|
||||
.getInstance()
|
||||
.isGooglePlayServicesAvailable(LocalContext.current) == 0
|
||||
|
||||
Column(
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
modifier = Modifier
|
||||
.background(MaterialTheme.colorScheme.background)
|
||||
.fillMaxSize()
|
||||
) {
|
||||
if (isGoogleAuthAvailable) {
|
||||
GoogleAuthButton(viewModel)
|
||||
}
|
||||
EmailLoginView(viewModel)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun LoggedInView(viewModel: LoginViewModel) {
|
||||
Column(
|
||||
@ -89,7 +121,7 @@ fun LoggedInView(viewModel: LoginViewModel) {
|
||||
|
||||
@SuppressLint("CoroutineCreationDuringComposition")
|
||||
@Composable
|
||||
fun LoginView(viewModel: LoginViewModel) {
|
||||
fun EmailLoginView(viewModel: LoginViewModel) {
|
||||
var email by rememberSaveable { mutableStateOf("") }
|
||||
var password by rememberSaveable { mutableStateOf("") }
|
||||
val focusManager = LocalFocusManager.current
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
<resources>
|
||||
<string name="app_name">Omnivore</string>
|
||||
</resources>
|
||||
<string name="gcp_id">590528454690-ovh9ku5r3fojgr1pp5kgaimr43o96btb.apps.googleusercontent.com</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user