Home > database >  Firebase - google sign in activity, getting these errors
Firebase - google sign in activity, getting these errors

Time:10-18

I am creating a google sign-in activity using firebase but getting errors in these lines plz help.

this is the sign in activity code

class SignInActivity : AppCompatActivity() {


lateinit var binding: ActivitySignInBinding

private lateinit var auth: FirebaseAuth

private lateinit var googleSignInClient: GoogleSignInClient


private val RC_SIGN_IN: Int = 123

private val TAG = "SignInActivity Tag"


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivitySignInBinding.inflate(layoutInflater)
    setContentView(binding.root)


    // Configure Google Sign In
    val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestEmail()
        .build()

    googleSignInClient = GoogleSignIn.getClient(this, gso)

    auth = FirebaseAuth.getInstance()

    binding.signInButton.setOnClickListener {
        signIn()
    }

}

// onStart is called after onCreate , and this is created so that // if a user is already signed in, he will directly lend to main activity, if not then // user will remain in sign in activity

override fun onStart() {
    super.onStart()
    val currentUser = auth.currentUser
    updateUI(currentUser)
}


private fun signIn() {
    val signInIntent = googleSignInClient.signInIntent
    startActivityForResult(signInIntent, RC_SIGN_IN)
}



override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == RC_SIGN_IN) {
        val task = GoogleSignIn.getSignedInAccountFromIntent(data)
        handleSignInResult(task)
    }
}

private fun handleSignInResult(completedTask: Task<GoogleSignInAccount>) {
    try {
        val account =
            completedTask.getResult(ApiException::class.java)!!
        Log.d(TAG, "firebaseAuthWithGoogle:"   account.id)
        firebaseAuthWithGoogle(account.idToken!!)
    } catch (e: ApiException) {
        Log.w(TAG, "signInResult:failed code="   e.statusCode)

    }
}

private fun firebaseAuthWithGoogle(idToken: String) {
    val credential = GoogleAuthProvider.getCredential(idToken, null)
    binding.signInButton.visibility = View.GONE
    binding.progressBar.visibility = View.VISIBLE
 /* coroutines */   GlobalScope.launch(Dispatchers.IO) {
        val auth = auth.signInWithCredential(credential).await()
        val firebaseUser = auth.user
        withContext(Dispatchers.Main) {
            updateUI(firebaseUser)
        }
    }

}



private fun updateUI(firebaseUser: FirebaseUser?) {
    if(firebaseUser != null) {

        val user = User(firebaseUser.uid, firebaseUser.displayName, firebaseUser.photoUrl.toString())
        val usersDao = UserDao()
        usersDao.addUser(user)




        val mainActivityIntent = Intent(this, MainActivity::class.java)
        startActivity(mainActivityIntent)
        finish()
    } else {
        binding.signInButton.visibility = View.VISIBLE
        binding.progressBar.visibility = View.GONE
    }
}

}

and in these lines, I am getting 4 errors

see these errors

this is the error 1 reason

this is error 2 reason

error 3 reason

error 4 reason

this is my user class code

data class User ( val uid : String = "" ,
                  val displayName : String? = "",
                  val imageUrl : String = "" )

and this is userdao class code

class UserDao {

private val db = FirebaseFirestore.getInstance()
private val usersCollection = db.collection("users")

fun addUser(user: User?) {

    user?.let {

        /* global scope is a coroutine use for doing process in background thread*/

        GlobalScope.launch(Dispatchers.IO) {
        usersCollection.document(user.uid).set(it)
    }


    }

}

CodePudding user response:

You import wrong Data class that why you see error. Goto top and change importer remove com.google.firebase.firestore.auth.user and add your User Data class

  • Related