Firebase Authentication gives us backend services to authenticate users with your app. It provides SDKs and ready-made UI libraries. It supports authentication using passwords, and other providers like Google, Facebook and Twitter, Github, and more.
Steps to configure your app with Firebase
- Open Firebase Console
- Create new project over there
- Create new app into the project
- You will be getting GoogleService-Info.plist file from settings
- Add it to the project
- Add just one line to configure your app FIRApp.configure()
1 2 3 4 5 6 7 8 9 10 |
import UIKit import Firebase @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { FIRApp.configure() return true } } |
Let’s step with Firebase Authentication
Step 1. Enable Email and Password authentication on console
Go to Firebase Console and open authentication tab. In authentication tab you will get 3 more tab Users, Sign-In Method and Email templates. We have to do Email and Password authentication so just enable that from the Sign-In Method tab.
Step 2. Jump on code by Create User/Register User in Firebase system
Firebase provides a simple method to create a user with email and password. Firebase will create a user and it will return Firebase user object if it got success else it will return an error object.
FIRAuth have createUser method which has arguments for email and password and it accepts the string value. It will return user and error object in block/closure.
1 2 3 4 5 6 7 8 |
FIRAuth.auth()?.createUser(withEmail: "USER@EMAIL.COM", password: "USER_PASSWORD") { (user, error) in if let error = error { print(error.localizedDescription) } else if let user = user { print(user) } } |
Following types of errors should return when you registering or creating a user:
- The password must be 6 characters long or more.
- The email address is already in use by another account.
Step 3. Login/Sign In with email and password
Firebase provides a method to sign in user with email and password. The method will return error and user object with closure/block. The method has two arguments email and password.
1 2 3 4 5 6 7 8 |
FIRAuth.auth()?.signIn(withEmail: "USER@EMAIL.COM", password: "USER_PASSWORD") { (user, error) in if let error = error { print(error.localizedDescription) } else if let user = user { print(user) } } |
Following types of errors should return when you sign in with firebase authentication:
- The email address is badly formatted.
- The password is invalid or the user does not have a password.
- There is no user record corresponding to this identifier.
Step 4. Listen to the events of login with authentication state change listener
Firebase provides authentication state did change listener, which will listen if any changes occur in FIRAuth (Firebase Authentication Shared Instance Object).
Declaration:
1 |
var handle: FIRAuthStateDidChangeListenerHandle? |
A method called addStateDidChangeListener used to add a listener on FIRAuth object which will result in FIRAuth and User object in closure/block.
Execution:
1 2 3 4 5 6 7 8 9 10 11 |
override func viewWillAppear(_ animated: Bool) { handle = FIRAuth.auth()?.addStateDidChangeListener() { (auth, user) in print(auth) if let user = user { print(user) } } } override func viewWillDisappear(_ animated: Bool) { FIRAuth.auth()?.removeStateDidChangeListener(handle!) } |
In above example, I have added listener when view will appear and removed that listener in view will disappear.
This is all about Email and password based authentication with Firebase. There are other methods available Google Sign In, Facebook login, GitHub authentication and more.
You can download the completed project with all of the code developed in this tutorial.
Hope you like this tutorial. Happy Coding 🙂