CHAPTER 23
Beginner
Authentication in iOS Apps
Updated: May 16, 2026
7 min read
# CHAPTER 23
Authentication in iOS Apps
1. Introduction
If you are building an app with a global database, you must control who has access to it. You cannot allow anonymous users to delete other people's posts or view private profile data. Your application must establish identity. Building a secure login system from scratch involves hashing passwords, managing JWT tokens, and handling complex encryption. Fortunately, Google's Firebase SDK provides FirebaseAuth, an industry-grade security wrapper that handles this instantly. In this chapter, we will master Authentication in iOS Apps, learning how to create secure accounts, process logins, and manage persistent user sessions.2. Learning Objectives
By the end of this chapter, you will be able to:- Enable Email/Password authentication in the Firebase Console.
-
Utilize the
Auth.auth().createUsermethod to register new users.
-
Utilize the
Auth.auth().signInmethod to authenticate existing users.
- Retrieve the unique User ID (UID) for database authorization.
- Listen to global authentication state changes to dynamically swap UI screens.
3. Enabling Authentication in Firebase
Before writing Swift code, you must explicitly enable the login module on Google's servers.- 1. Open the Firebase Web Console.
- 2. Navigate to Authentication in the left sidebar.
- 3. Click Get Started, then click the Sign-in method tab.
- 4. Select Email/Password, toggle it to "Enable", and click Save.
4. Creating a User Account (Sign Up)
Let's build the logic to securely register a new user. We must import theFirebaseAuth package.
swift
5. Logging In (Authentication)
The login process is mathematically identical, but uses thesignIn method instead.
swift
*Note: Firebase requires passwords to be at least 6 characters long by default. If it is shorter, the do-catch block will catch a specific Firebase error!*
6. Managing the Session (The "Magic" Switch)
When a user logs in, they expect to stay logged in even if they close the app. Firebase manages this session securely on the device automatically! However, we need our UI to know about it. We use the@Published broadcaster to track the current user. If the user exists, we show the Home Screen. If they are nil, we show the Login Screen!
swift
7. Mini Project: The Protected Screen UI
Let's build the absolute root of our application. It acts as a traffic cop.
swift
8. Common Mistakes
-
Logging In from Background Threads: If you execute the Firebase login function, and then attempt to immediately change a
@Publishedproperty (likeself.isLoggedIn = true) to swap the screens, you will trigger a Main Thread violation unless your ViewModel is explicitly marked with@MainActor.
-
Hardcoding Passwords: Never, ever hardcode testing credentials in your UI views. Always capture them dynamically via a
SecureFieldtwo-way binding.
9. Best Practices
-
Security Rules Mapping: Once you have a Firebase UID (
result.user.uid), you should use that exact UID as the document ID when saving their profile data in the Firestore database. This allows you to write a strict Firebase Security Rule: *"Only allow a user to edit this document if the document ID matches their Authentication UID."*
10. Exercises
- 1. Write the explicit Firebase Swift command required to log a user out of the application securely.
-
2.
Explain how the
SessionManagerin Section 6 knows the user is logged in even after the app has been completely closed and reopened.
11. Coding Challenges
Challenge: Build theLoginScreenView UI mentioned in Section 7. Create two @State variables for email and password. Use a TextField (with .keyboardType(.emailAddress)) and a SecureField. Add a "Sign In" button that triggers a local print statement to verify the UI is bound correctly.
12. MCQ Quiz with Answers
Question 1
Which Firebase SDK package must be explicitly imported at the top of the Swift file to gain access to the Auth.auth().signIn() methodologies?
Question 2
When successfully executing Auth.auth().createUser(withEmail:password:), what uniquely identifying cryptographic string is generated and returned by Firebase, which developers use to tie database records to that specific human being?
13. Interview Questions
- Q: Explain the immense architectural danger of rolling a custom authentication system (password hashing, salting, database storage) versus utilizing an enterprise BaaS solution like FirebaseAuth.
- Q: Describe the concept of a "Session State". How does FirebaseAuth maintain persistent sessions across application terminations, and how can SwiftUI react to these state changes?
-
Q: In a production application, detail the mechanism by which the root UI (
WindowGroup) acts as a router, dynamically swapping between an authentication flow and the main application dashboard based on the presence of a user token.
14. FAQs
Q: Can I let users log in with FaceID or Apple ID? A: Yes! Firebase Authentication fully supports "Sign in with Apple" (which is strictly required by Apple App Store guidelines if you offer social logins), as well as Google, Facebook, and Twitter logins. The SDK handles the complex OAuth token exchanges automatically.15. Summary
In Chapter 23, we fortified our application by establishing strict digital identity. We navigated the Firebase Console to enable secure Email/Password pipelines, and integrated the robust FirebaseAuth SDK. We mastered asynchronous authentication flows, securely executing bothcreateUser and signIn operations, while capturing the critical cryptographic UID. Finally, we engineered a persistent, reactive Session Manager, allowing the root SwiftUI architecture to dynamically barricade unauthenticated users from the core application experience.