新增错误处理
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,74 +52,58 @@ 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) {
|
||||
coroutineScope.launch {
|
||||
try {
|
||||
accountService.regiterUserWithGoogleAccount(idToken)
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to register with google", e)
|
||||
return@launch
|
||||
}
|
||||
// 获取用户信息
|
||||
// 获取 token
|
||||
val authResp = accountService.loginUserWithGoogle(idToken)
|
||||
if (authResp.token != null) {
|
||||
coroutineScope.launch(Dispatchers.Main) {
|
||||
Toast.makeText(context, "Successfully registered", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
AppStore.apply {
|
||||
token = authResp.token
|
||||
this.rememberMe = true
|
||||
saveData()
|
||||
}
|
||||
// 获取token 信息
|
||||
try {
|
||||
accountService.getMyAccount()
|
||||
} catch (e: ServiceException) {
|
||||
coroutineScope.launch(Dispatchers.Main) {
|
||||
Toast.makeText(context, "Failed to get account", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
coroutineScope.launch(Dispatchers.Main) {
|
||||
navController.navigate(NavigationRoute.Index.route) {
|
||||
popUpTo(NavigationRoute.Login.route) { inclusive = true }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
try {
|
||||
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(it)
|
||||
if (authResp.token != null) {
|
||||
coroutineScope.launch(Dispatchers.Main) {
|
||||
Toast.makeText(
|
||||
context,
|
||||
"Successfully registered",
|
||||
Toast.LENGTH_SHORT
|
||||
)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
AppStore.apply {
|
||||
token = authResp.token
|
||||
this.rememberMe = true
|
||||
saveData()
|
||||
}
|
||||
// 获取token 信息
|
||||
try {
|
||||
accountService.getMyAccount()
|
||||
} catch (e: ServiceException) {
|
||||
coroutineScope.launch(Dispatchers.Main) {
|
||||
Toast.makeText(context, "Failed to get account", Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
coroutineScope.launch(Dispatchers.Main) {
|
||||
navController.navigate(NavigationRoute.Index.route) {
|
||||
popUpTo(NavigationRoute.Login.route) { inclusive = true }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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,57 +115,36 @@ fun UserAuthScreen() {
|
||||
|
||||
}
|
||||
|
||||
fun onLoginWithGoogle(googleId: String) {
|
||||
scope.launch {
|
||||
try {
|
||||
val authResp = accountService.loginUserWithGoogle(googleId)
|
||||
if (authResp.token != null) {
|
||||
AppStore.apply {
|
||||
token = authResp.token
|
||||
this.rememberMe = rememberMe
|
||||
saveData()
|
||||
}
|
||||
navController.navigate(NavigationRoute.Index.route) {
|
||||
popUpTo(NavigationRoute.Login.route) { inclusive = true }
|
||||
}
|
||||
}
|
||||
} catch (e: ServiceException) {
|
||||
// handle error
|
||||
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)
|
||||
try {
|
||||
GoogleLogin(context) {
|
||||
scope.launch {
|
||||
try {
|
||||
val authResp = accountService.loginUserWithGoogle(it)
|
||||
if (authResp.token != null) {
|
||||
AppStore.apply {
|
||||
token = authResp.token
|
||||
this.rememberMe = rememberMe
|
||||
saveData()
|
||||
}
|
||||
navController.navigate(NavigationRoute.Index.route) {
|
||||
popUpTo(NavigationRoute.Login.route) { inclusive = true }
|
||||
}
|
||||
}
|
||||
} catch (e: ServiceException) {
|
||||
// handle error
|
||||
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Toast.makeText(context, e.message, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
StatusBarMaskLayout {
|
||||
Column(
|
||||
|
||||
37
app/src/main/java/com/aiosman/riderpro/utils/GoogleLogin.kt
Normal file
37
app/src/main/java/com/aiosman/riderpro/utils/GoogleLogin.kt
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user