This is a part of the Firebase instructional sequence:
On this demo app, we can information you in the course of the steps to allow FirebaseUI for authentication and exhibit how one can combine it into your Jetpack Compose utility.
We are going to upload those 2 sign-in strategies:
Attach Your App to Firebase
-
Pass to the Firebase console, and log in along with your Google account.
-
Upload task and practice on-screen directions
-
Flip off Allow Google Analytics for this task, and click on Create task.
-
Sign in the app with Android bundle identify (applicationId on your construct.gradle report)
-
Upload app nickname (it may be any identify)
-
Upload Debug signing certificates SHA-1 if you wish to use Google sign-in approach. Within the Android Studio terminal, run this command ./gradlew signingReport. Reproduction the SHA-1 and paste it right here.
-
-
Obtain google-services.json on your task app folder.
-
Pass to Authentication -> Signal-in approach. Upload and allow Electronic mail/Password, and Google sign-in suppliers.
The google-services.json and debug making a song certificates fingerprints will also be accessed from the Venture Evaluate -> Venture settings on the most sensible left nook.
Upload Firebase Authentication SDK on your app
-
Upload Google facilities Gradle plugin on your project-level construct.gradle.kts.
buildscript { dependencies { classpath("com.google.gms:google-services:4.3.15") } }
-
Upload Google carrier Gradle plugin on your app-level construct.gradle.kts/
plugins { identification ("com.google.gms.google-services") }
-
Upload FirebaseUI and Play Services and products Auth libraries
dependencies { implementation ("com.firebaseui:firebase-ui-auth:7.2.0") implementation ("com.google.android.gms:play-services-auth:20.5.0") }
The Play Services and products Auth library is needed for the Electronic mail/Password sign-in approach.
In Android Studio, all of the above steps may also be automatic with the Firebase Assistant (Gear -> Firebase) apart from that you want to manually upload the Debug signing certificates SHA-1 and likewise manually regulate the library dependencies.
Combine pre-build Firebase Signal-in UI
Fast steps on what you want to do to release the pre-build Signal-in UI:
-
Create ActivityResultLauncher
-
Select authentication suppliers
-
Create and release sign-in intent. This is an instance of
SignInScreen()
composable purposes:@Composable a laugh SignInScreen( onSignInResult: (FirebaseAuthUIAuthenticationResult) -> Unit ) { val launcher = rememberLauncherForActivityResult( contract = FirebaseAuthUIActivityResultContract(), onResult = onSignInResult ) val suppliers = arrayListOf( AuthUI.IdpConfig.GoogleBuilder().construct(), AuthUI.IdpConfig.EmailBuilder().construct(), ) val intent = AuthUI.getInstance() .createSignInIntentBuilder() .setAvailableProviders(suppliers) .construct() LaunchedEffect(true){ launcher.release(intent) } }
-
Care for the sign-in end result callback
SignInScreen { end result -> if (end result.resultCode == ComponentActivity.RESULT_OK) { signInStatus = "Signed In" val person = FirebaseAuth.getInstance().currentUser userId = person!!.uid } else { val reaction = end result.idpResponse signInStatus = if (reaction == null) { "No longer Signed In" } else { val errorCode = reaction.getError()?.getErrorCode() "Failed - Error Code: $errorCode" } } }
When the
reaction
is null, the person cancels the sign-in operation (e.g. press the again button)
Some Ideas
The Firebase Assistant in Android Studio is fast and useful, however if you are new, I like to recommend you cross in the course of the Firebase console task setup steps above to totally perceive the true steps that want to be performed.
Subsequent Step
Smartly, that is only a fast demo app to exhibit the Firebase sign-in APIs. I almost definitely will have to transfer the sign-in state to a ViewModel once I port this variation to my exact app.
[Updated – Aug 08, 2023]: Yeah, I’ve moved the sign-in state to the VIewModel if you happen to take a look at the supply code.
I additionally need to discover ways to retailer the person information within the faraway server the use of Firebase real-time database as an alternative of the use of Personal tastes and Proto DataStore at the native telephone.
Supply Code
GitHub Repository: Demo_FirebaseSignInRealtimeDB