Skip to content

KawtharE/FirebaseGoogleSignIn

Repository files navigation

FirebaseGoogleSignIn

Android Kotlin mini-project where i applied Clean architecture to implement Firebase Google Sign-In.

Steps to set set Firebase

  1. Connect Android studio to Firebase app in Firebase console: this can be done after selecting Tools -> firebase -> Authentication -> Authenticate using Google

Screenshot 2023-12-27 at 09 51 48

Screenshot 2023-12-27 at 09 52 09

Screenshot 2023-12-27 at 09 52 22

Screenshot 2023-12-27 at 09 53 03
  1. Set the SHA certificate fingerprints key in Firebase console: this key you can get it from signingReport gradle task in the android studio project

    Screenshot 2023-12-27 at 10 02 24

  2. After connection is successful, add the google-services.json file to the project

  3. Add Gradle dependencies:

    implementation("com.google.firebase:firebase-auth-ktx:22.3.0")
    implementation("com.google.android.gms:play-services-auth:20.7.0")
    
  4. Have an instance of SignInClient:

    private val googleSignInClient by lazy {
         Identity.getSignInClient(applicationContext)
     }
    
  5. Prepare the SingIn intent:

    SignInClient.beginSignIn(
             BeginSignInRequest.Builder()
               .setGoogleIdTokenRequestOptions(
                   GoogleIdTokenRequestOptions.builder()
                   .setSupported(true)
                   .setFilterByAuthorizedAccounts(false)
                   .setServerClientId(context.getString(R.string.web_client_id))
                 .build()
               )
               .setAutoSelectEnabled(true)
               .build()
         )
    

    where the web_client_id is set under your firebase project -> Authentication -> Sign-in method -> google -> Web SDK configuration

  6. Once we receive intent as response, we actually signin:

     val credential = SignInClient?.getSignInCredentialFromIntent(intent)
     val googleIdToken = credential?.googleIdToken
     val googleCredentials = GoogleAuthProvider.getCredential(googleIdToken, null)
     ...
     val user = auth.signInWithCredential(googleCredentials).await().user
     ...
    

No user should be connected using google account, bear in mind that at least you should have a connected google account in the device.

About

Android Kotlin module where i applied Clean architecture to implement Firebase Google Sign-In.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages