Password Authentication
Email and password authentication is a widely used method for user sign-in. It allows users to create accounts and log in using their email addresses and corresponding passwords.
Prerequisites
Firebase Setup
Before continuing, ensure you have Firebase set up for your project. If not, follow the instructions in our guide.
Enabling Password Authentication in the Firebase Console
- Navigate to the Firebase console .
- In the sidebar menu, locate the Build section and click on it.
- Within the Build section, find Authentication
- Navigate to the Tab Sign-in method
- Click on Add new provider and select Email/Password
Usage
Dependencies
Within your app
module’s build.gradle.kts
file, incorporate the following dependencies:
implementation(project(":core:firebase"))
implementation(project(":auth:password"))
// Needed to get access to models returned by Firebase like: AuthResult, FirebaseUser, etc...
implementation(libs.firebase.auth)
Example
The rememberPasswordAuth
function, readily available within your composable functions, streamlines email and password authentication management. This function simplifies user signup and sign-in processes.
Simply integrate this composable function with button actions and define callbacks to manage successful authentication (navigation) or error scenarios (error messages).
@Composable
fun PasswordAuthButtons() {
val passwordAuth = rememberPasswordAuth(
callback = object: FirebaseCallback<AuthResult> {
override fun onSuccess(data: AuthResult) {
// Handle Success -> Navigate to Logged in
}
override fun onError(error: String?) {
// Handle Error -> Show error message
}
}
)
Column(
verticalArrangement = Arrangement.spacedBy(16.dp, Alignment.CenterVertically)
) {
Button(
onClick = {
passwordAuth.signUpWithEmailAndPassword(
"example@mail.com",
"Test1234",
)
}
) {
Text(text = "Sign up with Email and Password")
}
Button(
onClick = {
passwordAuth.signInWithEmailAndPassword(
"example@mail.com",
"Test1234",
)
}
) {
Text(text = "Sign in with Email and Password")
}
}
}
More information on Password Authentication can be found in the official documentation .