新增错误处理

This commit is contained in:
2024-08-23 21:03:43 +08:00
parent 2dd2ee0281
commit 07f825c783
4 changed files with 130 additions and 120 deletions

View File

@@ -72,7 +72,6 @@ fun LoginPage() {
modifier = Modifier
.align(Alignment.BottomCenter)
.padding(bottom = 82.dp + 162.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
@@ -87,9 +86,6 @@ fun LoginPage() {
) {
navController.navigate(
NavigationRoute.UserAuth.route,
navOptions {
popUpTo(navController.currentDestination?.id ?: 0) { inclusive = true }
}
)
}
ActionButton(
@@ -101,9 +97,6 @@ fun LoginPage() {
){
navController.navigate(
NavigationRoute.SignUp.route,
navOptions {
popUpTo(navController.currentDestination?.id ?: 0) { inclusive = true }
}
)
}
}

View File

@@ -42,9 +42,7 @@ import com.aiosman.riderpro.data.AccountServiceImpl
import com.aiosman.riderpro.data.ServiceException
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException
import com.aiosman.riderpro.utils.GoogleLogin
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@@ -54,23 +52,30 @@ fun SignupScreen() {
val navController = LocalNavController.current
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()
val credentialManager = CredentialManager.create(context)
val accountService: AccountService = AccountServiceImpl()
fun registerWithGoogle(idToken: String) {
fun googleLogin() {
coroutineScope.launch {
try {
accountService.regiterUserWithGoogleAccount(idToken)
GoogleLogin(context) {
coroutineScope.launch {
try {
accountService.regiterUserWithGoogleAccount(it)
} catch (e: Exception) {
Log.e(TAG, "Failed to register with google", e)
return@launch
}
// 获取用户信息
// 获取 token
val authResp = accountService.loginUserWithGoogle(idToken)
val authResp = accountService.loginUserWithGoogle(it)
if (authResp.token != null) {
coroutineScope.launch(Dispatchers.Main) {
Toast.makeText(context, "Successfully registered", Toast.LENGTH_SHORT).show()
Toast.makeText(
context,
"Successfully registered",
Toast.LENGTH_SHORT
)
.show()
}
}
AppStore.apply {
@@ -83,7 +88,8 @@ fun SignupScreen() {
accountService.getMyAccount()
} catch (e: ServiceException) {
coroutineScope.launch(Dispatchers.Main) {
Toast.makeText(context, "Failed to get account", Toast.LENGTH_SHORT).show()
Toast.makeText(context, "Failed to get account", Toast.LENGTH_SHORT)
.show()
}
}
coroutineScope.launch(Dispatchers.Main) {
@@ -92,36 +98,12 @@ fun SignupScreen() {
}
}
}
}
} catch (e: Exception) {
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
}
fun handleGoogleSignIn(result: GetCredentialResponse) {
val credential: Credential = result.credential
if (credential is CustomCredential) {
if (GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(credential.type)) {
try {
val googleIdTokenCredential: GoogleIdTokenCredential =
GoogleIdTokenCredential.createFrom(credential.data)
registerWithGoogle(googleIdTokenCredential.idToken)
} catch (e: GoogleIdTokenParsingException) {
Log.e(TAG, "Received an invalid google id token response", e);
}
}
}
}
fun googleLogin() {
val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(true)
.setServerClientId("754277015802-pnua6tg8ibnjq69lv8qdcmsdhbe97ag9.apps.googleusercontent.com")
.build()
val request = GetCredentialRequest.Builder().addCredentialOption(googleIdOption)
.build()
coroutineScope.launch {
credentialManager.getCredential(context, request).let {
// Use the credential
handleGoogleSignIn(it)
}
}
}

View File

@@ -21,6 +21,8 @@ import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CheckboxDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
@@ -45,6 +47,8 @@ import androidx.credentials.CredentialManager
import androidx.credentials.CustomCredential
import androidx.credentials.GetCredentialRequest
import androidx.credentials.GetCredentialResponse
import androidx.credentials.exceptions.GetCredentialProviderConfigurationException
import androidx.credentials.exceptions.NoCredentialException
import com.aiosman.riderpro.AppStore
import com.aiosman.riderpro.LocalNavController
import com.aiosman.riderpro.R
@@ -55,9 +59,11 @@ import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.comment.NoticeScreenHeader
import com.aiosman.riderpro.ui.composables.StatusBarMaskLayout
import com.aiosman.riderpro.ui.modifiers.noRippleClickable
import com.aiosman.riderpro.utils.GoogleLogin
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@@ -72,9 +78,22 @@ fun UserAuthScreen() {
val navController = LocalNavController.current
val context = LocalContext.current
val credentialManager = CredentialManager.create(context)
fun validateForm(): Boolean {
if (email.isEmpty()) {
Toast.makeText(context, "Email is required", Toast.LENGTH_SHORT).show()
return false
}
if (password.isEmpty()) {
Toast.makeText(context, "Password is required", Toast.LENGTH_SHORT).show()
return false
}
return true
}
fun onLogin() {
if (!validateForm()) {
return
}
scope.launch {
try {
val authResp = accountService.loginUserWithPassword(email, password)
@@ -96,10 +115,14 @@ fun UserAuthScreen() {
}
fun onLoginWithGoogle(googleId: String) {
fun googleLogin() {
scope.launch {
try {
val authResp = accountService.loginUserWithGoogle(googleId)
GoogleLogin(context) {
scope.launch {
try {
val authResp = accountService.loginUserWithGoogle(it)
if (authResp.token != null) {
AppStore.apply {
token = authResp.token
@@ -115,38 +138,13 @@ fun UserAuthScreen() {
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
}
}
}
} catch (e: Exception) {
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
}
}
fun handleGoogleSignIn(result: GetCredentialResponse) {
val credential: Credential = result.credential
if (credential is CustomCredential) {
if (GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(credential.type)) {
try {
val googleIdTokenCredential: GoogleIdTokenCredential =
GoogleIdTokenCredential.createFrom(credential.data)
onLoginWithGoogle(googleIdTokenCredential.idToken)
} catch (e: GoogleIdTokenParsingException) {
Log.e(TAG, "Received an invalid google id token response", e);
}
}
}
}
fun googleLogin() {
val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(true)
.setServerClientId("754277015802-pnua6tg8ibnjq69lv8qdcmsdhbe97ag9.apps.googleusercontent.com")
.build()
val request = GetCredentialRequest.Builder().addCredentialOption(googleIdOption)
.build()
scope.launch {
credentialManager.getCredential(context, request).let {
// Use the credential
handleGoogleSignIn(it)
}
}
}
StatusBarMaskLayout {
Column(

View File

@@ -0,0 +1,37 @@
package com.aiosman.riderpro.utils
import android.content.Context
import androidx.credentials.Credential
import androidx.credentials.CredentialManager
import androidx.credentials.CustomCredential
import androidx.credentials.GetCredentialRequest
import androidx.credentials.GetCredentialResponse
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
fun handleGoogleSignIn(result: GetCredentialResponse, onLoginWithGoogle: (String) -> Unit) {
val credential: Credential = result.credential
if (credential is CustomCredential) {
if (GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL.equals(credential.type)) {
val googleIdTokenCredential: GoogleIdTokenCredential =
GoogleIdTokenCredential.createFrom(credential.data)
onLoginWithGoogle(googleIdTokenCredential.idToken)
}
}
}
suspend fun GoogleLogin(context: Context, onLoginWithGoogle: (String) -> Unit) {
val credentialManager = CredentialManager.create(context)
val googleIdOption: GetGoogleIdOption = GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(true)
.setServerClientId("754277015802-pnua6tg8ibnjq69lv8qdcmsdhbe97ag9.apps.googleusercontent.com")
.build()
val request = GetCredentialRequest.Builder().addCredentialOption(googleIdOption)
.build()
credentialManager.getCredential(context, request).let {
handleGoogleSignIn(it, onLoginWithGoogle)
}
}