Firebase Authentication helps you easily add user sign-in to your app. It keeps user accounts safe and lets users log in quickly.
When you want users to create accounts and log in to save their data.
When you want to let users sign in with email and password or social accounts like Google.
When you want to protect parts of your app so only signed-in users can access them.
When you want to manage user sessions without building your own backend.
When you want to quickly add secure authentication without much code.
Syntax
iOS Swift
import FirebaseAuth
// To sign up a user with email and password
Auth.auth().createUser(withEmail: "email@example.com", password: "password123") { authResult, error inif let error = error {
print("Error: \(error.localizedDescription)")
return
}
print("User signed up: \(authResult?.user.email ?? "No Email")")
}
// To sign in a user
Auth.auth().signIn(withEmail: "email@example.com", password: "password123") { authResult, error inif let error = error {
print("Error: \(error.localizedDescription)")
return
}
print("User signed in: \(authResult?.user.email ?? "No Email")")
}
Use createUser to register new users.
Use signIn to log in existing users.
Examples
Registers a new user with email and password.
iOS Swift
Auth.auth().createUser(withEmail: "user@example.com", password: "mypassword") { authResult, error inif let error = error {
print("Sign up failed: \(error.localizedDescription)")
} else {
print("Signed up: \(authResult?.user.email ?? "No Email")")
}
}
Logs in an existing user with email and password.
iOS Swift
Auth.auth().signIn(withEmail: "user@example.com", password: "mypassword") { authResult, error inif let error = error {
print("Sign in failed: \(error.localizedDescription)")
} else {
print("Signed in: \(authResult?.user.email ?? "No Email")")
}
}
Sample App
This SwiftUI app lets users enter email and password. They can tap Sign Up to create an account or Sign In to log in. The app shows messages about success or errors below the buttons.
Always handle errors to inform users what went wrong.
Use secure password rules to keep accounts safe.
Remember to initialize Firebase in your app before using Auth.
Summary
Firebase Authentication lets you add user sign-in easily.
Use createUser to register and signIn to log in users.
Show clear messages to users about success or errors.
Practice
(1/5)
1. What is the main purpose of Firebase Authentication in an iOS app?
easy
A. To send push notifications to users
B. To store large files in the cloud
C. To design the app's user interface
D. To let users sign up and log in easily
Solution
Step 1: Understand Firebase Authentication's role
Firebase Authentication helps manage user sign-up and sign-in processes.
Step 2: Compare options with this role
Only To let users sign up and log in easily matches this purpose; others relate to different Firebase features or app functions.
Final Answer:
To let users sign up and log in easily -> Option D
Quick Check:
Firebase Authentication = User sign-in [OK]
Hint: Firebase Auth is for user login/signup only [OK]
Common Mistakes:
Confusing Authentication with storage or UI design
Thinking it handles notifications
Assuming it manages app layout
2. Which Swift code snippet correctly creates a new user with Firebase Authentication?
easy
A. Auth.signIn(withEmail: email, password: password)
B. Auth.auth().createUser(withEmail: email, password: password) { result, error in }
C. FirebaseAuth.createUser(email, password)
D. Auth.createUser(email: email, password: password)
Solution
Step 1: Recall correct Firebase method for user creation
The method is Auth.auth().createUser(withEmail:password:completion:).
Step 2: Check each option's syntax
Only Auth.auth().createUser(withEmail: email, password: password) { result, error in } uses the correct method and parameters; others are invalid or for sign-in.
Final Answer:
Auth.auth().createUser(withEmail: email, password: password) { result, error in } -> Option B
Quick Check:
Create user method = Auth.auth().createUser(withEmail: email, password: password) { result, error in } [OK]
Hint: Use Auth.auth().createUser for new users [OK]
Common Mistakes:
Using signIn method instead of createUser
Wrong method call without Auth.auth()
Missing completion handler closure
3. What will be printed if the following Swift code runs and the user enters a wrong password?
Auth.auth().signIn(withEmail: email, password: wrongPassword) { authResult, error in
if let error = error {
print("Login failed: \(error.localizedDescription)")
} else {
print("Login successful")
}
}
medium
A. Login failed: The password is invalid or the user does not have a password.
If sign-in fails, error is non-nil and prints the error message.
Step 2: Analyze wrong password case
Wrong password triggers error with message about invalid password.
Final Answer:
Login failed: The password is invalid or the user does not have a password. -> Option A
Quick Check:
Error message prints on wrong password [OK]
Hint: Errors print message; success prints success [OK]
Common Mistakes:
Assuming success prints even on error
Ignoring error optional unwrapping
Expecting no output on failure
4. Identify the error in this Swift Firebase sign-in code:
Auth.auth().signIn(withEmail: email, password: password) { authResult, error in
if error != nil {
print("Error signing in")
}
print("User signed in")
}
medium
A. Missing else block causes "User signed in" to print even on error
B. Incorrect method name for signIn
C. No completion handler provided
D. Password parameter is missing
Solution
Step 1: Review control flow in completion handler
Code prints "User signed in" regardless of error presence.
Step 2: Identify missing else block
Without else, success message prints even if sign-in failed.
Final Answer:
Missing else block causes "User signed in" to print even on error -> Option A
Quick Check:
Print success only if no error [OK]
Hint: Use else to separate success from error prints [OK]
Common Mistakes:
Printing success message unconditionally
Not checking error properly
Confusing method names
5. You want to create a Swift function that registers a user and shows an alert with success or error message. Which approach correctly handles this using Firebase Authentication?
hard
A. Call createUser, then immediately show success alert without waiting for completion
B. Call signIn instead of createUser for registration
C. Call createUser with completion handler, show alert inside handler based on error or success
D. Use createUser without email or password parameters
Solution
Step 1: Understand asynchronous nature of createUser
createUser runs asynchronously; alerts must be shown after completion.
Call createUser, then immediately show success alert without waiting for completion shows alert too early; C uses wrong method; D misses required parameters.
Final Answer:
Call createUser with completion handler, show alert inside handler based on error or success -> Option C
Quick Check:
Show alert after createUser completes [OK]
Hint: Show alerts only inside completion handlers [OK]
Common Mistakes:
Showing alerts before async call finishes
Using signIn for registration
Omitting required parameters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.