Firebase Authentication in iOS
When You want to create a user based app where you want to feature or activity depends on Users. I can assume, you need User Login, Forget password and Registration screen for user-related interactions. In most of the cases, Login/Registration User functionality doesn’t belong to Client end (i.e. iOS or Android App). It is related to backend where user-tables are maintained in a database so that every platform can validate/operate on data. And, most of the time Client Application interacts with backend using REST APIs.
Firebase is a backend-as-service. And, it has a module called Firebase Authentication which targets User-related operations. It internally maintain a User Records for where you can perform defined operations like,
- Login (via, Email Address, Mobile Number)
- Registration
- Logged User
- Logout Session
- Forget Password
- Send Verification Mail to associated email address
- Update Password/Email
- Update Profile Information
- Delete User
- OpenID Connect (like Google SignIn, Facebook Sign In, Twitter SignIn and Github Sign In)
Benefits of Firebase Authentication
- No Need to own a database. No need to create a backend. Firebase has done all your backend job. And, Handled most of the cases.
- Supports OpenID Connect. So, you can integrate Social Media Sign in.
- Cross-Platform SDK. It supports Android, iOS, Unity, Javascript, and Unity.
- Authentication via Phone Number
How to Setup Firebase App in Xcode
Step.1
Log into https://console.firebase.google.com/ and Add Project with your name.
Step2.
Create your Project and select your region,
Step3.
Choose your Platform. Here, I’ll choose iOS
Step4.
Here, add your iOS App bundle Id (say, com.abcd.myDemo)
Step5.
Download GoogleService-Info.plist and import in your project root directory.
Step6.
Rest is Firebase pod installation. This is I’m assuming that you know how to install pod in your project.
pod 'Firebase/Core' pod 'Firebase/Auth'
And, add Firebase Initializer method in AppDebelagte.
import UIKit import Firebase @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { FirebaseApp.configure() return true } }
AuthenticationWorker.swift
Its a wrapper class around Firebase Authentication Method which will help you as a developer to abstract Firebase Authentication method under a single class. It’s a reusable class so you can use it any of your independent projects.
Add AuthenticationWorker.Swift file in your project before using following methods.
- Login User
In AuthenticationWorker class, there is a method called login(user: handler:) which takes a UserModel type object and gives a completion handler:(User, Error) as a response.
let user = UserModel(email: "ab.abhishek.ravi@gmail.com", password: "myPassword") AuthenticationWorker.shared.login(user: user, handler: { (user, error) in guard error == nil else { // Error Occurred While Authentication print(error.localizedDescription) return } //Successfully Authenticated print(user) })
- Register New User
You can signup new user via new email address, mobile number. AuthenticationWorker class has a method register(user:, handler:)
AuthenticationWorker.shared.register(user: user, handler: { (user, error) in guard error == nil else { //onError Occurred While Registering print(error.localizedDescription) return } //onSuccessfull Registration print(user) })
- Get LoggedIn User
After Successful logged in, you can access the current logged in user from the method isLoggedIn() which will return an object of User type.
if let user = AuthenticationWorker.shared.isLoggedIn() { print(user) }
- Update Password
You can update password of the logged account. So, there is a method updatePassword(newPassword:, handler:) in AuthenticationWorker class.
AuthenticationWorker.shared.updatePassword(newPassword: "NewPassword") { (hasChanged, error) in guard error == nil else { print(error?.localizedDescription) return } //onSuccess print(hasChanged) }
- Update Email Address
To update the email address of logged In account, you need to call updateEmail(newEmail: handler:) method
AuthenticationWorker.shared.updateEmailAddress(newEmail: "newEmailAddress@gmail.com") { (user, error) in guard error == nil else { print(error?.localizedDescription) return } //Successfully changed Email Address of LoggedIn Account print(user) }
- Send Verification Mail
When a new user registers himself, he should get a confirmation mail to its mail address to verify the account.
AuthenticationWorker.shared.sendVerificationMail { (hasSent, error) in guard error == nil else { print(error?.localizedDescription) return } //onSuccessfully Sent Verification Mail print(hasSent) }
- Remove User
You can also remove an existing user from user-list.
AuthenticationWorker.shared.deleteUser { (hasDeleted, error) in guard error == nil else { print(error?.localizedDescription) return } //onSuccessfull Deletion of User print(hasDeleted) }
Download AuthenticationWorker.swift