0
    @Composable
fun loginButton(loginType: Int, context: Context, googleSignInClient: GoogleSignInClient?) {
    Column(horizontalAlignment = Alignment.CenterHorizontally, modifier = Modifier.fillMaxWidth()) {
        if (loginType == 0) {
            OutlinedButton(onClick = {
                googleSignIn(context = context, googleSignInClient = googleSignInClient)
            }, border = BorderStroke(2.dp, Color.Black), colors = ButtonDefaults.buttonColors(backgroundColor = Color(0xFFFFFFFF)), modifier = Modifier
                .height(40.dp)
                .width(300.dp), shape = RoundedCornerShape(50)
            ) {
                Image(painter = painterResource(id = R.drawable.google_logo), contentDescription = "Google Logo(Src: Wikipedia)")
                Spacer(modifier = Modifier.width(30.dp))
                Text(text = "Sign in with Google", fontSize = 15.sp, color = Color.Black)
            }
        } else if (loginType == 1) {
            OutlinedButton(onClick = {

            }, border = BorderStroke(2.dp, Color.Black), colors = ButtonDefaults.buttonColors(backgroundColor = Color(0xFFFFFFFF)), modifier = Modifier
                .height(40.dp)
                .width(300.dp), shape = RoundedCornerShape(50)
            ) {
                Image(painter = painterResource(id = R.drawable.apple_logo), contentDescription = "Google Logo(Src: Wikipedia)")
                Spacer(modifier = Modifier.width(30.dp))
                Text(text = "Sign in with Apple", fontSize = 15.sp, color = Color.Black)
            }
        } else {
            Text("Cannot load the sign in button")
        }
    }
}

You can see googleSignIn() from the 6th line which caused that error. That is the function in other Kotlin file. The code for that function is under here.

@Composable
fun googleSignIn(context: Context, googleSignInClient: GoogleSignInClient?) {
    val signInLauncher = rememberLauncherForActivityResult(contract = ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
        if (result.resultCode == Activity.RESULT_OK) {
            val intent = result.data
            if (result.data != null) {
                val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(intent)
                try {
                    val account = task.getResult(ApiException::class.java)
                    Log.d("AuthManager", "FirebaseAuthWithGoogle" + account.id)
                } catch (e: ApiException) {
                    e.printStackTrace();
                    Log.w("LoginActivity", "Google sign in failed", e);
                }
            }
        }
    }

    signInLauncher.launch(googleSignInClient?.signInIntent)
}

I don't know what is the problem. First, I want googleSignIn() at the 6th line of loginButton to work with 'context' at the parameter of loginButton. Second, I want to get this error fixed.

Jalim
  • 45
  • 6
  • 1
    You googleSignIn method should not be composable. – Francesc Jul 23 '22 at 00:52
  • Calling `rememberLauncherForActivityResult` needs to be done ahead of time, not as part of the `onClick`. – ianhanniballake Jul 23 '22 at 01:18
  • I think that this [resource](https://medium.com/firebase-developers/how-to-authenticate-to-firebase-using-google-one-tap-in-jetpack-compose-60b30e621d0d) might help. Here is the corresponding [repo](https://github.com/alexmamo/FirebaseSignInWithGoogle). – Alex Mamo Jul 23 '22 at 07:06

0 Answers0