在第一个条目中 Firebase auth: Google sign in pop up window doesn't show up 使用新设备模拟器时
我删除并重新安装应用程序,错误没有显示在任何地方
应用模块
@Provides
fun provideGoogleSignInOptions(
app: Application
) = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(app.getString(R.string.web_client_id))
.requestEmail()
.build()
@Provides
@Named(SIGN_UP_REQUEST)
fun provideSignUpRequest(
app: Application
) = BeginSignInRequest.builder()
.setGoogleIdTokenRequestOptions(
BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
.setSupported(true)
.setServerClientId(app.getString(R.string.web_client_id))
.setFilterByAuthorizedAccounts(false)
.build())
.build()
@Provides
@Named(SIGN_IN_REQUEST)
fun provideSignInRequest(
app: Application
) = BeginSignInRequest.builder()
.setGoogleIdTokenRequestOptions(
BeginSignInRequest.GoogleIdTokenRequestOptions.builder()
.setSupported(true)
.setServerClientId(app.getString(R.string.web_client_id))
.setFilterByAuthorizedAccounts(true)
.build())
.setAutoSelectEnabled(true)
.build()
视图模型
var oneTapSignInResponse by mutableStateOf<OneTapSignInResponse>(Response.Success(null))
private set
var signInWithGoogleResponse by mutableStateOf<SignInWithGoogleResponse> (Response.Success(false))
private set
fun oneTapSignIn() = viewModelScope.launch {
oneTapSignInResponse = Response.Loading
oneTapSignInResponse = repo.oneTapSignInWithGoogle()
}
fun signInWithGoogle(googleCredential: AuthCredential) = viewModelScope.launch {
signInWithGoogleResponse = Response.Loading
signInWithGoogleResponse = repo.firebaseSignInWithGoogle(googleCredential)
}
AuthRepositoryImpl
override suspend fun oneTapSignInWithGoogle(): OneTapSignInResponse {
return try {
val signInResult = oneTapClient.beginSignIn(signInRequest).await()
Response.Success(signInResult)
} catch (e: Exception) {
try {
val signUpResult = oneTapClient.beginSignIn(signUpRequest).await()
Response.Success(signUpResult)
} catch (e: Exception) {
Response.Failure(e)
}
}
}
override suspend fun firebaseSignInWithGoogle(
googleCredential: AuthCredential
): SignInWithGoogleResponse {
return try {
val authResult = auth.signInWithCredential(googleCredential).await()
val isNewUser = authResult.additionalUserInfo?.isNewUser ?: false
if (isNewUser) {
createUserInFirestore()
}
Response.Success(true)
} catch (e: Exception) {
Response.Failure(e)
}
}
AuthScreen
Scaffold(
topBar = {
AuthTopBar()
},
content = { padding ->
AuthContent(
padding = padding,
oneTapSignIn = {
viewModel.oneTapSignIn()
}
)
}
)
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartIntentSenderForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
try {
val credentials = viewModel.oneTapClient.getSignInCredentialFromIntent(result.data)
val googleIdToken = credentials.googleIdToken
val googleCredentials = getCredential(googleIdToken, null)
viewModel.signInWithGoogle(googleCredentials)
} catch (it: ApiException) {
print(it)
}
}
}
fun launch(signInResult: BeginSignInResult) {
val intent = IntentSenderRequest.Builder(signInResult.pendingIntent.intentSender).build()
launcher.launch(intent)
}
OneTapSignIn {
launch(it)
}
SignInWithGoogle { signedIn ->
if (signedIn) {
navigateToHomeScreen()
}
}