Firebase Authentication helps you easily add user sign-in to your app. It keeps user accounts safe and lets users log in quickly.
Firebase Authentication in Android Kotlin
val auth = FirebaseAuth.getInstance()
auth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
// User created
} else {
// Handle error
}
}Use FirebaseAuth.getInstance() to get the authentication object.
Use createUserWithEmailAndPassword to register new users.
val auth = FirebaseAuth.getInstance()
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
// User signed in
} else {
// Sign-in failed
}
}val user = FirebaseAuth.getInstance().currentUser if (user != null) { // User is signed in } else { // No user signed in }
FirebaseAuth.getInstance().signOut()
This simple app lets users sign up with email and password. When the sign-up button is clicked, it tries to create a new user. If successful, it shows a success message; otherwise, it shows an error.
import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.google.firebase.auth.FirebaseAuth class MainActivity : AppCompatActivity() { private lateinit var auth: FirebaseAuth override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) auth = FirebaseAuth.getInstance() val emailInput = findViewById<EditText>(R.id.emailInput) val passwordInput = findViewById<EditText>(R.id.passwordInput) val signUpButton = findViewById<Button>(R.id.signUpButton) signUpButton.setOnClickListener { val email = emailInput.text.toString() val password = passwordInput.text.toString() auth.createUserWithEmailAndPassword(email, password) .addOnCompleteListener(this) { task -> if (task.isSuccessful) { Toast.makeText(this, "Sign up successful!", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this, "Sign up failed: ${task.exception?.message}", Toast.LENGTH_SHORT).show() } } } } }
Always validate email and password before sending to Firebase.
Firebase handles password security, so you don't store passwords yourself.
Use addOnCompleteListener to handle success or failure asynchronously.
Firebase Authentication makes user sign-in easy and secure.
Use createUserWithEmailAndPassword to register users.
Check current user with currentUser and sign out with signOut().
Practice
Solution
Step 1: Understand Firebase Authentication role
Firebase Authentication is designed to handle user sign-in and registration securely.Step 2: Differentiate from other Firebase services
It does not store user data (that's Firestore/Realtime Database), nor handle UI design or notifications.Final Answer:
To securely manage user sign-in and registration -> Option DQuick Check:
Firebase Authentication = Secure sign-in [OK]
- Confusing Authentication with database storage
- Thinking it handles UI design
- Assuming it manages notifications
Solution
Step 1: Identify correct Firebase method for registration
The method to register a new user is createUserWithEmailAndPassword(email, password).Step 2: Check method names in options
registerUser and createUser do not exist. signInWithEmailAndPassword is for signing in existing users, not registration.Final Answer:
firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { ... } -> Option CQuick Check:
Register new user = createUserWithEmailAndPassword [OK]
- Using signInWithEmailAndPassword to register
- Using non-existent methods like createUser or registerUser
- Confusing method names
val user = firebaseAuth.currentUser
if (user != null) {
println("User is signed in")
} else {
println("No user signed in")
}Solution
Step 1: Understand currentUser property
firebaseAuth.currentUser returns the current signed-in user or null if none.Step 2: Analyze the if condition
If user is not null, it prints "User is signed in"; otherwise, "No user signed in".Final Answer:
User is signed in -> Option AQuick Check:
currentUser != null means user signed in [OK]
- Assuming currentUser throws error if null
- Confusing sign-in check logic
- Expecting compilation errors incorrectly
firebaseAuth.signOut
println("User signed out")Solution
Step 1: Check signOut method usage
signOut is a method and requires parentheses: signOut()Step 2: Verify other options
signOut exists in FirebaseAuth; println can be outside callback; so only missing parentheses is error.Final Answer:
Missing parentheses after signOut method -> Option BQuick Check:
signOut() needs parentheses [OK]
- Forgetting parentheses on method calls
- Thinking signOut is a property
- Assuming println must be in callback
Solution
Step 1: Understand createUserWithEmailAndPassword behavior
This method registers and automatically signs in the user if successful.Step 2: Analyze options for correct flow
createUserWithEmailAndPassword(...).addOnCompleteListener { task -> if (task.isSuccessful) { println("User registered and signed in") } } registers and confirms success. signInWithEmailAndPassword only signs in existing users. Calling signIn after createUser is redundant. Calling signOut after is incorrect.Final Answer:
firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task -> if (task.isSuccessful) { println("User registered and signed in") } } -> Option AQuick Check:
Registration auto signs in [OK]
- Calling signIn after createUserWithEmailAndPassword unnecessarily
- Using signIn without registering first
- Signing out immediately after registration
Ready to prove it?
Pass the test and this pattern moves unknown
