Credential Manager進行google sign

 使用 Android 版 Credential Manager,進行google sign

https://developers.google.com/identity/android-credential-manager


@Composable
fun SignInScreen(viewModel: CredentialSignInViewModel = CredentialSignInViewModel()) {
val uiState by viewModel.uiState.collectAsState()
val context = LocalContext.current

Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Button(
onClick = {
viewModel.signInWithGoogle(
context = context,
webClientId = "" // 替換為你的 Web 用戶端 ID
)
}
) {
Text("Sign in with Google")
}

Spacer(modifier = Modifier.height(16.dp))

when (uiState) {
is CredentialSignInViewModel.UiState.Loading -> {
CircularProgressIndicator()
}

is CredentialSignInViewModel.UiState.Success -> {
val successState = uiState as CredentialSignInViewModel.UiState.Success
Text("Welcome, ${successState.displayName}")
Text("Email: ${successState.email}")
Toast.makeText(context, successState.idToken, Toast.LENGTH_LONG).show()
}

is CredentialSignInViewModel.UiState.Error -> {
val errorState = uiState as CredentialSignInViewModel.UiState.Error
Text("Error: ${errorState.message}")
}

else -> {
Text("Please sign in")
}
}
}
}

留言

這個網誌中的熱門文章

Android - 輸入字串排除 使用InputFilter

EndNote 20使用與APA 7th中英文設定