FireBase Google Login

FireBase Google Login

 
 
 
 

Add the Google Sign-in button to your app

  1. Add the SignInButton in your application's layout:
    1. <com.google.android.gms.common.SignInButton  android:id="@+id/sign_in_button"  android:layout_width="wrap_content"  android:layout_height="wrap_content" />
      Note: - You can also create your own view / layout and follow process below on that view / layout.
  1. Optional: If you are using the default sign-in button graphic instead of providing your own sign-in button assets, you can customize the button's size with the setSize method.
    1. lateinit var signInButton : Button // Set the dimensions of the sign-in button. signInButton = findViewById(R.id.sign_in_button) signInButton.setSize(SignInButton.SIZE_STANDARD) //binding.signInButton.setSize(SignInButton.SIZE_STANDARD) //binding.signInButton.setSize(SignInButton.SIZE_WIDE) //binding.signInButton.setSize(SignInButton.SIZE_ICON_ONLY)
  1. In the Android activity (for example, in the onCreate method), register your button's OnClickListener to sign in the user when clicked:
binding.signInButton.setOnClickListener { signInWithGoogle() }
 
 
 
  1. Implement Firebase in your App And add SHA-1 key in firebase project
FireBase Implementation
FireBase Implementation
  1. from there download newly updated google-services.json file and and to your source folder in project view from android studio
  1. Add Dependency
dependencies { // Import the BoM for the Firebase platform     implementation(platform("com.google.firebase:firebase-bom:32.7.1")) // Add the dependency for the Firebase Authentication library     // When using the BoM, you don't specify versions in Firebase library dependencies implementation("com.google.firebase:firebase-auth")  // Also add the dependency for the Google Play services library and specify its version     implementation("com.google.android.gms:play-services-auth:20.7.0") }
  • Sync your project and all gradle files
 
  1. In your sign-in activity's onCreate method, get the shared instance of the FirebaseAuth object:
private lateinit var auth: FirebaseAuth // ... // Initialize Firebase Auth auth = Firebase.auth
 
  1. Code to login with google account

How to get requestIdToken

  • Open google-services.json file in there get second client id value from page start
// Google Sign in Client // Outside and above onCreate(_) val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken("163183586333-uivj0s3qggjvhcjtdthlajlig0arehkq.apps.googleusercontent.com") .requestEmail() .build() lateinit var googleSignInClient : GoogleSignInClient // Inside OnCreate() Intialise this googleSignInClient = GoogleSignIn.getClient(this@SignInActivity, gso) binding.googleSignIn.setOnClickListener { signInWithGoogle() } // Add belove code outside the on create method private fun signInWithGoogle() { val signInIntent = googleSignInClient.signInIntent googleActivityResult.launch(signInIntent) } val googleActivityResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> val task = GoogleSignIn.getSignedInAccountFromIntent(result.data) try { // Google Sign In was successful, authenticate with Firebase val account = task.getResult(ApiException::class.java)!! Log.d(TAG, "firebaseAuthWithGoogle:" + account.id) firebaseAuthWithGoogle(account.idToken!!) } catch (e: ApiException) { // Google Sign In failed, update UI appropriately Log.w(TAG, "Google sign in failed", e) } } private fun firebaseAuthWithGoogle(idToken: String) { val credential = GoogleAuthProvider.getCredential(idToken, null) auth.signInWithCredential(credential) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithCredential:success") // Go to Main Page if Login Success val user = auth.currentUser } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithCredential:failure", task.exception) } } }
 
 
 
class GoogleSignInActivity : AppCompatActivity() { private val binding: ActivityGoogleSignInBinding by lazy { ActivityGoogleSignInBinding.inflate(layoutInflater) } private val googleSignInViewModel: GoogleSignInViewModel by viewModels() lateinit var googleSignInClient: GoogleSignInClient override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(binding.root) binding.signInButton.setSize(SignInButton.SIZE_ICON_ONLY) googleSignInClient = GoogleSignIn.getClient(this@GoogleSignInActivity, googleSignInViewModel.gso) binding.btnLogout.setOnClickListener { if (googleSignInViewModel.auth.currentUser != null) { googleSignInViewModel.auth.signOut() } else { toast("Login Or Sign In First") } } binding.signInButton.setOnClickListener { signInWithGoogle() } googleSignInViewModel.onStateChanged = object : OnStateChanged { override fun onCodeSent() {} override fun onLoginSuccess(message: String) { toast(message) } } } private fun signInWithGoogle() { val signInIntent = googleSignInClient.signInIntent googleActivityResult.launch(signInIntent) } private val googleActivityResult = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> val task = GoogleSignIn.getSignedInAccountFromIntent(result.data) try { // Google Sign In was successful, authenticate with Firebase val account = task.getResult(ApiException::class.java)!! Log.d("=+=+=+", "firebaseAuthWithGoogle:" + account.id) googleSignInViewModel.firebaseAuthWithGoogle( activity = this@GoogleSignInActivity, idToken = account.idToken!! ) } catch (e: ApiException) { // Google Sign In failed, update UI appropriately Log.w("=+=+=+", "Google sign in failed", e) } } }