Firebase Authentication in iOS

 

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.

And, add Firebase Initializer method in AppDebelagte.

 

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.

 

  • Register New User 

You can signup new user via new email address, mobile number. AuthenticationWorker class has a method register(user:, handler:)

 

  • 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.

  • Update Password

You can update password of the logged account. So, there is a method updatePassword(newPassword:, handler:) in AuthenticationWorker class.

 

  • Update Email Address

To update the email address of logged In account, you need to call updateEmail(newEmail: handler:) method

 

  • Send Verification Mail

When a new user registers himself, he should get a confirmation mail to its mail address to verify the account.

  • Remove User 

You can also remove an existing user from user-list.

 

Download AuthenticationWorker.swift

https://github.com/greenSyntax/ios-source/blob/master/Firebase/Authentication/AuthenticationWorker.swift

Leave a Reply